2 ;;; Copyright (c) 2006-2008, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;; Time-stamp: <2009-06-08 09:20:26 tony>
7 ;;; Creation: sometime in 2006...
9 ;;; Author: AJ Rossini <blindglobe@gmail.com>
10 ;;; Copyright: (c) 2007, AJ Rossini. BSD.
11 ;;; Purpose: demonstrations of how one might use CLSv2.
13 ;;; What is this talk of 'release'? Klingons do not make software
14 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
15 ;;; designers and quality assurance people in its wake.
20 ;; (asdf:oos 'asdf:compile-op 'lispstat :force t)
21 (asdf:oos
'asdf
:load-op
'lispstat
)
25 ;; a bit of infrastructure for beginners
26 (defparameter *my-cls-homedir
*
27 "/media/disk/Desktop/sandbox/CLS.git/")
28 (concatenate 'string
*my-cls-homedir
* "Data/example.csv")
30 (defun localized-pathto (x)
32 (concatenate 'string
*my-cls-homedir
* x
))
36 (defparameter *my-df-1
*
37 (make-instance 'dataframe-array
38 :storage
#2A
((1 2 3 4 5)
40 :doc
"This is an un-interesting dataframe-array"
41 :case-labels
(list "x" "y")
42 :var-labels
(list "a" "b" "c" "d" "e")))
44 (setf (dfref *my-df-1
* 0 0) -
1d0
)
48 (make-dataframe #2A
((1 2 3 4 5)
51 (make-dataframe (rand 4 3))
56 (defparameter *my-df-2
*
57 (make-dataframe #2A
((1 2 3 4 5)
59 :caselabels
(list "x" "y")
60 :varlabels
(list "a" "b" "c" "d" "e")
61 :doc
"This is another boring dataframe-array"))
63 (caselabels *my-df-1
*)
68 (defparameter *my-df-2
*
69 (make-dataframe #2A
((a 2 T
4 5)
71 :caselabels
(list "x" "y")
72 :varlabels
(list "a" "b" "c" "d" "e")
73 :doc
"This is another boring dataframe-array"))
80 ;;; read in a CSV dataframe...
83 ;; a better approach is:
84 (asdf:oos
'asdf
:load-op
'rsm-string
)
85 (rsm.string
:file-
>string-table
86 (localized-pathto "Data/example-mixed.csv")
89 (rsm.string
:file-
>number-table
90 (localized-pathto "Data/example-numeric.csv")
93 (rsm.string
:file-
>number-table
94 (localized-pathto "Data/R-chickwts.csv")
96 (rsm.string
:file-
>string-table
97 (localized-pathto "Data/R-chickwts.csv")
100 (defparameter *my-df-2
*
101 (make-instance 'dataframe-array
104 (transpose-listoflist
105 (rsm.string
:file-
>string-table
106 (localized-pathto "Data/example-mixed.csv"))))
107 :doc
"This is an interesting dataframe-array"))
110 (defparameter *my-df-3
*
111 (make-instance 'dataframe-array
114 (transpose-listoflist
115 (rsm.string
:file-
>number-table
116 (localized-pathto "Data/example-numeric.csv"))))
117 :doc
"This is an interesting dataframe-array"))
121 (defparameter *my-df-4
*
122 (make-instance 'dataframe-array
125 (rsm.string
:file-
>number-table
126 (localized-pathto "Data/R-chickwts.csv")
128 :doc
"This is an interesting dataframe-array that currently fails"))
132 (defparameter *my-df-5
*
133 (make-instance 'dataframe-array
136 (transpose-listoflist
137 (rsm.string
:file-
>number-table
138 (localized-pathto "Data/R-swiss.csv"))))
139 :doc
"This is an interesting dataframe-array that currently fails"))
143 (defparameter *mat-1
*
145 :initial-contents
#2A
((2d0 3d0
4d0
) (3d0 2d0
4d0
) (4d0 4d0
5d0
))))
147 (defparameter *mat-2
*
148 (let ((m (rand 3 3)))
149 (m* m
(transpose m
))))
151 (axpy 100.0d0
*mat-2
* (eye 3 3))
153 (potrf (copy *mat-2
*)) ;; factor
154 (potri (copy *mat-2
*)) ;; invert
155 (minv-cholesky (copy *mat-2
*))
156 (m* (minv-cholesky (copy *mat-2
*)) *mat-2
*)
158 (defparameter *mat-3
*
161 :initial-contents
'((16d0 13d0
12d0
)
165 (potrf (copy *mat-3
*)) ;; factor
169 #<LA-SIMPLE-MATRIX-DOUBLE
3 x
3
174 (potrf (copy *mat-3
*)) =>
175 (#<LA-SIMPLE-MATRIX-DOUBLE
3 x
3
177 13.0 3.3819373146171707 -
0.8131433980500301
178 12.0 7.0 2.7090215603069034>
181 ;; and compare with...
183 > testm
<- matrix
(data=c
(16,13,12,13,22,7,12,7,17),nrow
=3)
186 [1,] 4 3.250000 3.0000000
187 [2,] 0 3.381937 -
0.8131434
188 [3,] 0 0.000000 2.7090216
191 ;; which suggests that the major difference is that R zero's out the
192 ;; appropriate terms, and that CLS does not.
196 (potri (copy *mat-2
*)) ;; invert
197 (minv-cholesky (copy *mat-2
*))
198 (m* (minv-cholesky (copy *mat-2
*)) *mat-2
*)
202 (lu-decomp #2A
((2 3 4) (1 2 4) (2 4 5)))
203 ;; => (#2A((2.0 3.0 4.0) (1.0 1.0 1.0) (0.5 0.5 1.5)) #(0 2 2) -1.0 NIL)
205 (lu-decomp #2A
((2 3 4) (1 2 4) (2 4 5)))
207 ;; => #(-2.333333333333333 1.3333333333333335 0.6666666666666666)
211 :initial-contents
#2A
((2d0 3d0
4d0
) (1d0 2d0
4d0
) (2d0 4d0
5d0
))))
213 #|
=> ; so not so good for the vector, but matrix matches.
214 (#<LA-SIMPLE-MATRIX-DOUBLE
3 x
3
218 #<FNV-INT32
(3) 1 3 3> NIL
)
223 :initial-contents
#2A
((2d0 3d0
4d0
)
226 (make-vector 3 :type
:column
227 :initial-contents
'((2d0)
232 #<LA-SIMPLE-VECTOR-DOUBLE
(3 x
1)
240 ;;; LU common applications
243 "invert A using LU Factorization"
244 (let ((a-fac (getrf (copy a
))))
245 (first (getri (first a-fac
) (second a-fac
)))))
248 (let ((m1 (rand 3 3)))
249 (m* m1
(minv-lu m1
))))
251 (defun msolve-lu (a b
)
252 "Compute `x1' solving `A x = b', with LU factorization."
253 (let ((a-fac (getrf (copy a
))))
254 (first (getrs (first a-fac
) b
(second a-fac
)))))
258 ;; (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
259 ;; #2A((2.0 -0.33333333333333326 -1.3333333333333335)
260 ;; (-1.0 -0.6666666666666666 1.3333333333333333)
261 ;; (0.0 0.6666666666666666 -0.3333333333333333))
266 :initial-contents
#2A
((2d0 3d0
4d0
)
272 #<LA-SIMPLE-MATRIX-DOUBLE
3 x
3
273 2.0 -
0.3333333333333333 -
1.3333333333333333
274 -
1.0 -
0.6666666666666666 1.3333333333333333
275 0.0 0.6666666666666666 -
0.3333333333333333>
285 :initial-contents
#2A
((2d0 3d0
4d0
)
290 ;; (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
291 ;; (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
292 ;; (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
293 ;; (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
294 ;; #(9.699290438141343 0.8971681569301373 0.3447525123483081)
295 ;; #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
296 ;; (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
297 ;; (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
302 (qr-decomp #2A
((2 3 4) (1 2 4) (2 4 5)))
303 ;; (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
304 ;; (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
305 ;; (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
306 ;; #2A((-3.0 -5.333333333333334 -7.333333333333332)
307 ;; (0.0 -0.7453559924999292 -1.1925695879998877)
308 ;; (0.0 0.0 -1.3416407864998738)))
310 (rcondest #2A
((2 3 4) (1 2 4) (2 4 5)))
312 ;;; CURRENTLY FAILS!!
314 (eigen #2A
((2 3 4) (1 2 4) (2 4 5)))
315 ;; (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
316 ;; (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
317 ;; #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
318 ;; #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
321 (spline #(1.0
1.2 1.3 1.8 2.1 2.5)
322 #(1.2
2.0 2.1 2.0 1.1 2.8) :xvals
6)
323 ;; ((1.0 1.3 1.6 1.9 2.2 2.5)
324 ;; (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
326 ;;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
327 (kernel-smooth #(1.0
1.2 1.3 1.8 2.1 2.5)
328 #(1.2
2.0 2.1 2.0 1.1 2.8) :xvals
5)
329 ;; ((1.0 1.375 1.75 2.125 2.5)
330 ;; (1.6603277642110226 1.9471748095239771 1.7938127405752287
331 ;; 1.5871511322219498 2.518194783156392))
333 (kernel-dens #(1.0
1.2 2.5 2.1 1.8 1.2) :xvals
5)
334 ;; ((1.0 1.375 1.75 2.125 2.5)
335 ;; (0.7224150453621405 0.5820045548233707 0.38216411702854214
336 ;; 0.4829822708587095 0.3485939156929503))
338 (fft #(1.0
1.2 2.5 2.1 1.8))
339 ;; #(#C(1.0 0.0) #C(1.2 0.0) #C(2.5 0.0) #C(2.1 0.0) #C(1.8 0.0))
341 (lowess #(1.0
1.2 2.5 2.1 1.8 1.2) #(1.2
2.0 2.1 2.0 1.1 2.8))
342 ;; (#(1.0 1.2 1.2 1.8 2.1 2.5))
346 ;;;; Special functions
348 ;; Log-gamma function
350 (log-gamma 3.4) ;;1.0923280596789584
354 ;;;; Probability functions
356 ;; looking at these a bit more, perhaps a more CLOSy style is needed, i.e.
357 ;; (quantile :list-or-cons loc :type type (one of 'empirical 'normal 'cauchy, etc...))
358 ;; similar for the cdf, density, and rand.
359 ;; Probably worth figuring out how to add a new distribution
360 ;; efficiently, i.e. by keeping some kind of list.
362 ;; Normal distribution
364 (normal-quant 0.95) ;;1.6448536279366268
365 (normal-cdf 1.3) ;;0.9031995154143897
366 (normal-dens 1.3) ;;0.17136859204780736
367 (normal-rand 2) ;;(-0.40502015f0 -0.8091404f0)
369 (bivnorm-cdf 0.2 0.4 0.6) ;;0.4736873734160288
371 ;; Cauchy distribution
373 (cauchy-quant 0.95) ;;6.313751514675031
374 (cauchy-cdf 1.3) ;;0.7912855998398473
375 (cauchy-dens 1.3) ;;0.1183308127104695
376 (cauchy-rand 2) ;;(-1.06224644160405 -0.4524695943939537)
378 ;; Gamma distribution
380 (gamma-quant 0.95 4.3) ;;8.178692439291645
381 (gamma-cdf 1.3 4.3) ;;0.028895150986674906
382 (gamma-dens 1.3 4.3) ;;0.0731517686447374
383 (gamma-rand 2 4.3) ;;(2.454918912880936 4.081365384357454)
385 ;; Chi-square distribution
387 (chisq-quant 0.95 3) ;;7.814727903379012
388 (chisq-cdf 1 5) ;;0.03743422675631789
389 (chisq-dens 1 5) ;;0.08065690818083521
390 (chisq-rand 2 4) ;;(1.968535826180572 2.9988646156942997)
394 (beta-quant 0.95 3 2) ;;0.9023885371149876
395 (beta-cdf 0.4 2 2.4) ;;0.4247997418541529
396 (beta-dens 0.4 2 2.4) ;;1.5964741858913518
397 (beta-rand 2 2 2.4) ;;(0.8014897077282279 0.6516371997922659)
401 (t-quant 0.95 3) ;;2.35336343484194
402 (t-cdf 1 2.3) ;;0.794733624298342
403 (t-dens 1 2.3) ;;0.1978163816318102
404 (t-rand 2 2.3) ;;(-0.34303672776089306 -1.142505872436518)
408 (f-quant 0.95 3 5) ;;5.409451318117459
409 (f-cdf 1 3.2 5.4) ;;0.5347130905510765
410 (f-dens 1 3.2 5.4) ;;0.37551128864591415
411 (f-rand 2 3 2) ;;(0.7939093442091963 0.07442694152491144)
413 ;; Poisson distribution
415 (poisson-quant 0.95 3.2) ;;6
416 (poisson-cdf 1 3.2) ;;0.17120125672252395
417 (poisson-pmf 1 3.2) ;;0.13043905274097067
418 (poisson-rand 5 3.2) ;;(2 1 2 0 3)
420 ;; Binomial distribution
422 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
423 (binomial-quant 0 3 0.4) ;;; -2147483648
424 (binomial-cdf 1 3 0.4) ;;0.6479999999965776
425 (binomial-pmf 1 3 0.4) ;;0.4320000000226171
426 (binomial-rand 5 3 0.4) ;;(2 2 0 1 2)
430 (in-package :ls-user
)
431 (defproto *test-proto
*)
433 (defmeth *test-proto
* :make-data
(&rest args
) nil
)
435 (defparameter my-proto-instance nil
)
436 (setf my-proto-instance
(send *test-proto
* :new
))
437 (send *test-proto
* :own-slots
)
438 (lsos::ls-object-slots
*test-proto
*)
439 (lsos::ls-object-methods
*test-proto
*)
440 (lsos::ls-object-parents
*test-proto
*)
441 (lsos::ls-object-preclist
*test-proto
*)
442 ;;; The following fail and I do not know why?
443 (send *test-proto
* :has-slot
'proto-name
)
444 (send *test-proto
* :has-slot
'PROTO-NAME
)
445 (send *test-proto
* :has-slot
'make-data
)
446 (send *test-proto
* :has-slot
'MAKE-DATA
)
447 (send *test-proto
* :has-method
'make-data
)
448 (send *test-proto
* :has-method
'MAKE-DATA
)
451 (defproto2 *test-proto3
* (list) (list) (list) "test doc" t
)
452 (defproto2 *test-proto4
*)
454 (defmeth *test-proto
* :make-data
(&rest args
) nil
)
456 (defparameter my-proto-instance nil
)
457 (setf my-proto-instance
(send *test-proto
* :new
))
458 (send *test-proto
* :own-slots
)
459 (send *test-proto
* :has-slot
'proto-name
)
460 (send *test-proto
* :has-slot
'PROTO-NAME
)
465 (in-package :lisp-stat-unittests
)
472 (describe (run-tests :suite
'lisp-stat-ut-testsupport
))
473 (describe (run-tests :suite
'lisp-stat-ut-testsupport2
))
475 (testsuite-tests 'lisp-stat-ut
)
476 (run-tests :suite
'lisp-stat-ut
)
477 (describe (run-tests :suite
'lisp-stat-ut
))
479 (run-tests :suite
'lisp-stat-ut-probdistn
)
480 (describe (run-tests :suite
'lisp-stat-ut-probdistn
))
481 (run-tests :suite
'lisp-stat-ut-spec-fns
)
482 (describe (run-tests :suite
'lisp-stat-ut-spec-fns
))
484 (find-testsuite 'lisp-stat-ut-lin-alg
)
485 (testsuite-tests 'lisp-stat-ut-lin-alg
)
486 (run-tests :suite
'lisp-stat-ut-lin-alg
)
487 (describe (run-tests :suite
'lisp-stat-ut-lin-alg
))
489 ;;;; Data Analysis test
491 (in-package :ls-user
)
493 ;; LispStat 1 approach to variables
496 (def iron
(list 61 175 111 124 130 173 169 169 160 224 257 333 199))
498 (def aluminum
(list 13 21 24 23 64 38 33 61 39 71 112 88 54))
500 (def absorbtion
(list 4 18 14 18 26 26 21 30 28 36 65 62 40))
503 ;; LispStat 1 approach to data frames... (list of lists).
506 (QUOTE ((80 97 105 90 90 86 100 85 97 97 91 87 78 90 86 80 90 99 85 90 90 88 95 90 92 74 98 100 86 98 70 99 75 90 85 99 100 78 106 98 102 90 94 80 93 86 85 96 88 87 94 93 86 86 96 86 89 83 98 100 110 88 100 80 89 91 96 95 82 84 90 100 86 93 107 112 94 93 93 90 99 93 85 89 96 111 107 114 101 108 112 105 103 99 102 110 102 96 95 112 110 92 104 75 92 92 92 93 112 88 114 103 300 303 125 280 216 190 151 303 173 203 195 140 151 275 260 149 233 146 124 213 330 123 130 120 138 188 339 265 353 180 213 328 346)
507 (356 289 319 356 323 381 350 301 379 296 353 306 290 371 312 393 364 359 296 345 378 304 347 327 386 365 365 352 325 321 360 336 352 353 373 376 367 335 396 277 378 360 291 269 318 328 334 356 291 360 313 306 319 349 332 323 323 351 478 398 426 439 429 333 472 436 418 391 390 416 413 385 393 376 403 414 426 364 391 356 398 393 425 318 465 558 503 540 469 486 568 527 537 466 599 477 472 456 517 503 522 476 472 455 442 541 580 472 562 423 643 533 1468 1487 714 1470 1113 972 854 1364 832 967 920 613 857 1373 1133 849 1183 847 538 1001 1520 557 670 636 741 958 1354 1263 1428 923 1025 1246 1568)
508 (124 117 143 199 240 157 221 186 142 131 221 178 136 200 208 202 152 185 116 123 136 134 184 192 279 228 145 172 179 222 134 143 169 263 174 134 182 241 128 222 165 282 94 121 73 106 118 112 157 292 200 220 144 109 151 158 73 81 151 122 117 208 201 131 162 148 130 137 375 146 344 192 115 195 267 281 213 156 221 199 76 490 143 73 237 748 320 188 607 297 232 480 622 287 266 124 297 326 564 408 325 433 180 392 109 313 132 285 139 212 155 120 28 23 232 54 81 87 76 42 102 138 160 131 145 45 118 159 73 103 460 42 13 130 44 314 219 100 10 83 41 77 29 124 15)
509 (3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 2 2 3 2 2 3 3 3 3 2 3 3 3 3 3 2 3 3 3 3 3 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1))))
512 (DEF DLABS
(QUOTE ("GLUFAST" "GLUTEST" "INSTEST" "CCLASS")))
513 (format t
"loaded data.~%")
514 ) ;; eval at this point.
516 ;; Simple univariate variable-specific descriptions.
519 (sort-data absorbtion
)
521 (standard-deviation absorbtion
)
522 (interquartile-range absorbtion
)
524 (lisp-stat-matrix::bind-columns aluminum iron
)
525 (bind-columns aluminum iron
)
526 (apply #'bind-columns
(list aluminum iron
))
527 (lisp-stat-matrix::bind-columns
#2a
((1 2)(3 4)) #(5 6))
528 (bind-columns #2a
((1 2)(3 4)) #(5 6))
531 (defparameter fit1 nil
)
532 (setf fit1
(regression-model absorbtion iron
))
534 (send fit1
:residuals
)
537 (defparameter fit1a nil
)
538 (setf fit1a
(regression-model absorbtion iron
:print nil
))
540 ;; (setf (send fit1a :doc) "this") ;; FIXME: this error...
541 (send fit1a
:doc
"this") ;; FIXME: this is a more natural
545 (send fit1a
:compute
)
546 (send fit1a
:sweep-matrix
)
548 (send fit1a
:residuals
)
549 (send fit1a
:display
)
553 (array-dimension #2A
((1)) 0)
556 ;;; FIXME: need to get multiple-linear regression working -- clearly
557 ;;; simple linear is working above!
558 (defvar m nil
"holding variable.")
559 (def m
(regression-model (list iron aluminum
) absorbtion
:print nil
))
561 (send m
:sweep-matrix
)
562 (format t
"~%~A~%" (send m
:sweep-matrix
))
565 (send m
:basis
) ;; this should be positive?
566 (send m
:coef-estimates
)
569 (def m
(regression-model (bind-columns iron aluminum
) absorbtion
))
571 (send m
:help
:display
)
572 (send m
:help
:basis
)
573 ;; No graphics! But handle the error gracefully...
574 (send m
:plot-residuals
)
577 (typep aluminum
'sequence
)
578 (typep iron
'sequence
)
589 (asdf:oos
'asdf
:compile-op
'cl-cairo2
:force t
)
590 (asdf:oos
'asdf
:load-op
'cl-cairo2
)
592 ;; The above can be used to generate PDF, PS, PNG, and X11/Microsoft
593 ;; displays (the latter being a proof of concept, of limited use for
596 ;; and this below, as well.
597 (asdf:oos
'asdf
:load-op
'cl-plplot
)
601 (asdf:oos
'asdf
:compile-op
'rclg
:force t
)
602 (asdf:oos
'asdf
:load-op
'rclg
)
605 (in-package :rclg-user
)
607 ;; rclg-init::*r-started*
609 ;;;#3 Start R within Lisp
612 ;; rclg-init::*r-started*
613 (rclg-init::check-stack
)
615 (defparameter *x
* (r seq
1 11))
616 (defparameter *y
* (r rnorm
10))
621 (defparameter *r-version
* (r "version"))
623 ;; This is for illustrative purposes only. It is not a "good" use of rnbi.
624 ;; Really, you'll want rnbi to hold anonymous intermeditae results, like:
625 (r plot
*x
* (rnbi rnorm
10))
627 (r "Sys.getenv" "LD_LIBRARY_PATH")
628 (r "Sys.getenv" "LD_PRELOAD")
636 (r "library" "stats")
638 (r "library" "Biobase")
640 (setf my.lib
"Biobase")
646 (r "print.default" 3)
649 ;; Working in the R space
652 (r assign
"x2" (list 1 2 3 5))
654 (r assign
"x2" #(1 2 3 5 3 4 5))
655 (r assign
"z" "y") ;; unlike the above, this assigns character data
659 (setf my.r.x2
(r get
"x2")) ;; moving data from R to CL
660 (r assign
"x2" my.r.x2
) ;; moving data from CL to R
662 ;; The following is not the smartest thing to do!
667 ;;; How might we do statistics with Common Lisp?
668 ;;; How might we work with a data.frame?
669 ;;; What could the structures be?
670 ;;; How much hinting, and of what type, should drive the data
673 (defpackage :my-data-analysis-example
674 (:documentation
"Example work-package for a data analysis")
675 (:use
:common-lisp
:lisp-stat
)
676 (:export results figures report
))
678 (in-package :my-data-analysis-example
)
680 (defvar my-dataset1
(read-file "data/test1.lisp"))
682 (defvar my-dataset2
(read-file "data/test1.csv" :type
'csv
))
686 (setf my-dataset2
(set-description my-datasets2
687 :dependent-variables
(list of symbols
)))
688 (setf my-dataset2
(set-description my-datasets2
689 :independent-variables
(list of symbols
)))
691 ;; the following could be true in many cases.
693 (list-intersection (get-description my-datasets2
:independent-variables
)
694 (get-description my-datasets2
:dependent-variables
)))
696 ;; but we could phrase better,i.e.
700 :predicate-list-on-variable-metadata
(list (and 'independent-variables
701 'dependent-variables
)))
704 ;; statistical relations re: input/output, as done above, is one
705 ;; issue, another one is getting the right approach for statistical
709 :predicate-list-on-variable-metadata
(list 'ordinal-variables
))
712 ;; so we could use a set of logical ops to selection from variable
715 ;; do we really need the simplifying extensions?
720 (report my-dataset1
:style
'five-num
)
721 (report my-dataset1
:style
'univariate
)
722 (report my-dataset1
:style
'bivariate
)
723 (report my-dataset1
:style
'metadata
)
730 :stream
(filename-as-stream "my-dataset1-5num.pdf"))
731 (report my-dataset1
:style
'univariate
)
732 (report my-dataset1
:style
'bivariate
)
733 (report my-dataset1
:style
'metadata
)
735 ;;; so report could handle datasets... and models?
737 (report my-model
:style
'formula
)
738 (report my-model
:style
'simulate
739 (list :parameters
(:eta
5 :mu
4 :sigma
(list 2 1 0.5))
741 ;; should return a list of parameters along with range information,
742 ;; useful for auto-building the above. Note that there are 3 types
743 ;; of parameters that can be considered -- we can have values which
744 ;; define ddata, we can have values which define fixed values and some
745 ;; could be things tht we estimate.
748 (defgeneric report
(object &optional style format stream
)
749 (:documentation
"method for reporting on data"))
751 (defmethod report ((object dataset
)
752 (style report-dataset-style-type
)
753 (format output-format-type
)
754 ((stream *repl
*) output-stream-type
))
758 (defmethod report ((object model
)
759 (style report-model-style-type
)
760 (format output-format-type
)
761 ((stream *repl
*) output-stream-type
))
764 (defmethod report ((object analysis-instance
)
765 (style report-analysis-style-type
)
766 (format output-format-type
)
767 ((stream *repl
*) output-stream-type
))
768 "model + dataset reporting")
771 ;; parameters are just things which get filled with values, repeatedly
772 ;; with data, or by considering to need estimation.
773 (parameters my-model
)
774 (parameters my-model
:type
'data
)
775 (parameters my-model
:type
'fixed
)
776 (parameters my-model
:type
'estimate
)
777 (parameters my-model
:type
'(estimate fixed
))
778 (parameters my-model
:list-types
) ;; useful for list-based extraction
779 ;; of particular types
781 (setf my-model-data-instance
782 (compute model data
:specification
(list :spec
'linear-model
784 :indepvar
(list x1 x2
))))
785 (report my-model-data-instance
)
788 ;;; So how might we use this? Probably need to consider the
789 ;;; serialization of any lisp objects generated, perhaps via some form
790 ;;; of memoization...?
791 (in-package :cl-user
)
793 (my-data-analysis-example:report
:type
'full
)
794 (my-data-analysis-example:report
:type
'summary
)
795 (my-data-analysis-example:figures
:type
'pdf
:file
"results-figs.pdf")
797 (my-data-analysis-example:report
)
802 (def m
(regression-model (bind-columns iron aluminum
) absorbtion
))
804 (send m
:help
:display
)
805 (send m
:help
:basis
)
807 (send m
:plot-residuals
)
810 ;; General Lisp, there is also a need to add, remove symbols from the
811 ;; workspace/namespace. This is a fundamental skill, similar to
812 ;; stopping, which is critical.
815 ;; makunbound, fmakunbound
820 ;;; A study in array vs list access
821 (defparameter *x
* (list 1 2 3))
822 (defparameter *y
* #(1 2 3))
823 (defparameter *z
* (list 1 (list 2 3) (list 4 5 (list 6 7)) ))
826 (length *z
*) ; => need a means to make this 7.
827 (length (reduce #'cons
*z
*)) ; => not quite -- missing iterative
834 (setf (aref *y
* 1) 6)
838 (in-package :ls-user
)
841 (defparameter *x
* (make-vector 5 :initial-contents
'((1d0 2d0
3d0
4d0
5d0
))))
842 ;; estimating a mean, simple way.
843 (/ (loop for i from
0 to
(- (nelts *x
*) 1)
844 summing
(vref *x
* i
))
848 (checktype x
'vector-like
)
849 (/ (loop for i from
0 to
(- (nelts *x
*) 1)
850 summing
(vref *x
* i
))
853 ;; estimating variance, Moments
854 (let ((meanx (mean *x
*))
856 (/ (loop for i from
0 to
(1- n
)
857 summing
(* (- (vref *x
* i
) meanx
)
858 (- (vref *x
* i
) meanx
)))
861 ;; estimating variance, Moments
862 (let ((meanx (mean *x
*))
863 (nm1 (1- (nelts *x
*))))
864 (/ (loop for i from
0 to nm1
865 summing
(* (- (vref *x
* i
) meanx
)
866 (- (vref *x
* i
) meanx
) ))
871 ;;;;;;;;;;;;;;; Data stuff
875 ;; Making data-frames (i.e. cases (rows) by variables (columns))
876 ;; takes a bit of getting used to. For this, it is important to
877 ;; realize that we can do the following:
878 ;; #1 - consider the possibility of having a row, and transposing
879 ;; it, so the list-of-lists is: ((1 2 3 4 5)) (1 row, 5 columns)
880 ;; #2 - naturally list-of-lists: ((1)(2)(3)(4)(5)) (5 rows, 1 column)
881 ;; see src/data/listoflist.lisp for code to process this particular
883 (defparameter *indep-vars-1-matrix
*
884 (transpose (make-matrix 1 (length iron
)
886 (list (mapcar #'(lambda (x) (coerce x
'double-float
))
888 "creating iron into double float, straightforward")
890 (documentation '*indep-vars-1-matrix
* 'variable
)
891 ;; *indep-vars-1-matrix*
894 (defparameter *indep-vars-1a-matrix
*
895 (make-matrix (length iron
) 1
897 (mapcar #'(lambda (x) (list (coerce x
'double-float
)))
899 ;; *indep-vars-1a-matrix*
901 ;; and mathematically, they seem equal:
902 (m= *indep-vars-1-matrix
* *indep-vars-1a-matrix
*) ; => T
903 ;; but of course not completely...
904 (eql *indep-vars-1-matrix
* *indep-vars-1a-matrix
*) ; => NIL
905 (eq *indep-vars-1-matrix
* *indep-vars-1a-matrix
*) ; => NIL
908 (print *indep-vars-1-matrix
*)
909 (print *indep-vars-1a-matrix
*)
911 (documentation 'lisp-matrix
:bind2
'function
) ; by which we mean:
912 (documentation 'bind2
'function
)
913 (bind2 *indep-vars-1-matrix
* *indep-vars-1a-matrix
* :by
:column
) ; 2 col
914 (bind2 *indep-vars-1-matrix
* *indep-vars-1a-matrix
* :by
:row
) ; 1 long col
917 (defparameter *indep-vars-2-matrix
*
918 (transpose (make-matrix 2 (length iron
)
921 (mapcar #'(lambda (x) (coerce x
'double-float
))
923 (mapcar #'(lambda (x) (coerce x
'double-float
))
925 ;; *indep-vars-2-matrix*
928 (defparameter *indep-vars-2-matrix
*
929 (make-matrix (length iron
) 2
931 (mapcar #'(lambda (x y
)
932 (list (coerce x
'double-float
)
933 (coerce y
'double-float
)))
935 ;; *indep-vars-2-matrix*
938 ;; The below FAILS due to coercion issues; it just isn't lispy, it's R'y.
940 (defparameter *dep-var
* (make-vector (length absorbtion
)
941 :initial-contents
(list absorbtion
)))
943 ;; BUT below, this should be the right type.
944 (defparameter *dep-var
*
945 (make-vector (length absorbtion
)
949 (mapcar #'(lambda (x) (coerce x
'double-float
))
954 (defparameter *dep-var-int
*
955 (make-vector (length absorbtion
)
957 :element-type
'integer
958 :initial-contents
(list absorbtion
)))
960 (typep *dep-var
* 'matrix-like
) ; => T
961 (typep *dep-var
* 'vector-like
) ; => T
963 (typep *indep-vars-1-matrix
* 'matrix-like
) ; => T
964 (typep *indep-vars-1-matrix
* 'vector-like
) ; => T
965 (typep *indep-vars-2-matrix
* 'matrix-like
) ; => T
966 (typep *indep-vars-2-matrix
* 'vector-like
) ; => F
969 ;; following fails, need to ensure that we work on list elts, not just
970 ;; elts within a list:
972 ;; (coerce iron 'real)
974 ;; the following is a general list-conversion coercion approach -- is
975 ;; there a more efficient way?
977 ;; (mapcar #'(lambda (x) (coerce x 'double-float)) iron)
979 (princ "Data Set up"))
986 (describe 'make-matrix
)
988 (defparameter *indep-vars-2-matrix
*
989 (make-matrix (length iron
) 2
991 (mapcar #'(lambda (x y
)
992 (list (coerce x
'double-float
)
993 (coerce y
'double-float
)))
997 (defparameter *dep-var
*
998 (make-vector (length absorbtion
)
1002 (mapcar #'(lambda (x) (coerce x
'double-float
))
1005 (make-dataframe *dep-var
*)
1006 (make-dataframe (transpose *dep-var
*))
1008 (defparameter *dep-var-int
*
1009 (make-vector (length absorbtion
)
1011 :element-type
'integer
1012 :initial-contents
(list absorbtion
)))
1015 (defparameter *xv
+1a
*
1018 :initial-contents
#2A
((1d0 1d0
)
1027 (defparameter *xv
+1b
*
1032 :initial-contents
'((1d0)
1042 (m= *xv
+1a
* *xv
+1b
*) ; => T
1044 (princ "Data Set up"))
1056 :initial-contents
'((1d0 2d0
3d0
4d0
5d0
6d0
7d0
8d0
))))
1059 (defparameter *xv
+1*
1062 :initial-contents
'((1d0 1d0
)
1072 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
1073 (defparameter *xtx-2
* (m* (transpose *xv
+1*) *xv
+1*))
1074 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
1078 (defparameter *xty-2
* (m* (transpose *xv
+1*) (transpose *y
*)))
1079 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
1083 (defparameter *rcond-2
* 0.000001)
1084 (defparameter *betahat-2
* (gelsy *xtx-2
* *xty-2
* *rcond-2
*))
1085 ;; *xtx-2* => "details of complete orthogonal factorization"
1086 ;; according to man page:
1087 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
1088 ;; -119.33147112141039d0 -29.095426104883202d0
1089 ;; 0.7873402682880205d0 -1.20672274167718d0>
1091 ;; *xty-2* => output becomes solution:
1092 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
1093 ;; -0.16666666666668312d0
1094 ;; 1.333333333333337d0>
1096 *betahat-2
* ; which matches R, see below
1098 (documentation 'gelsy
'function
)
1101 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
1102 ;; -0.16666666666668312 1.333333333333337>
1105 ;; ## Test case in R:
1106 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
1107 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
1109 ;; ## => Call: lm(formula = y ~ x)
1111 ;; Coefficients: (Intercept) x
1118 ;; lm(formula = y ~ x)
1121 ;; Min 1Q Median 3Q Max
1122 ;; -1.833e+00 -6.667e-01 -3.886e-16 6.667e-01 1.833e+00
1125 ;; Estimate Std. Error t value Pr(>|t|)
1126 ;; (Intercept) -0.1667 1.1587 -0.144 0.89034
1127 ;; x 1.3333 0.3043 4.382 0.00466 **
1129 ;; Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
1131 ;; Residual standard error: 1.291 on 6 degrees of freedom
1132 ;; Multiple R-squared: 0.7619, Adjusted R-squared: 0.7222
1133 ;; F-statistic: 19.2 on 1 and 6 DF, p-value: 0.004659
1137 ;; which suggests one might do (modulo ensuring correct
1138 ;; orientations). When this is finalized, it should migrate to
1143 (defparameter *n
* 20) ; # rows = # obsns
1144 (defparameter *p
* 10) ; # cols = # vars
1145 (defparameter *x-temp
* (rand *n
* *p
*))
1146 (defparameter *b-temp
* (rand *p
* 1))
1147 (defparameter *y-temp
* (m* *x-temp
* *b-temp
*))
1149 (defparameter *rcond
* (* (coerce (expt 2 -
52) 'double-float
)
1150 (max (nrows *x-temp
*) (ncols *y-temp
*))))
1151 (defparameter *orig-x
* (copy *x-temp
*))
1152 (defparameter *orig-b
* (copy *b-temp
*))
1153 (defparameter *orig-y
* (copy *y-temp
*))
1155 (defparameter *lm-result
* (lm *x-temp
* *y-temp
*))
1156 (princ (first *lm-result
*))
1157 (princ (second *lm-result
*))
1158 (princ (third *lm-result
*))
1159 (v= (third *lm-result
*)
1160 (v- (first (first *lm-result
*))
1161 (first (second *lm-result
*))))
1166 ;; Some issues exist in the LAPACK vs. LINPACK variants, hence R
1167 ;; uses LINPACK primarily, rather than LAPACK. See comments in R
1168 ;; source for issues.
1171 ;; Goal is to start from X, Y and then realize that if
1172 ;; Y = X \beta, then, i.e. 8x1 = 8xp px1 + 8x1
1173 ;; XtX \hat\beta = Xt Y
1174 ;; so that we can solve the equation W \beta = Z where W and Z
1175 ;; are known, to estimate \beta.
1177 ;; the above is known to be numerically instable -- some processing
1178 ;; of X is preferred and should be done prior. And most of the
1179 ;; transformation-based work does precisely that.
1181 ;; recall: Var[Y] = E[(Y - E[Y])(Y-E[Y])t]
1182 ;; = E[Y Yt] - 2 \mu \mut + \mu \mut
1183 ;; = E[Y Yt] - \mu \mut
1185 ;; Var Y = E[Y^2] - \mu^2
1188 ;; For initial estimates of covariance of \hat\beta:
1190 ;; \hat\beta = (Xt X)^-1 Xt Y
1191 ;; with E[ \hat\beta ]
1192 ;; = E[ (Xt X)^-1 Xt Y ]
1193 ;; = E[(Xt X)^-1 Xt (X\beta)]
1196 ;; So Var[\hat\beta] = ...
1198 ;; and this gives SE(\beta_i) = (* (sqrt (mref Var i i)) adjustment)
1204 (let ((*default-implementation
* :foreign-array
))
1210 (rcond (* (coerce (expt 2 -
52) 'double-float
)
1211 (max (nrows a
) (ncols a
))))
1215 (list x
(gelsy a b rcond
))
1216 ;; no applicable conversion?
1217 ;; (m- (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1))
1218 ;; (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1)) )
1219 (v- x
(first (gelsy a b rcond
))))))
1222 (princ *temp-result
*)
1225 (let ((*default-implementation
* :lisp-array
))
1231 (rcond (* (coerce (expt 2 -
52) 'double-float
)
1232 (max (nrows a
) (ncols a
))))
1236 (list x
(gelsy a b rcond
))
1237 (m- x
(first (gelsy a b rcond
)))
1239 (princ *temp-result
*)
1245 :type
:row
;; default, not usually needed!
1246 :initial-contents
'((1d0 3d0
2d0
4d0
3d0
5d0
4d0
6d0
))))
1252 :initial-contents
'((1d0 2d0
3d0
4d0
5d0
6d0
7d0
8d0
))))
1254 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
1255 (defparameter *xtx-1
* (m* *xv
* (transpose *xv
*)))
1256 (defparameter *xty-1
* (m* *xv
* (transpose *y
*)))
1257 (defparameter *rcond-in
* (* (coerce (expt 2 -
52) 'double-float
)
1258 (max (nrows *xtx-1
*)
1261 (defparameter *betahat
* (gelsy *xtx-1
* *xty-1
* *rcond-in
*))
1263 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (1 x 1)
1264 ;; 1.293103448275862>
1267 ;; ## Test case in R:
1268 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
1269 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
1273 ;; lm(formula = y ~ x - 1)
1284 (type-of #2A
((1 2 3 4 5)
1287 (type-of (rand 10 20))
1289 (typep #2A
((1 2 3 4 5)
1293 (typep (rand 10 20) 'matrix-like
)
1295 (typep #2A
((1 2 3 4 5)
1299 (typep (rand 10 20) 'array
)