CLSv1 demo, using older proto system.
[CommonLispStat.git] / ls-demo.lisp
blob11032968339737cd86bf2b9337fb5fe178fb972d
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-05-26 09:17:22 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)
19 ;; (asdf:oos 'asdf:compile-op 'lispstat :force t)
20 (asdf:oos 'asdf:load-op 'lispstat)
22 (in-package :ls-user)
25 ;;; == READ DATA
27 (defparameter *my-df-1*
28 (make-instance 'dataframe-array
29 :storage #2A((1 2 3 4 5)
30 (10 20 30 40 50))
31 :doc "This is an un-interesting dataframe-array"
32 :case-labels (list "x" "y")
33 :var-labels (list "a" "b" "c" "d" "e")))
35 (setf (dfref *my-df-1* 0 0) -1d0)
36 ;; *my-df-1*
39 (make-dataframe #2A((1 2 3 4 5)
40 (10 20 30 40 50)))
42 (make-dataframe (rand 4 3))
47 (defparameter *my-df-2*
48 (make-dataframe #2A((1 2 3 4 5)
49 (10 20 30 40 50))
50 :caselabels (list "x" "y")
51 :varlabels (list "a" "b" "c" "d" "e")
52 :doc "This is another boring dataframe-array"))
54 (caselabels *my-df-1*)
55 (varlabels *my-df-1*)
59 (defparameter *my-df-2*
60 (make-dataframe #2A((a 2 T 4 5)
61 (b 20 nil 40 50))
62 :caselabels (list "x" "y")
63 :varlabels (list "a" "b" "c" "d" "e")
64 :doc "This is another boring dataframe-array"))
66 ;; *my-df-2*
69 ;;; HERE
71 ;;; read in a CSV dataframe...
81 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
82 ;; (#2A((1.7888543819998317 0.0 0.0)
83 ;; (1.6770509831248424 0.11180339887498929 0.0)
84 ;; (2.23606797749979 2.23606797749979 3.332000937312528e-8))
85 ;; 5.000000000000003)
89 (defparameter my-chol-decomp-test (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5))))
90 my-chol-decomp-test
91 (nth 0 my-chol-decomp-test)
92 (nth 1 my-chol-decomp-test)
95 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
96 ;; (#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)
98 (lu-solve
99 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
100 #(2 3 4))
101 ;; #(-2.333333333333333 1.3333333333333335 0.6666666666666666)
103 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
104 ;; #2A((2.0 -0.33333333333333326 -1.3333333333333335)
105 ;; (-1.0 -0.6666666666666666 1.3333333333333333)
106 ;; (0.0 0.6666666666666666 -0.3333333333333333))
108 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
109 ;; (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
110 ;; (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
111 ;; (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
112 ;; #(9.699290438141343 0.8971681569301373 0.3447525123483081)
113 ;; #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
114 ;; (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
115 ;; (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
116 ;; T)
118 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
119 ;; (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
120 ;; (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
121 ;; (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
122 ;; #2A((-3.0 -5.333333333333334 -7.333333333333332)
123 ;; (0.0 -0.7453559924999292 -1.1925695879998877)
124 ;; (0.0 0.0 -1.3416407864998738)))
126 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
127 ;; 6.8157451e7
128 ;;; CURRENTLY FAILS!!
130 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
131 ;; (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
132 ;; (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
133 ;; #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
134 ;; #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
135 ;; NIL)
137 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
138 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 6)
139 ;; ((1.0 1.3 1.6 1.9 2.2 2.5)
140 ;; (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
142 ;;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
143 (kernel-smooth #(1.0 1.2 1.3 1.8 2.1 2.5)
144 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 5)
145 ;; ((1.0 1.375 1.75 2.125 2.5)
146 ;; (1.6603277642110226 1.9471748095239771 1.7938127405752287
147 ;; 1.5871511322219498 2.518194783156392))
149 (kernel-dens #(1.0 1.2 2.5 2.1 1.8 1.2) :xvals 5)
150 ;; ((1.0 1.375 1.75 2.125 2.5)
151 ;; (0.7224150453621405 0.5820045548233707 0.38216411702854214
152 ;; 0.4829822708587095 0.3485939156929503))
154 (fft #(1.0 1.2 2.5 2.1 1.8))
155 ;; #(#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))
157 (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))
158 ;; (#(1.0 1.2 1.2 1.8 2.1 2.5))
162 ;;;; Special functions
164 ;; Log-gamma function
166 (log-gamma 3.4) ;;1.0923280596789584
170 ;;;; Probability functions
172 ;; looking at these a bit more, perhaps a more CLOSy style is needed, i.e.
173 ;; (quantile :list-or-cons loc :type type (one of 'empirical 'normal 'cauchy, etc...))
174 ;; similar for the cdf, density, and rand.
175 ;; Probably worth figuring out how to add a new distribution
176 ;; efficiently, i.e. by keeping some kind of list.
178 ;; Normal distribution
180 (normal-quant 0.95) ;;1.6448536279366268
181 (normal-cdf 1.3) ;;0.9031995154143897
182 (normal-dens 1.3) ;;0.17136859204780736
183 (normal-rand 2) ;;(-0.40502015f0 -0.8091404f0)
185 (bivnorm-cdf 0.2 0.4 0.6) ;;0.4736873734160288
187 ;; Cauchy distribution
189 (cauchy-quant 0.95) ;;6.313751514675031
190 (cauchy-cdf 1.3) ;;0.7912855998398473
191 (cauchy-dens 1.3) ;;0.1183308127104695
192 (cauchy-rand 2) ;;(-1.06224644160405 -0.4524695943939537)
194 ;; Gamma distribution
196 (gamma-quant 0.95 4.3) ;;8.178692439291645
197 (gamma-cdf 1.3 4.3) ;;0.028895150986674906
198 (gamma-dens 1.3 4.3) ;;0.0731517686447374
199 (gamma-rand 2 4.3) ;;(2.454918912880936 4.081365384357454)
201 ;; Chi-square distribution
203 (chisq-quant 0.95 3) ;;7.814727903379012
204 (chisq-cdf 1 5) ;;0.03743422675631789
205 (chisq-dens 1 5) ;;0.08065690818083521
206 (chisq-rand 2 4) ;;(1.968535826180572 2.9988646156942997)
208 ;; Beta distribution
210 (beta-quant 0.95 3 2) ;;0.9023885371149876
211 (beta-cdf 0.4 2 2.4) ;;0.4247997418541529
212 (beta-dens 0.4 2 2.4) ;;1.5964741858913518
213 (beta-rand 2 2 2.4) ;;(0.8014897077282279 0.6516371997922659)
215 ;; t distribution
217 (t-quant 0.95 3) ;;2.35336343484194
218 (t-cdf 1 2.3) ;;0.794733624298342
219 (t-dens 1 2.3) ;;0.1978163816318102
220 (t-rand 2 2.3) ;;(-0.34303672776089306 -1.142505872436518)
222 ;; F distribution
224 (f-quant 0.95 3 5) ;;5.409451318117459
225 (f-cdf 1 3.2 5.4) ;;0.5347130905510765
226 (f-dens 1 3.2 5.4) ;;0.37551128864591415
227 (f-rand 2 3 2) ;;(0.7939093442091963 0.07442694152491144)
229 ;; Poisson distribution
231 (poisson-quant 0.95 3.2) ;;6
232 (poisson-cdf 1 3.2) ;;0.17120125672252395
233 (poisson-pmf 1 3.2) ;;0.13043905274097067
234 (poisson-rand 5 3.2) ;;(2 1 2 0 3)
236 ;; Binomial distribution
238 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
239 (binomial-quant 0 3 0.4) ;;; -2147483648
240 (binomial-cdf 1 3 0.4) ;;0.6479999999965776
241 (binomial-pmf 1 3 0.4) ;;0.4320000000226171
242 (binomial-rand 5 3 0.4) ;;(2 2 0 1 2)
244 ;;;; OBJECT SYSTEM
246 (in-package :ls-user)
247 (defproto *test-proto*)
248 *test-proto*
249 (defmeth *test-proto* :make-data (&rest args) nil)
251 (defparameter my-proto-instance nil)
252 (setf my-proto-instance (send *test-proto* :new))
253 (send *test-proto* :own-slots)
254 (lsos::ls-object-slots *test-proto*)
255 (lsos::ls-object-methods *test-proto*)
256 (lsos::ls-object-parents *test-proto*)
257 (lsos::ls-object-preclist *test-proto*)
258 ;;; The following fail and I do not know why?
259 (send *test-proto* :has-slot 'proto-name)
260 (send *test-proto* :has-slot 'PROTO-NAME)
261 (send *test-proto* :has-slot 'make-data)
262 (send *test-proto* :has-slot 'MAKE-DATA)
263 (send *test-proto* :has-method 'make-data)
264 (send *test-proto* :has-method 'MAKE-DATA)
267 (defproto2 *test-proto3* (list) (list) (list) "test doc" t)
268 (defproto2 *test-proto4*)
269 *test-proto2*
270 (defmeth *test-proto* :make-data (&rest args) nil)
272 (defparameter my-proto-instance nil)
273 (setf my-proto-instance (send *test-proto* :new))
274 (send *test-proto* :own-slots)
275 (send *test-proto* :has-slot 'proto-name)
276 (send *test-proto* :has-slot 'PROTO-NAME)
279 ;;;; Testing
281 (in-package :lisp-stat-unittests)
282 (testsuites)
283 (print-tests)
284 (run-tests)
285 (last-test-status)
286 ;;(failures)
288 (describe (run-tests :suite 'lisp-stat-ut-testsupport))
289 (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
291 (testsuite-tests 'lisp-stat-ut)
292 (run-tests :suite 'lisp-stat-ut)
293 (describe (run-tests :suite 'lisp-stat-ut))
295 (run-tests :suite 'lisp-stat-ut-probdistn)
296 (describe (run-tests :suite 'lisp-stat-ut-probdistn))
297 (run-tests :suite 'lisp-stat-ut-spec-fns)
298 (describe (run-tests :suite 'lisp-stat-ut-spec-fns))
300 (find-testsuite 'lisp-stat-ut-lin-alg)
301 (testsuite-tests 'lisp-stat-ut-lin-alg)
302 (run-tests :suite 'lisp-stat-ut-lin-alg)
303 (describe (run-tests :suite 'lisp-stat-ut-lin-alg))
305 ;;;; Data Analysis test
307 (in-package :ls-user)
309 ;; LispStat 1 approach to variables
311 (progn
312 (def iron (list 61 175 111 124 130 173 169 169 160 224 257 333 199))
313 iron
314 (def aluminum (list 13 21 24 23 64 38 33 61 39 71 112 88 54))
315 aluminum
316 (def absorbtion (list 4 18 14 18 26 26 21 30 28 36 65 62 40))
317 absorbtion
319 ;; LispStat 1 approach to data frames... (list of lists).
321 (DEF DIABETES
322 (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)
323 (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)
324 (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)
325 (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))))
328 (DEF DLABS (QUOTE ("GLUFAST" "GLUTEST" "INSTEST" "CCLASS")))
329 (format t "loaded data.~%")
330 ) ;; eval at this point.
332 ;; Simple univariate variable-specific descriptions.
333 (fivnum absorbtion)
334 (median absorbtion)
335 (sort-data absorbtion)
336 (rank absorbtion)
337 (standard-deviation absorbtion)
338 (interquartile-range absorbtion)
340 (lisp-stat-matrix::bind-columns aluminum iron)
341 (bind-columns aluminum iron)
342 (apply #'bind-columns (list aluminum iron))
343 (lisp-stat-matrix::bind-columns #2a((1 2)(3 4)) #(5 6))
344 (bind-columns #2a((1 2)(3 4)) #(5 6))
347 (defparameter fit1 nil)
348 (setf fit1 (regression-model absorbtion iron))
349 (send fit1 :display)
350 (send fit1 :residuals)
352 iron
353 (defparameter fit1a nil)
354 (setf fit1a (regression-model absorbtion iron :print nil))
355 (send fit1a :doc)
356 ;; (setf (send fit1a :doc) "this") ;; FIXME: this error...
357 (send fit1a :doc "this") ;; FIXME: this is a more natural
358 (send fit1a :doc)
359 (send fit1a :x)
360 (send fit1a :y)
361 (send fit1a :compute)
362 (send fit1a :sweep-matrix)
363 (send fit1a :basis)
364 (send fit1a :residuals)
365 (send fit1a :display)
367 #+nil(progn
368 ;; syntax example
369 (array-dimension #2A ((1)) 0)
372 ;;; FIXME: need to get multiple-linear regression working -- clearly
373 ;;; simple linear is working above!
374 (defvar m nil "holding variable.")
375 (def m (regression-model (list iron aluminum) absorbtion :print nil))
376 (send m :compute)
377 (send m :sweep-matrix)
378 (format t "~%~A~%" (send m :sweep-matrix))
380 ;; ERROR... FIX-ME!!
381 (send m :basis) ;; this should be positive?
382 (send m :coef-estimates)
384 (send m :display)
385 (def m (regression-model (bind-columns iron aluminum) absorbtion))
386 (send m :help)
387 (send m :help :display)
388 (send m :help :basis)
389 ;; No graphics! But handle the error gracefully...
390 (send m :plot-residuals)
393 (typep aluminum 'sequence)
394 (typep iron 'sequence)
395 (matrixp iron)
397 *variables*
399 (variables)
400 (undef 'iron)
401 (variables)
403 ;;; Plotting!
405 (asdf:oos 'asdf:compile-op 'cl-cairo2 :force t)
406 (asdf:oos 'asdf:load-op 'cl-cairo2)
408 ;; The above can be used to generate PDF, PS, PNG, and X11/Microsoft
409 ;; displays (the latter being a proof of concept, of limited use for
410 ;; "real work".
412 ;; and this below, as well.
413 (asdf:oos 'asdf:load-op 'cl-plplot)
415 ;;; Using R!
417 (asdf:oos 'asdf:compile-op 'rclg :force t)
418 (asdf:oos 'asdf:load-op 'rclg)
421 (in-package :rclg-user)
423 ;; rclg-init::*r-started*
425 ;;;#3 Start R within Lisp
427 (start-rclg)
428 ;; rclg-init::*r-started*
429 (rclg-init::check-stack)
430 (r "Cstack_info")
431 (defparameter *x* (r seq 1 11))
432 (defparameter *y* (r rnorm 10))
434 (r plot *x* *y*)
437 (defparameter *r-version* (r "version"))
439 ;; This is for illustrative purposes only. It is not a "good" use of rnbi.
440 ;; Really, you'll want rnbi to hold anonymous intermeditae results, like:
441 (r plot *x* (rnbi rnorm 10))
443 (r "Sys.getenv" "LD_LIBRARY_PATH")
444 (r "Sys.getenv" "LD_PRELOAD")
446 (r "ls")
447 (r ls)
448 (r "search")
450 (r "geterrmessage")
452 (r "library" "stats")
453 (r library "MASS")
454 (r "library" "Biobase")
456 (setf my.lib "Biobase")
457 my.lib
458 (r library my.lib)
460 (r "ls")
462 (r "print.default" 3)
463 (r "rnorm" 10)
465 ;; Working in the R space
467 (r assign "x" 5)
468 (r assign "x2" (list 1 2 3 5))
470 (r assign "x2" #(1 2 3 5 3 4 5))
471 (r assign "z" "y") ;; unlike the above, this assigns character data
472 (r "ls")
473 (r ls)
475 (setf my.r.x2 (r get "x2")) ;; moving data from R to CL
476 (r assign "x2" my.r.x2) ;; moving data from CL to R
478 ;; The following is not the smartest thing to do!
479 ;;(r q)
483 ;;; How might we do statistics with Common Lisp?
484 ;;; How might we work with a data.frame?
485 ;;; What could the structures be?
486 ;;; How much hinting, and of what type, should drive the data
487 ;;; analysis?
489 (defpackage :my-data-analysis-example
490 (:documentation "Example work-package for a data analysis")
491 (:use :common-lisp :lisp-stat)
492 (:export results figures report))
494 (in-package :my-data-analysis-example)
496 (defvar my-dataset1 (read-file "data/test1.lisp"))
497 ;; or
498 (defvar my-dataset2 (read-file "data/test1.csv" :type 'csv))
500 ;;; manipulate
502 (setf my-dataset2 (set-description my-datasets2
503 :dependent-variables (list of symbols)))
504 (setf my-dataset2 (set-description my-datasets2
505 :independent-variables (list of symbols)))
507 ;; the following could be true in many cases.
508 (assert
509 (list-intersection (get-description my-datasets2 :independent-variables)
510 (get-description my-datasets2 :dependent-variables)))
512 ;; but we could phrase better,i.e.
514 (get-description
515 my-datasets2
516 :predicate-list-on-variable-metadata (list (and 'independent-variables
517 'dependent-variables)))
520 ;; statistical relations re: input/output, as done above, is one
521 ;; issue, another one is getting the right approach for statistical
522 ;; typing, i.e.
523 (get-description
524 my-datasets2
525 :predicate-list-on-variable-metadata (list 'ordinal-variables))
528 ;; so we could use a set of logical ops to selection from variable
529 ;; metadata, i.e.
530 ;; and, or, not
531 ;; do we really need the simplifying extensions?
534 ;;; output to REPL
536 (report my-dataset1 :style 'five-num)
537 (report my-dataset1 :style 'univariate)
538 (report my-dataset1 :style 'bivariate)
539 (report my-dataset1 :style 'metadata)
541 ;;; to file?
543 (report my-dataset1
544 :style 'five-num
545 :format 'pdf
546 :stream (filename-as-stream "my-dataset1-5num.pdf"))
547 (report my-dataset1 :style 'univariate)
548 (report my-dataset1 :style 'bivariate)
549 (report my-dataset1 :style 'metadata)
551 ;;; so report could handle datasets... and models?
553 (report my-model :style 'formula)
554 (report my-model :style 'simulate
555 (list :parameters (:eta 5 :mu 4 :sigma (list 2 1 0.5))
556 :number-of-reps 10))
557 ;; should return a list of parameters along with range information,
558 ;; useful for auto-building the above. Note that there are 3 types
559 ;; of parameters that can be considered -- we can have values which
560 ;; define ddata, we can have values which define fixed values and some
561 ;; could be things tht we estimate.
564 (defgeneric report (object &optional style format stream)
565 (:documentation "method for reporting on data"))
567 (defmethod report ((object dataset)
568 (style report-dataset-style-type)
569 (format output-format-type)
570 ((stream *repl*) output-stream-type))
571 "dataset reporting")
574 (defmethod report ((object model)
575 (style report-model-style-type)
576 (format output-format-type)
577 ((stream *repl*) output-stream-type))
578 "model reporting")
580 (defmethod report ((object analysis-instance)
581 (style report-analysis-style-type)
582 (format output-format-type)
583 ((stream *repl*) output-stream-type))
584 "model + dataset reporting")
587 ;; parameters are just things which get filled with values, repeatedly
588 ;; with data, or by considering to need estimation.
589 (parameters my-model)
590 (parameters my-model :type 'data)
591 (parameters my-model :type 'fixed)
592 (parameters my-model :type 'estimate)
593 (parameters my-model :type '(estimate fixed))
594 (parameters my-model :list-types) ;; useful for list-based extraction
595 ;; of particular types
597 (setf my-model-data-instance
598 (compute model data :specification (list :spec 'linear-model
599 :depvar y
600 :indepvar (list x1 x2))))
601 (report my-model-data-instance)
604 ;;; So how might we use this? Probably need to consider the
605 ;;; serialization of any lisp objects generated, perhaps via some form
606 ;;; of memoization...?
607 (in-package :cl-user)
609 (my-data-analysis-example:report :type 'full)
610 (my-data-analysis-example:report :type 'summary)
611 (my-data-analysis-example:figures :type 'pdf :file "results-figs.pdf")
613 (my-data-analysis-example:report)
615 ;;; more stuff
617 (send m :display)
618 (def m (regression-model (bind-columns iron aluminum) absorbtion))
619 (send m :help)
620 (send m :help :display)
621 (send m :help :basis)
623 (send m :plot-residuals)
625 (progn
626 ;; General Lisp, there is also a need to add, remove symbols from the
627 ;; workspace/namespace. This is a fundamental skill, similar to
628 ;; stopping, which is critical.
630 ;; boundp, fboundp
631 ;; makunbound, fmakunbound
635 (progn
636 ;;; A study in array vs list access
637 (defparameter *x* (list 1 2 3))
638 (defparameter *y* #(1 2 3))
639 (defparameter *z* (list 1 (list 2 3) (list 4 5 (list 6 7)) ))
640 (length *x*)
641 (length *y*)
642 (length *z*) ; => need a means to make this 7.
643 (length (reduce #'cons *z*)) ; => not quite -- missing iterative
645 (nelts *x*)
646 (nth 1 *x*)
647 (aref *y* 1)
648 (setf (nth 1 *x*) 6)
650 (setf (aref *y* 1) 6)
654 (in-package :ls-user)
656 (progn
657 (defparameter *x* (make-vector 5 :initial-contents '((1d0 2d0 3d0 4d0 5d0))))
658 ;; estimating a mean, simple way.
659 (/ (loop for i from 0 to (- (nelts *x*) 1)
660 summing (vref *x* i))
661 (nelts *x*))
663 (defun mean (x)
664 (checktype x 'vector-like)
665 (/ (loop for i from 0 to (- (nelts *x*) 1)
666 summing (vref *x* i))
667 (nelts *x*)))
669 ;; estimating variance, Moments
670 (let ((meanx (mean *x*))
671 (n (nelts *x*)))
672 (/ (loop for i from 0 to (1- n)
673 summing (* (- (vref *x* i) meanx)
674 (- (vref *x* i) meanx)))
677 ;; estimating variance, Moments
678 (let ((meanx (mean *x*))
679 (nm1 (1- (nelts *x*))))
680 (/ (loop for i from 0 to nm1
681 summing (* (- (vref *x* i) meanx)
682 (- (vref *x* i) meanx) ))
683 nm1))
687 ;;;;;;;;;;;;;;; Data stuff
689 (progn ;; Data setup
691 ;; Making data-frames (i.e. cases (rows) by variables (columns))
692 ;; takes a bit of getting used to. For this, it is important to
693 ;; realize that we can do the following:
694 ;; #1 - consider the possibility of having a row, and transposing
695 ;; it, so the list-of-lists is: ((1 2 3 4 5)) (1 row, 5 columns)
696 ;; #2 - naturally list-of-lists: ((1)(2)(3)(4)(5)) (5 rows, 1 column)
697 ;; see src/data/listoflist.lisp for code to process this particular
698 ;; data structure.
699 (defparameter *indep-vars-1-matrix*
700 (transpose (make-matrix 1 (length iron)
701 :initial-contents
702 (list (mapcar #'(lambda (x) (coerce x 'double-float))
703 iron))))
704 "creating iron into double float, straightforward")
706 (documentation '*indep-vars-1-matrix* 'variable)
707 ;; *indep-vars-1-matrix*
709 ;; or directly:
710 (defparameter *indep-vars-1a-matrix*
711 (make-matrix (length iron) 1
712 :initial-contents
713 (mapcar #'(lambda (x) (list (coerce x 'double-float)))
714 iron)))
715 ;; *indep-vars-1a-matrix*
717 ;; and mathematically, they seem equal:
718 (m= *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => T
719 ;; but of course not completely...
720 (eql *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL
721 (eq *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL
723 ;; and verify...
724 (print *indep-vars-1-matrix*)
725 (print *indep-vars-1a-matrix*)
727 (documentation 'lisp-matrix:bind2 'function) ; by which we mean:
728 (documentation 'bind2 'function)
729 (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :column) ; 2 col
730 (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :row) ; 1 long col
732 ;; the weird way
733 (defparameter *indep-vars-2-matrix*
734 (transpose (make-matrix 2 (length iron)
735 :initial-contents
736 (list
737 (mapcar #'(lambda (x) (coerce x 'double-float))
738 iron)
739 (mapcar #'(lambda (x) (coerce x 'double-float))
740 aluminum)))))
741 ;; *indep-vars-2-matrix*
743 ;; the "right"? way
744 (defparameter *indep-vars-2-matrix*
745 (make-matrix (length iron) 2
746 :initial-contents
747 (mapcar #'(lambda (x y)
748 (list (coerce x 'double-float)
749 (coerce y 'double-float)))
750 iron aluminum)))
751 ;; *indep-vars-2-matrix*
754 ;; The below FAILS due to coercion issues; it just isn't lispy, it's R'y.
756 (defparameter *dep-var* (make-vector (length absorbtion)
757 :initial-contents (list absorbtion)))
759 ;; BUT below, this should be the right type.
760 (defparameter *dep-var*
761 (make-vector (length absorbtion)
762 :type :row
763 :initial-contents
764 (list
765 (mapcar #'(lambda (x) (coerce x 'double-float))
766 absorbtion))))
767 ;; *dep-var*
770 (defparameter *dep-var-int*
771 (make-vector (length absorbtion)
772 :type :row
773 :element-type 'integer
774 :initial-contents (list absorbtion)))
776 (typep *dep-var* 'matrix-like) ; => T
777 (typep *dep-var* 'vector-like) ; => T
779 (typep *indep-vars-1-matrix* 'matrix-like) ; => T
780 (typep *indep-vars-1-matrix* 'vector-like) ; => T
781 (typep *indep-vars-2-matrix* 'matrix-like) ; => T
782 (typep *indep-vars-2-matrix* 'vector-like) ; => F
784 iron
785 ;; following fails, need to ensure that we work on list elts, not just
786 ;; elts within a list:
788 ;; (coerce iron 'real)
790 ;; the following is a general list-conversion coercion approach -- is
791 ;; there a more efficient way?
792 ;; (coerce 1 'real)
793 ;; (mapcar #'(lambda (x) (coerce x 'double-float)) iron)
795 (princ "Data Set up"))
800 (progn ;; Data setup
802 (describe 'make-matrix)
804 (defparameter *indep-vars-2-matrix*
805 (make-matrix (length iron) 2
806 :initial-contents
807 (mapcar #'(lambda (x y)
808 (list (coerce x 'double-float)
809 (coerce y 'double-float)))
810 iron aluminum)))
813 (defparameter *dep-var*
814 (make-vector (length absorbtion)
815 :type :row
816 :initial-contents
817 (list
818 (mapcar #'(lambda (x) (coerce x 'double-float))
819 absorbtion))))
821 (make-dataframe *dep-var*)
822 (make-dataframe (transpose *dep-var*))
824 (defparameter *dep-var-int*
825 (make-vector (length absorbtion)
826 :type :row
827 :element-type 'integer
828 :initial-contents (list absorbtion)))
831 (defparameter *xv+1a*
832 (make-matrix
834 :initial-contents #2A((1d0 1d0)
835 (1d0 3d0)
836 (1d0 2d0)
837 (1d0 4d0)
838 (1d0 3d0)
839 (1d0 5d0)
840 (1d0 4d0)
841 (1d0 6d0))))
843 (defparameter *xv+1b*
844 (bind2
845 (ones 8 1)
846 (make-matrix
848 :initial-contents '((1d0)
849 (3d0)
850 (2d0)
851 (4d0)
852 (3d0)
853 (5d0)
854 (4d0)
855 (6d0)))
856 :by :column))
858 (m= *xv+1a* *xv+1b*) ; => T
860 (princ "Data Set up"))
864 ;;;; LM
866 (progn
868 (defparameter *y*
869 (make-vector
871 :type :row
872 :initial-contents '((1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0))))
875 (defparameter *xv+1*
876 (make-matrix
878 :initial-contents '((1d0 1d0)
879 (1d0 3d0)
880 (1d0 2d0)
881 (1d0 4d0)
882 (1d0 3d0)
883 (1d0 5d0)
884 (1d0 4d0)
885 (1d0 6d0))))
888 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
889 (defparameter *xtx-2* (m* (transpose *xv+1*) *xv+1*))
890 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
891 ;; 8.0d0 28.0d0
892 ;; 28.0d0 116.0d0>
894 (defparameter *xty-2* (m* (transpose *xv+1*) (transpose *y*)))
895 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
896 ;; 36.0d0
897 ;; 150.0d0>
899 (defparameter *rcond-2* 0.000001)
900 (defparameter *betahat-2* (gelsy *xtx-2* *xty-2* *rcond-2*))
901 ;; *xtx-2* => "details of complete orthogonal factorization"
902 ;; according to man page:
903 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
904 ;; -119.33147112141039d0 -29.095426104883202d0
905 ;; 0.7873402682880205d0 -1.20672274167718d0>
907 ;; *xty-2* => output becomes solution:
908 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
909 ;; -0.16666666666668312d0
910 ;; 1.333333333333337d0>
912 *betahat-2* ; which matches R, see below
914 (documentation 'gelsy 'function)
917 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
918 ;; -0.16666666666668312 1.333333333333337>
919 ;; 2)
921 ;; ## Test case in R:
922 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
923 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
924 ;; lm(y~x)
925 ;; ## => Call: lm(formula = y ~ x)
927 ;; Coefficients: (Intercept) x
928 ;; -0.1667 1.3333
930 ;; summary(lm(y~x))
931 ;; ## =>
933 ;; Call:
934 ;; lm(formula = y ~ x)
936 ;; Residuals:
937 ;; Min 1Q Median 3Q Max
938 ;; -1.833e+00 -6.667e-01 -3.886e-16 6.667e-01 1.833e+00
940 ;; Coefficients:
941 ;; Estimate Std. Error t value Pr(>|t|)
942 ;; (Intercept) -0.1667 1.1587 -0.144 0.89034
943 ;; x 1.3333 0.3043 4.382 0.00466 **
944 ;; ---
945 ;; Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
947 ;; Residual standard error: 1.291 on 6 degrees of freedom
948 ;; Multiple R-squared: 0.7619, Adjusted R-squared: 0.7222
949 ;; F-statistic: 19.2 on 1 and 6 DF, p-value: 0.004659
953 ;; which suggests one might do (modulo ensuring correct
954 ;; orientations). When this is finalized, it should migrate to
955 ;; CLS.
959 (defparameter *n* 20) ; # rows = # obsns
960 (defparameter *p* 10) ; # cols = # vars
961 (defparameter *x-temp* (rand *n* *p*))
962 (defparameter *b-temp* (rand *p* 1))
963 (defparameter *y-temp* (m* *x-temp* *b-temp*))
964 ;; so Y=Xb + \eps
965 (defparameter *rcond* (* (coerce (expt 2 -52) 'double-float)
966 (max (nrows *x-temp*) (ncols *y-temp*))))
967 (defparameter *orig-x* (copy *x-temp*))
968 (defparameter *orig-b* (copy *b-temp*))
969 (defparameter *orig-y* (copy *y-temp*))
971 (defparameter *lm-result* (lm *x-temp* *y-temp*))
972 (princ (first *lm-result*))
973 (princ (second *lm-result*))
974 (princ (third *lm-result*))
975 (v= (third *lm-result*)
976 (v- (first (first *lm-result*))
977 (first (second *lm-result*))))
982 ;; Some issues exist in the LAPACK vs. LINPACK variants, hence R
983 ;; uses LINPACK primarily, rather than LAPACK. See comments in R
984 ;; source for issues.
987 ;; Goal is to start from X, Y and then realize that if
988 ;; Y = X \beta, then, i.e. 8x1 = 8xp px1 + 8x1
989 ;; XtX \hat\beta = Xt Y
990 ;; so that we can solve the equation W \beta = Z where W and Z
991 ;; are known, to estimate \beta.
993 ;; the above is known to be numerically instable -- some processing
994 ;; of X is preferred and should be done prior. And most of the
995 ;; transformation-based work does precisely that.
997 ;; recall: Var[Y] = E[(Y - E[Y])(Y-E[Y])t]
998 ;; = E[Y Yt] - 2 \mu \mut + \mu \mut
999 ;; = E[Y Yt] - \mu \mut
1001 ;; Var Y = E[Y^2] - \mu^2
1004 ;; For initial estimates of covariance of \hat\beta:
1006 ;; \hat\beta = (Xt X)^-1 Xt Y
1007 ;; with E[ \hat\beta ]
1008 ;; = E[ (Xt X)^-1 Xt Y ]
1009 ;; = E[(Xt X)^-1 Xt (X\beta)]
1010 ;; = \beta
1012 ;; So Var[\hat\beta] = ...
1013 ;; (Xt X)
1014 ;; and this gives SE(\beta_i) = (* (sqrt (mref Var i i)) adjustment)
1017 ;; from docs:
1019 (setf *temp-result*
1020 (let ((*default-implementation* :foreign-array))
1021 (let* ((m 10)
1022 (n 10)
1023 (a (rand m n))
1024 (x (rand n 1))
1025 (b (m* a x))
1026 (rcond (* (coerce (expt 2 -52) 'double-float)
1027 (max (nrows a) (ncols a))))
1028 (orig-a (copy a))
1029 (orig-b (copy b))
1030 (orig-x (copy x)))
1031 (list x (gelsy a b rcond))
1032 ;; no applicable conversion?
1033 ;; (m- (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1))
1034 ;; (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1)) )
1035 (v- x (first (gelsy a b rcond))))))
1038 (princ *temp-result*)
1040 (setf *temp-result*
1041 (let ((*default-implementation* :lisp-array))
1042 (let* ((m 10)
1043 (n 10)
1044 (a (rand m n))
1045 (x (rand n 1))
1046 (b (m* a x))
1047 (rcond (* (coerce (expt 2 -52) 'double-float)
1048 (max (nrows a) (ncols a))))
1049 (orig-a (copy a))
1050 (orig-b (copy b))
1051 (orig-x (copy x)))
1052 (list x (gelsy a b rcond))
1053 (m- x (first (gelsy a b rcond)))
1055 (princ *temp-result*)
1058 (defparameter *xv*
1059 (make-vector
1061 :type :row ;; default, not usually needed!
1062 :initial-contents '((1d0 3d0 2d0 4d0 3d0 5d0 4d0 6d0))))
1064 (defparameter *y*
1065 (make-vector
1067 :type :row
1068 :initial-contents '((1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0))))
1070 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
1071 (defparameter *xtx-1* (m* *xv* (transpose *xv*)))
1072 (defparameter *xty-1* (m* *xv* (transpose *y*)))
1073 (defparameter *rcond-in* (* (coerce (expt 2 -52) 'double-float)
1074 (max (nrows *xtx-1*)
1075 (ncols *xty-1*))))
1077 (defparameter *betahat* (gelsy *xtx-1* *xty-1* *rcond-in*))
1079 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (1 x 1)
1080 ;; 1.293103448275862>
1081 ;; 1)
1083 ;; ## Test case in R:
1084 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
1085 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
1086 ;; lm(y~x-1)
1087 ;; ## =>
1088 ;; Call:
1089 ;; lm(formula = y ~ x - 1)
1091 ;; Coefficients:
1092 ;; x
1093 ;; 1.293
1095 (first *betahat*))
1100 (type-of #2A((1 2 3 4 5)
1101 (10 20 30 40 50)))
1103 (type-of (rand 10 20))
1105 (typep #2A((1 2 3 4 5)
1106 (10 20 30 40 50))
1107 'matrix-like)
1109 (typep (rand 10 20) 'matrix-like)
1111 (typep #2A((1 2 3 4 5)
1112 (10 20 30 40 50))
1113 'array)
1115 (typep (rand 10 20) 'array)