cholesky and LU computations demo'd.
[CommonLispStat.git] / ls-demo.lisp
blobda83a850c1b32eeefe2299da7a82e20c422d7024
1 ;;; -*- mode: lisp -*-
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-04 17:42:31 tony>
7 ;;; Creation: sometime in 2006...
8 ;;; File: ls-demo.lisp
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.
17 (in-package :cl-user)
20 ;; (asdf:oos 'asdf:compile-op 'lispstat :force t)
21 (asdf:oos 'asdf:load-op 'lispstat)
23 (in-package :ls-user)
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")
29 ;; implies
30 (defun localized-pathto (x)
31 (check-type x string)
32 (concatenate 'string *my-cls-homedir* x))
34 ;;; == READ DATA
36 (defparameter *my-df-1*
37 (make-instance 'dataframe-array
38 :storage #2A((1 2 3 4 5)
39 (10 20 30 40 50))
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)
45 ;; *my-df-1*
48 (make-dataframe #2A((1 2 3 4 5)
49 (10 20 30 40 50)))
51 (make-dataframe (rand 4 3))
56 (defparameter *my-df-2*
57 (make-dataframe #2A((1 2 3 4 5)
58 (10 20 30 40 50))
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*)
64 (varlabels *my-df-1*)
68 (defparameter *my-df-2*
69 (make-dataframe #2A((a 2 T 4 5)
70 (b 20 nil 40 50))
71 :caselabels (list "x" "y")
72 :varlabels (list "a" "b" "c" "d" "e")
73 :doc "This is another boring dataframe-array"))
75 ;; *my-df-2*
78 ;;; HERE#1
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")
87 :delims ",")
89 (rsm.string:file->number-table
90 (localized-pathto "Data/example-numeric.csv")
91 :delims ",")
93 (rsm.string:file->number-table
94 (localized-pathto "Data/R-chickwts.csv")
95 :delims ",")
96 (rsm.string:file->string-table
97 (localized-pathto "Data/R-chickwts.csv")
98 :delims ",")
100 (defparameter *my-df-2*
101 (make-instance 'dataframe-array
102 :storage
103 (listoflist->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"))
108 ;; *my-df-2*
110 (defparameter *my-df-3*
111 (make-instance 'dataframe-array
112 :storage
113 (listoflist->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"))
118 ;; *my-df-3*
121 (defparameter *my-df-4*
122 (make-instance 'dataframe-array
123 :storage
124 (listoflist->array
125 (rsm.string:file->number-table
126 (localized-pathto "Data/R-chickwts.csv")
127 :delims ","))
128 :doc "This is an interesting dataframe-array that currently fails"))
129 ;; *my-df-4*
132 (defparameter *my-df-5*
133 (make-instance 'dataframe-array
134 :storage
135 (listoflist->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"))
140 ;; *my-df-5*
143 (defparameter *mat-1*
144 (make-matrix 3 3
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*
159 (make-matrix
161 :initial-contents '((16d0 13d0 12d0)
162 (13d0 22d0 7d0)
163 (12d0 7d0 17d0))))
165 (potrf (copy *mat-3*)) ;; factor
168 *mat-3* =>
169 #<LA-SIMPLE-MATRIX-DOUBLE 3 x 3
170 16.0 13.0 12.0
171 13.0 22.0 7.0
172 12.0 7.0 17.0>
174 (potrf (copy *mat-3*)) =>
175 (#<LA-SIMPLE-MATRIX-DOUBLE 3 x 3
176 4.0 3.25 3.0
177 13.0 3.3819373146171707 -0.8131433980500301
178 12.0 7.0 2.7090215603069034>
179 "U" NIL)
181 ;; and compare with...
183 > testm <- matrix(data=c(16,13,12,13,22,7,12,7,17),nrow=3)
184 > chol(testm)
185 [,1] [,2] [,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*)
203 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
204 ;; (#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)
206 (lu-solve
207 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
208 #(2 3 4))
209 ;; #(-2.333333333333333 1.3333333333333335 0.6666666666666666)
214 ;; (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
215 ;; #2A((2.0 -0.33333333333333326 -1.3333333333333335)
216 ;; (-1.0 -0.6666666666666666 1.3333333333333333)
217 ;; (0.0 0.6666666666666666 -0.3333333333333333))
219 (minv-lu
220 (make-matrix
222 :initial-contents #2A((2d0 3d0 4d0)
223 (1d0 2d0 4d0)
224 (2d0 4d0 5d0))))
228 #<LA-SIMPLE-MATRIX-DOUBLE 3 x 3
229 2.0 -0.3333333333333333 -1.3333333333333333
230 -1.0 -0.6666666666666666 1.3333333333333333
231 0.0 0.6666666666666666 -0.3333333333333333>
233 ;; so is correct.
237 ;;;;;HERE#2
240 ;; (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
241 ;; (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
242 ;; (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
243 ;; (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
244 ;; #(9.699290438141343 0.8971681569301373 0.3447525123483081)
245 ;; #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
246 ;; (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
247 ;; (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
248 ;; T)
252 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
253 ;; (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
254 ;; (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
255 ;; (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
256 ;; #2A((-3.0 -5.333333333333334 -7.333333333333332)
257 ;; (0.0 -0.7453559924999292 -1.1925695879998877)
258 ;; (0.0 0.0 -1.3416407864998738)))
260 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
261 ;; 6.8157451e7
262 ;;; CURRENTLY FAILS!!
264 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
265 ;; (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
266 ;; (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
267 ;; #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
268 ;; #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
269 ;; NIL)
271 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
272 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 6)
273 ;; ((1.0 1.3 1.6 1.9 2.2 2.5)
274 ;; (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
276 ;;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
277 (kernel-smooth #(1.0 1.2 1.3 1.8 2.1 2.5)
278 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 5)
279 ;; ((1.0 1.375 1.75 2.125 2.5)
280 ;; (1.6603277642110226 1.9471748095239771 1.7938127405752287
281 ;; 1.5871511322219498 2.518194783156392))
283 (kernel-dens #(1.0 1.2 2.5 2.1 1.8 1.2) :xvals 5)
284 ;; ((1.0 1.375 1.75 2.125 2.5)
285 ;; (0.7224150453621405 0.5820045548233707 0.38216411702854214
286 ;; 0.4829822708587095 0.3485939156929503))
288 (fft #(1.0 1.2 2.5 2.1 1.8))
289 ;; #(#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))
291 (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))
292 ;; (#(1.0 1.2 1.2 1.8 2.1 2.5))
296 ;;;; Special functions
298 ;; Log-gamma function
300 (log-gamma 3.4) ;;1.0923280596789584
304 ;;;; Probability functions
306 ;; looking at these a bit more, perhaps a more CLOSy style is needed, i.e.
307 ;; (quantile :list-or-cons loc :type type (one of 'empirical 'normal 'cauchy, etc...))
308 ;; similar for the cdf, density, and rand.
309 ;; Probably worth figuring out how to add a new distribution
310 ;; efficiently, i.e. by keeping some kind of list.
312 ;; Normal distribution
314 (normal-quant 0.95) ;;1.6448536279366268
315 (normal-cdf 1.3) ;;0.9031995154143897
316 (normal-dens 1.3) ;;0.17136859204780736
317 (normal-rand 2) ;;(-0.40502015f0 -0.8091404f0)
319 (bivnorm-cdf 0.2 0.4 0.6) ;;0.4736873734160288
321 ;; Cauchy distribution
323 (cauchy-quant 0.95) ;;6.313751514675031
324 (cauchy-cdf 1.3) ;;0.7912855998398473
325 (cauchy-dens 1.3) ;;0.1183308127104695
326 (cauchy-rand 2) ;;(-1.06224644160405 -0.4524695943939537)
328 ;; Gamma distribution
330 (gamma-quant 0.95 4.3) ;;8.178692439291645
331 (gamma-cdf 1.3 4.3) ;;0.028895150986674906
332 (gamma-dens 1.3 4.3) ;;0.0731517686447374
333 (gamma-rand 2 4.3) ;;(2.454918912880936 4.081365384357454)
335 ;; Chi-square distribution
337 (chisq-quant 0.95 3) ;;7.814727903379012
338 (chisq-cdf 1 5) ;;0.03743422675631789
339 (chisq-dens 1 5) ;;0.08065690818083521
340 (chisq-rand 2 4) ;;(1.968535826180572 2.9988646156942997)
342 ;; Beta distribution
344 (beta-quant 0.95 3 2) ;;0.9023885371149876
345 (beta-cdf 0.4 2 2.4) ;;0.4247997418541529
346 (beta-dens 0.4 2 2.4) ;;1.5964741858913518
347 (beta-rand 2 2 2.4) ;;(0.8014897077282279 0.6516371997922659)
349 ;; t distribution
351 (t-quant 0.95 3) ;;2.35336343484194
352 (t-cdf 1 2.3) ;;0.794733624298342
353 (t-dens 1 2.3) ;;0.1978163816318102
354 (t-rand 2 2.3) ;;(-0.34303672776089306 -1.142505872436518)
356 ;; F distribution
358 (f-quant 0.95 3 5) ;;5.409451318117459
359 (f-cdf 1 3.2 5.4) ;;0.5347130905510765
360 (f-dens 1 3.2 5.4) ;;0.37551128864591415
361 (f-rand 2 3 2) ;;(0.7939093442091963 0.07442694152491144)
363 ;; Poisson distribution
365 (poisson-quant 0.95 3.2) ;;6
366 (poisson-cdf 1 3.2) ;;0.17120125672252395
367 (poisson-pmf 1 3.2) ;;0.13043905274097067
368 (poisson-rand 5 3.2) ;;(2 1 2 0 3)
370 ;; Binomial distribution
372 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
373 (binomial-quant 0 3 0.4) ;;; -2147483648
374 (binomial-cdf 1 3 0.4) ;;0.6479999999965776
375 (binomial-pmf 1 3 0.4) ;;0.4320000000226171
376 (binomial-rand 5 3 0.4) ;;(2 2 0 1 2)
378 ;;;; OBJECT SYSTEM
380 (in-package :ls-user)
381 (defproto *test-proto*)
382 *test-proto*
383 (defmeth *test-proto* :make-data (&rest args) nil)
385 (defparameter my-proto-instance nil)
386 (setf my-proto-instance (send *test-proto* :new))
387 (send *test-proto* :own-slots)
388 (lsos::ls-object-slots *test-proto*)
389 (lsos::ls-object-methods *test-proto*)
390 (lsos::ls-object-parents *test-proto*)
391 (lsos::ls-object-preclist *test-proto*)
392 ;;; The following fail and I do not know why?
393 (send *test-proto* :has-slot 'proto-name)
394 (send *test-proto* :has-slot 'PROTO-NAME)
395 (send *test-proto* :has-slot 'make-data)
396 (send *test-proto* :has-slot 'MAKE-DATA)
397 (send *test-proto* :has-method 'make-data)
398 (send *test-proto* :has-method 'MAKE-DATA)
401 (defproto2 *test-proto3* (list) (list) (list) "test doc" t)
402 (defproto2 *test-proto4*)
403 *test-proto2*
404 (defmeth *test-proto* :make-data (&rest args) nil)
406 (defparameter my-proto-instance nil)
407 (setf my-proto-instance (send *test-proto* :new))
408 (send *test-proto* :own-slots)
409 (send *test-proto* :has-slot 'proto-name)
410 (send *test-proto* :has-slot 'PROTO-NAME)
413 ;;;; Testing
415 (in-package :lisp-stat-unittests)
416 (testsuites)
417 (print-tests)
418 (run-tests)
419 (last-test-status)
420 ;;(failures)
422 (describe (run-tests :suite 'lisp-stat-ut-testsupport))
423 (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
425 (testsuite-tests 'lisp-stat-ut)
426 (run-tests :suite 'lisp-stat-ut)
427 (describe (run-tests :suite 'lisp-stat-ut))
429 (run-tests :suite 'lisp-stat-ut-probdistn)
430 (describe (run-tests :suite 'lisp-stat-ut-probdistn))
431 (run-tests :suite 'lisp-stat-ut-spec-fns)
432 (describe (run-tests :suite 'lisp-stat-ut-spec-fns))
434 (find-testsuite 'lisp-stat-ut-lin-alg)
435 (testsuite-tests 'lisp-stat-ut-lin-alg)
436 (run-tests :suite 'lisp-stat-ut-lin-alg)
437 (describe (run-tests :suite 'lisp-stat-ut-lin-alg))
439 ;;;; Data Analysis test
441 (in-package :ls-user)
443 ;; LispStat 1 approach to variables
445 (progn
446 (def iron (list 61 175 111 124 130 173 169 169 160 224 257 333 199))
447 iron
448 (def aluminum (list 13 21 24 23 64 38 33 61 39 71 112 88 54))
449 aluminum
450 (def absorbtion (list 4 18 14 18 26 26 21 30 28 36 65 62 40))
451 absorbtion
453 ;; LispStat 1 approach to data frames... (list of lists).
455 (DEF DIABETES
456 (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)
457 (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)
458 (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)
459 (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))))
462 (DEF DLABS (QUOTE ("GLUFAST" "GLUTEST" "INSTEST" "CCLASS")))
463 (format t "loaded data.~%")
464 ) ;; eval at this point.
466 ;; Simple univariate variable-specific descriptions.
467 (fivnum absorbtion)
468 (median absorbtion)
469 (sort-data absorbtion)
470 (rank absorbtion)
471 (standard-deviation absorbtion)
472 (interquartile-range absorbtion)
474 (lisp-stat-matrix::bind-columns aluminum iron)
475 (bind-columns aluminum iron)
476 (apply #'bind-columns (list aluminum iron))
477 (lisp-stat-matrix::bind-columns #2a((1 2)(3 4)) #(5 6))
478 (bind-columns #2a((1 2)(3 4)) #(5 6))
481 (defparameter fit1 nil)
482 (setf fit1 (regression-model absorbtion iron))
483 (send fit1 :display)
484 (send fit1 :residuals)
486 iron
487 (defparameter fit1a nil)
488 (setf fit1a (regression-model absorbtion iron :print nil))
489 (send fit1a :doc)
490 ;; (setf (send fit1a :doc) "this") ;; FIXME: this error...
491 (send fit1a :doc "this") ;; FIXME: this is a more natural
492 (send fit1a :doc)
493 (send fit1a :x)
494 (send fit1a :y)
495 (send fit1a :compute)
496 (send fit1a :sweep-matrix)
497 (send fit1a :basis)
498 (send fit1a :residuals)
499 (send fit1a :display)
501 #+nil(progn
502 ;; syntax example
503 (array-dimension #2A ((1)) 0)
506 ;;; FIXME: need to get multiple-linear regression working -- clearly
507 ;;; simple linear is working above!
508 (defvar m nil "holding variable.")
509 (def m (regression-model (list iron aluminum) absorbtion :print nil))
510 (send m :compute)
511 (send m :sweep-matrix)
512 (format t "~%~A~%" (send m :sweep-matrix))
514 ;; ERROR... FIX-ME!!
515 (send m :basis) ;; this should be positive?
516 (send m :coef-estimates)
518 (send m :display)
519 (def m (regression-model (bind-columns iron aluminum) absorbtion))
520 (send m :help)
521 (send m :help :display)
522 (send m :help :basis)
523 ;; No graphics! But handle the error gracefully...
524 (send m :plot-residuals)
527 (typep aluminum 'sequence)
528 (typep iron 'sequence)
529 (matrixp iron)
531 *variables*
533 (variables)
534 (undef 'iron)
535 (variables)
537 ;;; Plotting!
539 (asdf:oos 'asdf:compile-op 'cl-cairo2 :force t)
540 (asdf:oos 'asdf:load-op 'cl-cairo2)
542 ;; The above can be used to generate PDF, PS, PNG, and X11/Microsoft
543 ;; displays (the latter being a proof of concept, of limited use for
544 ;; "real work".
546 ;; and this below, as well.
547 (asdf:oos 'asdf:load-op 'cl-plplot)
549 ;;; Using R!
551 (asdf:oos 'asdf:compile-op 'rclg :force t)
552 (asdf:oos 'asdf:load-op 'rclg)
555 (in-package :rclg-user)
557 ;; rclg-init::*r-started*
559 ;;;#3 Start R within Lisp
561 (start-rclg)
562 ;; rclg-init::*r-started*
563 (rclg-init::check-stack)
564 (r "Cstack_info")
565 (defparameter *x* (r seq 1 11))
566 (defparameter *y* (r rnorm 10))
568 (r plot *x* *y*)
571 (defparameter *r-version* (r "version"))
573 ;; This is for illustrative purposes only. It is not a "good" use of rnbi.
574 ;; Really, you'll want rnbi to hold anonymous intermeditae results, like:
575 (r plot *x* (rnbi rnorm 10))
577 (r "Sys.getenv" "LD_LIBRARY_PATH")
578 (r "Sys.getenv" "LD_PRELOAD")
580 (r "ls")
581 (r ls)
582 (r "search")
584 (r "geterrmessage")
586 (r "library" "stats")
587 (r library "MASS")
588 (r "library" "Biobase")
590 (setf my.lib "Biobase")
591 my.lib
592 (r library my.lib)
594 (r "ls")
596 (r "print.default" 3)
597 (r "rnorm" 10)
599 ;; Working in the R space
601 (r assign "x" 5)
602 (r assign "x2" (list 1 2 3 5))
604 (r assign "x2" #(1 2 3 5 3 4 5))
605 (r assign "z" "y") ;; unlike the above, this assigns character data
606 (r "ls")
607 (r ls)
609 (setf my.r.x2 (r get "x2")) ;; moving data from R to CL
610 (r assign "x2" my.r.x2) ;; moving data from CL to R
612 ;; The following is not the smartest thing to do!
613 ;;(r q)
617 ;;; How might we do statistics with Common Lisp?
618 ;;; How might we work with a data.frame?
619 ;;; What could the structures be?
620 ;;; How much hinting, and of what type, should drive the data
621 ;;; analysis?
623 (defpackage :my-data-analysis-example
624 (:documentation "Example work-package for a data analysis")
625 (:use :common-lisp :lisp-stat)
626 (:export results figures report))
628 (in-package :my-data-analysis-example)
630 (defvar my-dataset1 (read-file "data/test1.lisp"))
631 ;; or
632 (defvar my-dataset2 (read-file "data/test1.csv" :type 'csv))
634 ;;; manipulate
636 (setf my-dataset2 (set-description my-datasets2
637 :dependent-variables (list of symbols)))
638 (setf my-dataset2 (set-description my-datasets2
639 :independent-variables (list of symbols)))
641 ;; the following could be true in many cases.
642 (assert
643 (list-intersection (get-description my-datasets2 :independent-variables)
644 (get-description my-datasets2 :dependent-variables)))
646 ;; but we could phrase better,i.e.
648 (get-description
649 my-datasets2
650 :predicate-list-on-variable-metadata (list (and 'independent-variables
651 'dependent-variables)))
654 ;; statistical relations re: input/output, as done above, is one
655 ;; issue, another one is getting the right approach for statistical
656 ;; typing, i.e.
657 (get-description
658 my-datasets2
659 :predicate-list-on-variable-metadata (list 'ordinal-variables))
662 ;; so we could use a set of logical ops to selection from variable
663 ;; metadata, i.e.
664 ;; and, or, not
665 ;; do we really need the simplifying extensions?
668 ;;; output to REPL
670 (report my-dataset1 :style 'five-num)
671 (report my-dataset1 :style 'univariate)
672 (report my-dataset1 :style 'bivariate)
673 (report my-dataset1 :style 'metadata)
675 ;;; to file?
677 (report my-dataset1
678 :style 'five-num
679 :format 'pdf
680 :stream (filename-as-stream "my-dataset1-5num.pdf"))
681 (report my-dataset1 :style 'univariate)
682 (report my-dataset1 :style 'bivariate)
683 (report my-dataset1 :style 'metadata)
685 ;;; so report could handle datasets... and models?
687 (report my-model :style 'formula)
688 (report my-model :style 'simulate
689 (list :parameters (:eta 5 :mu 4 :sigma (list 2 1 0.5))
690 :number-of-reps 10))
691 ;; should return a list of parameters along with range information,
692 ;; useful for auto-building the above. Note that there are 3 types
693 ;; of parameters that can be considered -- we can have values which
694 ;; define ddata, we can have values which define fixed values and some
695 ;; could be things tht we estimate.
698 (defgeneric report (object &optional style format stream)
699 (:documentation "method for reporting on data"))
701 (defmethod report ((object dataset)
702 (style report-dataset-style-type)
703 (format output-format-type)
704 ((stream *repl*) output-stream-type))
705 "dataset reporting")
708 (defmethod report ((object model)
709 (style report-model-style-type)
710 (format output-format-type)
711 ((stream *repl*) output-stream-type))
712 "model reporting")
714 (defmethod report ((object analysis-instance)
715 (style report-analysis-style-type)
716 (format output-format-type)
717 ((stream *repl*) output-stream-type))
718 "model + dataset reporting")
721 ;; parameters are just things which get filled with values, repeatedly
722 ;; with data, or by considering to need estimation.
723 (parameters my-model)
724 (parameters my-model :type 'data)
725 (parameters my-model :type 'fixed)
726 (parameters my-model :type 'estimate)
727 (parameters my-model :type '(estimate fixed))
728 (parameters my-model :list-types) ;; useful for list-based extraction
729 ;; of particular types
731 (setf my-model-data-instance
732 (compute model data :specification (list :spec 'linear-model
733 :depvar y
734 :indepvar (list x1 x2))))
735 (report my-model-data-instance)
738 ;;; So how might we use this? Probably need to consider the
739 ;;; serialization of any lisp objects generated, perhaps via some form
740 ;;; of memoization...?
741 (in-package :cl-user)
743 (my-data-analysis-example:report :type 'full)
744 (my-data-analysis-example:report :type 'summary)
745 (my-data-analysis-example:figures :type 'pdf :file "results-figs.pdf")
747 (my-data-analysis-example:report)
749 ;;; more stuff
751 (send m :display)
752 (def m (regression-model (bind-columns iron aluminum) absorbtion))
753 (send m :help)
754 (send m :help :display)
755 (send m :help :basis)
757 (send m :plot-residuals)
759 (progn
760 ;; General Lisp, there is also a need to add, remove symbols from the
761 ;; workspace/namespace. This is a fundamental skill, similar to
762 ;; stopping, which is critical.
764 ;; boundp, fboundp
765 ;; makunbound, fmakunbound
769 (progn
770 ;;; A study in array vs list access
771 (defparameter *x* (list 1 2 3))
772 (defparameter *y* #(1 2 3))
773 (defparameter *z* (list 1 (list 2 3) (list 4 5 (list 6 7)) ))
774 (length *x*)
775 (length *y*)
776 (length *z*) ; => need a means to make this 7.
777 (length (reduce #'cons *z*)) ; => not quite -- missing iterative
779 (nelts *x*)
780 (nth 1 *x*)
781 (aref *y* 1)
782 (setf (nth 1 *x*) 6)
784 (setf (aref *y* 1) 6)
788 (in-package :ls-user)
790 (progn
791 (defparameter *x* (make-vector 5 :initial-contents '((1d0 2d0 3d0 4d0 5d0))))
792 ;; estimating a mean, simple way.
793 (/ (loop for i from 0 to (- (nelts *x*) 1)
794 summing (vref *x* i))
795 (nelts *x*))
797 (defun mean (x)
798 (checktype x 'vector-like)
799 (/ (loop for i from 0 to (- (nelts *x*) 1)
800 summing (vref *x* i))
801 (nelts *x*)))
803 ;; estimating variance, Moments
804 (let ((meanx (mean *x*))
805 (n (nelts *x*)))
806 (/ (loop for i from 0 to (1- n)
807 summing (* (- (vref *x* i) meanx)
808 (- (vref *x* i) meanx)))
811 ;; estimating variance, Moments
812 (let ((meanx (mean *x*))
813 (nm1 (1- (nelts *x*))))
814 (/ (loop for i from 0 to nm1
815 summing (* (- (vref *x* i) meanx)
816 (- (vref *x* i) meanx) ))
817 nm1))
821 ;;;;;;;;;;;;;;; Data stuff
823 (progn ;; Data setup
825 ;; Making data-frames (i.e. cases (rows) by variables (columns))
826 ;; takes a bit of getting used to. For this, it is important to
827 ;; realize that we can do the following:
828 ;; #1 - consider the possibility of having a row, and transposing
829 ;; it, so the list-of-lists is: ((1 2 3 4 5)) (1 row, 5 columns)
830 ;; #2 - naturally list-of-lists: ((1)(2)(3)(4)(5)) (5 rows, 1 column)
831 ;; see src/data/listoflist.lisp for code to process this particular
832 ;; data structure.
833 (defparameter *indep-vars-1-matrix*
834 (transpose (make-matrix 1 (length iron)
835 :initial-contents
836 (list (mapcar #'(lambda (x) (coerce x 'double-float))
837 iron))))
838 "creating iron into double float, straightforward")
840 (documentation '*indep-vars-1-matrix* 'variable)
841 ;; *indep-vars-1-matrix*
843 ;; or directly:
844 (defparameter *indep-vars-1a-matrix*
845 (make-matrix (length iron) 1
846 :initial-contents
847 (mapcar #'(lambda (x) (list (coerce x 'double-float)))
848 iron)))
849 ;; *indep-vars-1a-matrix*
851 ;; and mathematically, they seem equal:
852 (m= *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => T
853 ;; but of course not completely...
854 (eql *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL
855 (eq *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL
857 ;; and verify...
858 (print *indep-vars-1-matrix*)
859 (print *indep-vars-1a-matrix*)
861 (documentation 'lisp-matrix:bind2 'function) ; by which we mean:
862 (documentation 'bind2 'function)
863 (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :column) ; 2 col
864 (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :row) ; 1 long col
866 ;; the weird way
867 (defparameter *indep-vars-2-matrix*
868 (transpose (make-matrix 2 (length iron)
869 :initial-contents
870 (list
871 (mapcar #'(lambda (x) (coerce x 'double-float))
872 iron)
873 (mapcar #'(lambda (x) (coerce x 'double-float))
874 aluminum)))))
875 ;; *indep-vars-2-matrix*
877 ;; the "right"? way
878 (defparameter *indep-vars-2-matrix*
879 (make-matrix (length iron) 2
880 :initial-contents
881 (mapcar #'(lambda (x y)
882 (list (coerce x 'double-float)
883 (coerce y 'double-float)))
884 iron aluminum)))
885 ;; *indep-vars-2-matrix*
888 ;; The below FAILS due to coercion issues; it just isn't lispy, it's R'y.
890 (defparameter *dep-var* (make-vector (length absorbtion)
891 :initial-contents (list absorbtion)))
893 ;; BUT below, this should be the right type.
894 (defparameter *dep-var*
895 (make-vector (length absorbtion)
896 :type :row
897 :initial-contents
898 (list
899 (mapcar #'(lambda (x) (coerce x 'double-float))
900 absorbtion))))
901 ;; *dep-var*
904 (defparameter *dep-var-int*
905 (make-vector (length absorbtion)
906 :type :row
907 :element-type 'integer
908 :initial-contents (list absorbtion)))
910 (typep *dep-var* 'matrix-like) ; => T
911 (typep *dep-var* 'vector-like) ; => T
913 (typep *indep-vars-1-matrix* 'matrix-like) ; => T
914 (typep *indep-vars-1-matrix* 'vector-like) ; => T
915 (typep *indep-vars-2-matrix* 'matrix-like) ; => T
916 (typep *indep-vars-2-matrix* 'vector-like) ; => F
918 iron
919 ;; following fails, need to ensure that we work on list elts, not just
920 ;; elts within a list:
922 ;; (coerce iron 'real)
924 ;; the following is a general list-conversion coercion approach -- is
925 ;; there a more efficient way?
926 ;; (coerce 1 'real)
927 ;; (mapcar #'(lambda (x) (coerce x 'double-float)) iron)
929 (princ "Data Set up"))
934 (progn ;; Data setup
936 (describe 'make-matrix)
938 (defparameter *indep-vars-2-matrix*
939 (make-matrix (length iron) 2
940 :initial-contents
941 (mapcar #'(lambda (x y)
942 (list (coerce x 'double-float)
943 (coerce y 'double-float)))
944 iron aluminum)))
947 (defparameter *dep-var*
948 (make-vector (length absorbtion)
949 :type :row
950 :initial-contents
951 (list
952 (mapcar #'(lambda (x) (coerce x 'double-float))
953 absorbtion))))
955 (make-dataframe *dep-var*)
956 (make-dataframe (transpose *dep-var*))
958 (defparameter *dep-var-int*
959 (make-vector (length absorbtion)
960 :type :row
961 :element-type 'integer
962 :initial-contents (list absorbtion)))
965 (defparameter *xv+1a*
966 (make-matrix
968 :initial-contents #2A((1d0 1d0)
969 (1d0 3d0)
970 (1d0 2d0)
971 (1d0 4d0)
972 (1d0 3d0)
973 (1d0 5d0)
974 (1d0 4d0)
975 (1d0 6d0))))
977 (defparameter *xv+1b*
978 (bind2
979 (ones 8 1)
980 (make-matrix
982 :initial-contents '((1d0)
983 (3d0)
984 (2d0)
985 (4d0)
986 (3d0)
987 (5d0)
988 (4d0)
989 (6d0)))
990 :by :column))
992 (m= *xv+1a* *xv+1b*) ; => T
994 (princ "Data Set up"))
998 ;;;; LM
1000 (progn
1002 (defparameter *y*
1003 (make-vector
1005 :type :row
1006 :initial-contents '((1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0))))
1009 (defparameter *xv+1*
1010 (make-matrix
1012 :initial-contents '((1d0 1d0)
1013 (1d0 3d0)
1014 (1d0 2d0)
1015 (1d0 4d0)
1016 (1d0 3d0)
1017 (1d0 5d0)
1018 (1d0 4d0)
1019 (1d0 6d0))))
1022 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
1023 (defparameter *xtx-2* (m* (transpose *xv+1*) *xv+1*))
1024 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
1025 ;; 8.0d0 28.0d0
1026 ;; 28.0d0 116.0d0>
1028 (defparameter *xty-2* (m* (transpose *xv+1*) (transpose *y*)))
1029 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
1030 ;; 36.0d0
1031 ;; 150.0d0>
1033 (defparameter *rcond-2* 0.000001)
1034 (defparameter *betahat-2* (gelsy *xtx-2* *xty-2* *rcond-2*))
1035 ;; *xtx-2* => "details of complete orthogonal factorization"
1036 ;; according to man page:
1037 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
1038 ;; -119.33147112141039d0 -29.095426104883202d0
1039 ;; 0.7873402682880205d0 -1.20672274167718d0>
1041 ;; *xty-2* => output becomes solution:
1042 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
1043 ;; -0.16666666666668312d0
1044 ;; 1.333333333333337d0>
1046 *betahat-2* ; which matches R, see below
1048 (documentation 'gelsy 'function)
1051 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
1052 ;; -0.16666666666668312 1.333333333333337>
1053 ;; 2)
1055 ;; ## Test case in R:
1056 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
1057 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
1058 ;; lm(y~x)
1059 ;; ## => Call: lm(formula = y ~ x)
1061 ;; Coefficients: (Intercept) x
1062 ;; -0.1667 1.3333
1064 ;; summary(lm(y~x))
1065 ;; ## =>
1067 ;; Call:
1068 ;; lm(formula = y ~ x)
1070 ;; Residuals:
1071 ;; Min 1Q Median 3Q Max
1072 ;; -1.833e+00 -6.667e-01 -3.886e-16 6.667e-01 1.833e+00
1074 ;; Coefficients:
1075 ;; Estimate Std. Error t value Pr(>|t|)
1076 ;; (Intercept) -0.1667 1.1587 -0.144 0.89034
1077 ;; x 1.3333 0.3043 4.382 0.00466 **
1078 ;; ---
1079 ;; Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
1081 ;; Residual standard error: 1.291 on 6 degrees of freedom
1082 ;; Multiple R-squared: 0.7619, Adjusted R-squared: 0.7222
1083 ;; F-statistic: 19.2 on 1 and 6 DF, p-value: 0.004659
1087 ;; which suggests one might do (modulo ensuring correct
1088 ;; orientations). When this is finalized, it should migrate to
1089 ;; CLS.
1093 (defparameter *n* 20) ; # rows = # obsns
1094 (defparameter *p* 10) ; # cols = # vars
1095 (defparameter *x-temp* (rand *n* *p*))
1096 (defparameter *b-temp* (rand *p* 1))
1097 (defparameter *y-temp* (m* *x-temp* *b-temp*))
1098 ;; so Y=Xb + \eps
1099 (defparameter *rcond* (* (coerce (expt 2 -52) 'double-float)
1100 (max (nrows *x-temp*) (ncols *y-temp*))))
1101 (defparameter *orig-x* (copy *x-temp*))
1102 (defparameter *orig-b* (copy *b-temp*))
1103 (defparameter *orig-y* (copy *y-temp*))
1105 (defparameter *lm-result* (lm *x-temp* *y-temp*))
1106 (princ (first *lm-result*))
1107 (princ (second *lm-result*))
1108 (princ (third *lm-result*))
1109 (v= (third *lm-result*)
1110 (v- (first (first *lm-result*))
1111 (first (second *lm-result*))))
1116 ;; Some issues exist in the LAPACK vs. LINPACK variants, hence R
1117 ;; uses LINPACK primarily, rather than LAPACK. See comments in R
1118 ;; source for issues.
1121 ;; Goal is to start from X, Y and then realize that if
1122 ;; Y = X \beta, then, i.e. 8x1 = 8xp px1 + 8x1
1123 ;; XtX \hat\beta = Xt Y
1124 ;; so that we can solve the equation W \beta = Z where W and Z
1125 ;; are known, to estimate \beta.
1127 ;; the above is known to be numerically instable -- some processing
1128 ;; of X is preferred and should be done prior. And most of the
1129 ;; transformation-based work does precisely that.
1131 ;; recall: Var[Y] = E[(Y - E[Y])(Y-E[Y])t]
1132 ;; = E[Y Yt] - 2 \mu \mut + \mu \mut
1133 ;; = E[Y Yt] - \mu \mut
1135 ;; Var Y = E[Y^2] - \mu^2
1138 ;; For initial estimates of covariance of \hat\beta:
1140 ;; \hat\beta = (Xt X)^-1 Xt Y
1141 ;; with E[ \hat\beta ]
1142 ;; = E[ (Xt X)^-1 Xt Y ]
1143 ;; = E[(Xt X)^-1 Xt (X\beta)]
1144 ;; = \beta
1146 ;; So Var[\hat\beta] = ...
1147 ;; (Xt X)
1148 ;; and this gives SE(\beta_i) = (* (sqrt (mref Var i i)) adjustment)
1151 ;; from docs:
1153 (setf *temp-result*
1154 (let ((*default-implementation* :foreign-array))
1155 (let* ((m 10)
1156 (n 10)
1157 (a (rand m n))
1158 (x (rand n 1))
1159 (b (m* a x))
1160 (rcond (* (coerce (expt 2 -52) 'double-float)
1161 (max (nrows a) (ncols a))))
1162 (orig-a (copy a))
1163 (orig-b (copy b))
1164 (orig-x (copy x)))
1165 (list x (gelsy a b rcond))
1166 ;; no applicable conversion?
1167 ;; (m- (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1))
1168 ;; (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1)) )
1169 (v- x (first (gelsy a b rcond))))))
1172 (princ *temp-result*)
1174 (setf *temp-result*
1175 (let ((*default-implementation* :lisp-array))
1176 (let* ((m 10)
1177 (n 10)
1178 (a (rand m n))
1179 (x (rand n 1))
1180 (b (m* a x))
1181 (rcond (* (coerce (expt 2 -52) 'double-float)
1182 (max (nrows a) (ncols a))))
1183 (orig-a (copy a))
1184 (orig-b (copy b))
1185 (orig-x (copy x)))
1186 (list x (gelsy a b rcond))
1187 (m- x (first (gelsy a b rcond)))
1189 (princ *temp-result*)
1192 (defparameter *xv*
1193 (make-vector
1195 :type :row ;; default, not usually needed!
1196 :initial-contents '((1d0 3d0 2d0 4d0 3d0 5d0 4d0 6d0))))
1198 (defparameter *y*
1199 (make-vector
1201 :type :row
1202 :initial-contents '((1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0))))
1204 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
1205 (defparameter *xtx-1* (m* *xv* (transpose *xv*)))
1206 (defparameter *xty-1* (m* *xv* (transpose *y*)))
1207 (defparameter *rcond-in* (* (coerce (expt 2 -52) 'double-float)
1208 (max (nrows *xtx-1*)
1209 (ncols *xty-1*))))
1211 (defparameter *betahat* (gelsy *xtx-1* *xty-1* *rcond-in*))
1213 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (1 x 1)
1214 ;; 1.293103448275862>
1215 ;; 1)
1217 ;; ## Test case in R:
1218 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
1219 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
1220 ;; lm(y~x-1)
1221 ;; ## =>
1222 ;; Call:
1223 ;; lm(formula = y ~ x - 1)
1225 ;; Coefficients:
1226 ;; x
1227 ;; 1.293
1229 (first *betahat*))
1234 (type-of #2A((1 2 3 4 5)
1235 (10 20 30 40 50)))
1237 (type-of (rand 10 20))
1239 (typep #2A((1 2 3 4 5)
1240 (10 20 30 40 50))
1241 'matrix-like)
1243 (typep (rand 10 20) 'matrix-like)
1245 (typep #2A((1 2 3 4 5)
1246 (10 20 30 40 50))
1247 'array)
1249 (typep (rand 10 20) 'array)