macro is about dataframes, not import.
[CommonLispStat.git] / ls-demo-ls1.lisp
blob04e0eb49396bca8131ca1a5a350768439392ac5a
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-08-18 08:07:35 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 CLSv1.
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 'cls :force t)
20 (asdf:oos 'asdf:load-op 'cls)
22 ;;; non-rigorous check for exports.
23 ;;; This is generally not how I expect it to be used.
25 (in-package :cl-user)
27 (lisp-stat:binomial-quant 0.95 3 0.4) ;;; 3
28 (lisp-stat:binomial-quant 0 3 0.4) ;;; 0
29 (lisp-stat:normal-rand 20) ;;; 20 floating-point numbers :-)
31 ;;;; THIS is how I expect it to be used, either with work in ls-user,
32 ;;;; or a cloned package similar to ls-user.
34 (in-package :ls-user)
36 ;;; linear algebra (matrices and vectors)
38 (in-package :cl-user) ; the following will barf, but...
39 (+ #(2 3 4) #(4 5 6))
40 (+ #2A((2 3 4) (2 3 4)) #2A ( (4 5 6) (1 1 1)))
41 (* #2A((2 3 4) (2 3 4)) #2A ( (4 5 6) (1 1 1)))
43 (in-package :ls-user) ; the next will succeed
44 (+ #(2 3 4) #(4 5 6))
45 (+ #2A((2 3 4) (2 3 4)) #2A ( (4 5 6) (1 1 1)))
46 (* #2A((2 3 4) (2 3 4)) #2A ( (4 5 6) (1 1 1)))
51 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
52 ;; (#2A((1.7888543819998317 0.0 0.0)
53 ;; (1.6770509831248424 0.11180339887498929 0.0)
54 ;; (2.23606797749979 2.23606797749979 3.332000937312528e-8))
55 ;; 5.000000000000003)
59 (defparameter my-chol-decomp-test (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5))))
60 my-chol-decomp-test
61 (nth 0 my-chol-decomp-test)
62 (nth 1 my-chol-decomp-test)
65 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
66 ;; (#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)
68 (lu-solve
69 (lu-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
70 #(2 3 4))
71 ;; #(-2.333333333333333 1.3333333333333335 0.6666666666666666)
73 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
74 ;; #2A((2.0 -0.33333333333333326 -1.3333333333333335)
75 ;; (-1.0 -0.6666666666666666 1.3333333333333333)
76 ;; (0.0 0.6666666666666666 -0.3333333333333333))
78 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
79 ;; (#2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
80 ;; (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
81 ;; (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
82 ;; #(9.699290438141343 0.8971681569301373 0.3447525123483081)
83 ;; #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
84 ;; (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
85 ;; (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
86 ;; T)
88 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
89 ;; (#2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
90 ;; (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
91 ;; (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
92 ;; #2A((-3.0 -5.333333333333334 -7.333333333333332)
93 ;; (0.0 -0.7453559924999292 -1.1925695879998877)
94 ;; (0.0 0.0 -1.3416407864998738)))
96 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
97 ;; 6.8157451e7
98 ;;; CURRENTLY FAILS!!
100 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
101 ;; (#(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
102 ;; (#(0.4999999999999998 0.4999999999999997 0.7071067811865475)
103 ;; #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
104 ;; #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
105 ;; NIL)
107 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
108 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 6)
109 ;; ((1.0 1.3 1.6 1.9 2.2 2.5)
110 ;; (1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
112 ;;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
113 (kernel-smooth #(1.0 1.2 1.3 1.8 2.1 2.5)
114 #(1.2 2.0 2.1 2.0 1.1 2.8) :xvals 5)
115 ;; ((1.0 1.375 1.75 2.125 2.5)
116 ;; (1.6603277642110226 1.9471748095239771 1.7938127405752287
117 ;; 1.5871511322219498 2.518194783156392))
119 (kernel-dens #(1.0 1.2 2.5 2.1 1.8 1.2) :xvals 5)
120 ;; ((1.0 1.375 1.75 2.125 2.5)
121 ;; (0.7224150453621405 0.5820045548233707 0.38216411702854214
122 ;; 0.4829822708587095 0.3485939156929503))
124 (fft #(1.0 1.2 2.5 2.1 1.8))
125 ;; #(#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))
127 (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))
128 ;; (#(1.0 1.2 1.2 1.8 2.1 2.5))
132 ;;;; Special functions
134 ;; Log-gamma function
136 (log-gamma 3.4) ;;1.0923280596789584
140 ;;;; Probability functions
142 ;; looking at these a bit more, perhaps a more CLOSy style is needed, i.e.
143 ;; (quantile :list-or-cons loc :type type (one of 'empirical 'normal 'cauchy, etc...))
144 ;; similar for the cdf, density, and rand.
145 ;; Probably worth figuring out how to add a new distribution
146 ;; efficiently, i.e. by keeping some kind of list.
148 ;; Normal distribution
150 (normal-quant 0.95) ;;1.6448536279366268
151 (normal-cdf 1.3) ;;0.9031995154143897
152 (normal-dens 1.3) ;;0.17136859204780736
153 (normal-rand 2) ;;(-0.40502015f0 -0.8091404f0)
155 (bivnorm-cdf 0.2 0.4 0.6) ;;0.4736873734160288
157 ;; Cauchy distribution
159 (cauchy-quant 0.95) ;;6.313751514675031
160 (cauchy-cdf 1.3) ;;0.7912855998398473
161 (cauchy-dens 1.3) ;;0.1183308127104695
162 (cauchy-rand 2) ;;(-1.06224644160405 -0.4524695943939537)
164 ;; Gamma distribution
166 (gamma-quant 0.95 4.3) ;;8.178692439291645
167 (gamma-cdf 1.3 4.3) ;;0.028895150986674906
168 (gamma-dens 1.3 4.3) ;;0.0731517686447374
169 (gamma-rand 2 4.3) ;;(2.454918912880936 4.081365384357454)
171 ;; Chi-square distribution
173 (chisq-quant 0.95 3) ;;7.814727903379012
174 (chisq-cdf 1 5) ;;0.03743422675631789
175 (chisq-dens 1 5) ;;0.08065690818083521
176 (chisq-rand 2 4) ;;(1.968535826180572 2.9988646156942997)
178 ;; Beta distribution
180 (beta-quant 0.95 3 2) ;;0.9023885371149876
181 (beta-cdf 0.4 2 2.4) ;;0.4247997418541529
182 (beta-dens 0.4 2 2.4) ;;1.5964741858913518
183 (beta-rand 2 2 2.4) ;;(0.8014897077282279 0.6516371997922659)
185 ;; t distribution
187 (t-quant 0.95 3) ;;2.35336343484194
188 (t-cdf 1 2.3) ;;0.794733624298342
189 (t-dens 1 2.3) ;;0.1978163816318102
190 (t-rand 2 2.3) ;;(-0.34303672776089306 -1.142505872436518)
192 ;; F distribution
194 (f-quant 0.95 3 5) ;;5.409451318117459
195 (f-cdf 1 3.2 5.4) ;;0.5347130905510765
196 (f-dens 1 3.2 5.4) ;;0.37551128864591415
197 (f-rand 2 3 2) ;;(0.7939093442091963 0.07442694152491144)
199 ;; Poisson distribution
201 (poisson-quant 0.95 3.2) ;;6
202 (poisson-cdf 1 3.2) ;;0.17120125672252395
203 (poisson-pmf 1 3.2) ;;0.13043905274097067
204 (poisson-rand 5 3.2) ;;(2 1 2 0 3)
206 ;; Binomial distribution
208 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
209 (binomial-quant 0 3 0.4) ;;; -2147483648
210 (binomial-cdf 1 3 0.4) ;;0.6479999999965776
211 (binomial-pmf 1 3 0.4) ;;0.4320000000226171
212 (binomial-rand 5 3 0.4) ;;(2 2 0 1 2)
214 ;;;; OBJECT SYSTEM
216 (in-package :ls-user)
217 (defproto *test-proto*)
218 *test-proto*
219 (defmeth *test-proto* :make-data (&rest args) nil)
221 (defparameter my-proto-instance nil)
222 (setf my-proto-instance (send *test-proto* :new))
223 (send *test-proto* :own-slots)
224 (lsos::ls-object-slots *test-proto*)
225 (lsos::ls-object-methods *test-proto*)
226 (lsos::ls-object-parents *test-proto*)
227 (lsos::ls-object-preclist *test-proto*)
228 ;;; The following fail and I do not know why?
229 (send *test-proto* :has-slot 'proto-name)
230 (send *test-proto* :has-slot 'PROTO-NAME)
231 (send *test-proto* :has-slot 'make-data)
232 (send *test-proto* :has-slot 'MAKE-DATA)
233 (send *test-proto* :has-method 'make-data)
234 (send *test-proto* :has-method 'MAKE-DATA)
237 (defproto2 *test-proto3* (list) (list) (list) "test doc" t)
238 (defproto2 *test-proto4*)
239 *test-proto2*
240 (defmeth *test-proto* :make-data (&rest args) nil)
242 (defparameter my-proto-instance nil)
243 (setf my-proto-instance (send *test-proto* :new))
244 (send *test-proto* :own-slots)
245 (send *test-proto* :has-slot 'proto-name)
246 (send *test-proto* :has-slot 'PROTO-NAME)
249 ;;;; Testing
251 (in-package :lisp-stat-unittests)
252 (testsuites)
253 (print-tests)
254 (run-tests)
255 (last-test-status)
256 ;;(failures)
258 (describe (run-tests :suite 'lisp-stat-ut-testsupport))
259 (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
261 (testsuite-tests 'lisp-stat-ut)
262 (run-tests :suite 'lisp-stat-ut)
263 (describe (run-tests :suite 'lisp-stat-ut))
265 (run-tests :suite 'lisp-stat-ut-probdistn)
266 (describe (run-tests :suite 'lisp-stat-ut-probdistn))
267 (run-tests :suite 'lisp-stat-ut-spec-fns)
268 (describe (run-tests :suite 'lisp-stat-ut-spec-fns))
270 (find-testsuite 'lisp-stat-ut-lin-alg)
271 (testsuite-tests 'lisp-stat-ut-lin-alg)
272 (run-tests :suite 'lisp-stat-ut-lin-alg)
273 (describe (run-tests :suite 'lisp-stat-ut-lin-alg))
275 ;;;; Data Analysis test
277 (in-package :ls-user)
279 ;; LispStat 1 approach to variables
281 (progn
282 (def iron (list 61 175 111 124 130 173 169 169 160 224 257 333 199))
283 iron
284 (def aluminum (list 13 21 24 23 64 38 33 61 39 71 112 88 54))
285 aluminum
286 (def absorbtion (list 4 18 14 18 26 26 21 30 28 36 65 62 40))
287 absorbtion
289 ;; LispStat 1 approach to data frames... (list of lists).
291 (DEF DIABETES
292 (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)
293 (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)
294 (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)
295 (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))))
298 (DEF DLABS (QUOTE ("GLUFAST" "GLUTEST" "INSTEST" "CCLASS")))
299 (format t "loaded data.~%")
300 ) ;; eval at this point.
302 ;; Simple univariate variable-specific descriptions.
303 (fivnum absorbtion)
304 (median absorbtion)
305 (sort-data absorbtion)
306 (rank absorbtion)
307 (standard-deviation absorbtion)
308 (interquartile-range absorbtion)
310 (lisp-stat-matrix::bind-columns aluminum iron)
311 (bind-columns aluminum iron)
312 (apply #'bind-columns (list aluminum iron))
313 (lisp-stat-matrix::bind-columns #2a((1 2)(3 4)) #(5 6))
314 (bind-columns #2a((1 2)(3 4)) #(5 6))
317 (defparameter fit1 nil)
318 (setf fit1 (regression-model absorbtion iron))
319 (send fit1 :display)
320 (send fit1 :residuals)
322 iron
323 (defparameter fit1a nil)
324 (setf fit1a (regression-model absorbtion iron :print nil))
325 (send fit1a :doc)
326 ;; (setf (send fit1a :doc) "this") ;; FIXME: this error...
327 (send fit1a :doc "this") ;; FIXME: this is a more natural
328 (send fit1a :doc)
329 (send fit1a :x)
330 (send fit1a :y)
331 (send fit1a :compute)
332 (send fit1a :sweep-matrix)
333 (send fit1a :basis)
334 (send fit1a :residuals)
335 (send fit1a :display)
337 #+nil(progn
338 ;; syntax example
339 (array-dimension #2A ((1)) 0)
342 ;;; FIXME: need to get multiple-linear regression working -- clearly
343 ;;; simple linear is working above!
344 (defvar m nil "holding variable.")
345 (def m (regression-model (list iron aluminum) absorbtion :print nil))
346 (send m :compute)
347 (send m :sweep-matrix)
348 (format t "~%~A~%" (send m :sweep-matrix))
350 ;; ERROR... FIX-ME!!
351 (send m :basis) ;; this should be positive?
352 (send m :coef-estimates)
354 (send m :display)
355 (def m (regression-model (bind-columns iron aluminum) absorbtion))
356 (send m :help)
357 (send m :help :display)
358 (send m :help :basis)
359 ;; No graphics! But handle the error gracefully...
360 (send m :plot-residuals)
363 (typep aluminum 'sequence)
364 (typep iron 'sequence)
365 (matrixp iron)
367 *variables*
369 (variables)
370 (undef 'iron)
371 (variables)
373 ;;; Plotting!
375 (asdf:oos 'asdf:compile-op 'cl-cairo2 :force t)
376 (asdf:oos 'asdf:load-op 'cl-cairo2)
378 ;; This can be used to generate PDF, PS, PNG, and X11/Microsoft
379 ;; displays (the latter being a proof of concept, of limited use for
380 ;; "real work".
382 ;;; Using R!
384 (asdf:oos 'asdf:compile-op 'rclg :force t)
385 (asdf:oos 'asdf:load-op 'rclg)
388 (in-package :rclg-user)
390 ;; rclg-init::*r-started*
392 ;;;#3 Start R within Lisp
394 (start-rclg)
395 ;; rclg-init::*r-started*
396 (rclg-init::check-stack)
397 (r "Cstack_info")
398 (defparameter *x* (r seq 1 11))
399 (defparameter *y* (r rnorm 10))
401 (r plot *x* *y*)
404 (defparameter *r-version* (r "version"))
406 ;; This is for illustrative purposes only. It is not a "good" use of rnbi.
407 ;; Really, you'll want rnbi to hold anonymous intermeditae results, like:
408 (r plot *x* (rnbi rnorm 10))
410 (r "Sys.getenv" "LD_LIBRARY_PATH")
411 (r "Sys.getenv" "LD_PRELOAD")
413 (r "ls")
414 (r ls)
415 (r "search")
417 (r "geterrmessage")
419 (r "library" "stats")
420 (r library "MASS")
421 (r "library" "Biobase")
423 (setf my.lib "Biobase")
424 my.lib
425 (r library my.lib)
427 (r "ls")
429 (r "print.default" 3)
430 (r "rnorm" 10)
432 ;; Working in the R space
434 (r assign "x" 5)
435 (r assign "x2" (list 1 2 3 5))
437 (r assign "x2" #(1 2 3 5 3 4 5))
438 (r assign "z" "y") ;; unlike the above, this assigns character data
439 (r "ls")
440 (r ls)
442 (setf my.r.x2 (r get "x2")) ;; moving data from R to CL
443 (r assign "x2" my.r.x2) ;; moving data from CL to R
445 ;; The following is not the smartest thing to do!
446 ;;(r q)
450 ;;; How might we do statistics with Common Lisp?
451 ;;; How might we work with a data.frame?
452 ;;; What could the structures be?
453 ;;; How much hinting, and of what type, should drive the data
454 ;;; analysis?
456 (defpackage :my-data-analysis-example
457 (:documentation "Example work-package for a data analysis")
458 (:use :common-lisp :lisp-stat)
459 (:export results figures report))
461 (in-package :my-data-analysis-example)
463 (defvar my-dataset1 (read-file "data/test1.lisp"))
464 ;; or
465 (defvar my-dataset2 (read-file "data/test1.csv" :type 'csv))
467 ;;; manipulate
469 (setf my-dataset2 (set-description my-datasets2
470 :dependent-variables (list of symbols)))
471 (setf my-dataset2 (set-description my-datasets2
472 :independent-variables (list of symbols)))
474 ;; the following could be true in many cases.
475 (assert
476 (list-intersection (get-description my-datasets2 :independent-variables)
477 (get-description my-datasets2 :dependent-variables)))
479 ;; but we could phrase better,i.e.
481 (get-description
482 my-datasets2
483 :predicate-list-on-variable-metadata (list (and 'independent-variables
484 'dependent-variables)))
487 ;; statistical relations re: input/output, as done above, is one
488 ;; issue, another one is getting the right approach for statistical
489 ;; typing, i.e.
490 (get-description
491 my-datasets2
492 :predicate-list-on-variable-metadata (list 'ordinal-variables))
495 ;; so we could use a set of logical ops to selection from variable
496 ;; metadata, i.e.
497 ;; and, or, not
498 ;; do we really need the simplifying extensions?
501 ;;; output to REPL
503 (report my-dataset1 :style 'five-num)
504 (report my-dataset1 :style 'univariate)
505 (report my-dataset1 :style 'bivariate)
506 (report my-dataset1 :style 'metadata)
508 ;;; to file?
510 (report my-dataset1
511 :style 'five-num
512 :format 'pdf
513 :stream (filename-as-stream "my-dataset1-5num.pdf"))
514 (report my-dataset1 :style 'univariate)
515 (report my-dataset1 :style 'bivariate)
516 (report my-dataset1 :style 'metadata)
518 ;;; so report could handle datasets... and models?
520 (report my-model :style 'formula)
521 (report my-model :style 'simulate
522 (list :parameters (:eta 5 :mu 4 :sigma (list 2 1 0.5))
523 :number-of-reps 10))
524 ;; should return a list of parameters along with range information,
525 ;; useful for auto-building the above. Note that there are 3 types
526 ;; of parameters that can be considered -- we can have values which
527 ;; define ddata, we can have values which define fixed values and some
528 ;; could be things tht we estimate.
531 (defgeneric report (object &optional style format stream)
532 (:documentation "method for reporting on data"))
534 (defmethod report ((object dataset)
535 (style report-dataset-style-type)
536 (format output-format-type)
537 ((stream *repl*) output-stream-type))
538 "dataset reporting")
541 (defmethod report ((object model)
542 (style report-model-style-type)
543 (format output-format-type)
544 ((stream *repl*) output-stream-type))
545 "model reporting")
547 (defmethod report ((object analysis-instance)
548 (style report-analysis-style-type)
549 (format output-format-type)
550 ((stream *repl*) output-stream-type))
551 "model + dataset reporting")
554 ;; parameters are just things which get filled with values, repeatedly
555 ;; with data, or by considering to need estimation.
556 (parameters my-model)
557 (parameters my-model :type 'data)
558 (parameters my-model :type 'fixed)
559 (parameters my-model :type 'estimate)
560 (parameters my-model :type '(estimate fixed))
561 (parameters my-model :list-types) ;; useful for list-based extraction
562 ;; of particular types
564 (setf my-model-data-instance
565 (compute model data :specification (list :spec 'linear-model
566 :depvar y
567 :indepvar (list x1 x2))))
568 (report my-model-data-instance)
571 ;;; So how might we use this? Probably need to consider the
572 ;;; serialization of any lisp objects generated, perhaps via some form
573 ;;; of memoization...?
574 (in-package :cl-user)
576 (my-data-analysis-example:report :type 'full)
577 (my-data-analysis-example:report :type 'summary)
578 (my-data-analysis-example:figures :type 'pdf :file "results-figs.pdf")
580 (my-data-analysis-example:report)
582 ;;; more stuff
584 (send m :display)
585 (def m (regression-model (bind-columns iron aluminum) absorbtion))
586 (send m :help)
587 (send m :help :display)
588 (send m :help :basis)
590 (send m :plot-residuals)
592 (progn
593 ;; General Lisp, there is also a need to add, remove symbols from the
594 ;; workspace/namespace. This is a fundamental skill, similar to
595 ;; stopping, which is critical.
597 ;; boundp, fboundp
598 ;; makunbound, fmakunbound
602 (progn
603 ;;; A study in array vs list access
604 (defparameter *x* (list 1 2 3))
605 (defparameter *y* #(1 2 3))
606 (defparameter *z* (list 1 (list 2 3) (list 4 5 (list 6 7)) ))
607 (length *x*)
608 (length *y*)
609 (length *z*) ; => need a means to make this 7.
610 (length (reduce #'cons *z*)) ; => not quite -- missing iterative
612 (nelts *x*)
613 (nth 1 *x*)
614 (aref *y* 1)
615 (setf (nth 1 *x*) 6)
617 (setf (aref *y* 1) 6)
621 (in-package :ls-user)
623 (progn
624 (defparameter *x* (make-vector 5 :initial-contents '((1d0 2d0 3d0 4d0 5d0))))
625 ;; estimating a mean, simple way.
626 (/ (loop for i from 0 to (- (nelts *x*) 1)
627 summing (vref *x* i))
628 (nelts *x*))
630 (defun mean (x)
631 (checktype x 'vector-like)
632 (/ (loop for i from 0 to (- (nelts *x*) 1)
633 summing (vref *x* i))
634 (nelts *x*)))
636 ;; estimating variance, Moments
637 (let ((meanx (mean *x*))
638 (n (nelts *x*)))
639 (/ (loop for i from 0 to (1- n)
640 summing (* (- (vref *x* i) meanx)
641 (- (vref *x* i) meanx)))
644 ;; estimating variance, Moments
645 (let ((meanx (mean *x*))
646 (nm1 (1- (nelts *x*))))
647 (/ (loop for i from 0 to nm1
648 summing (* (- (vref *x* i) meanx)
649 (- (vref *x* i) meanx) ))
650 nm1))
654 ;;;;;;;;;;;;;;; Data stuff
656 (progn ;; Data setup
658 ;; Making data-frames (i.e. cases (rows) by variables (columns))
659 ;; takes a bit of getting used to. For this, it is important to
660 ;; realize that we can do the following:
661 ;; #1 - consider the possibility of having a row, and transposing
662 ;; it, so the list-of-lists is: ((1 2 3 4 5)) (1 row, 5 columns)
663 ;; #2 - naturally list-of-lists: ((1)(2)(3)(4)(5)) (5 rows, 1 column)
664 ;; see src/data/listoflist.lisp for code to process this particular
665 ;; data structure.
666 (defparameter *indep-vars-1-matrix*
667 (transpose (make-matrix 1 (length iron)
668 :initial-contents
669 (list (mapcar #'(lambda (x) (coerce x 'double-float))
670 iron))))
671 "creating iron into double float, straightforward")
673 (documentation '*indep-vars-1-matrix* 'variable)
674 ;; *indep-vars-1-matrix*
676 ;; or directly:
677 (defparameter *indep-vars-1a-matrix*
678 (make-matrix (length iron) 1
679 :initial-contents
680 (mapcar #'(lambda (x) (list (coerce x 'double-float)))
681 iron)))
682 ;; *indep-vars-1a-matrix*
684 ;; and mathematically, they seem equal:
685 (m= *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => T
686 ;; but of course not completely...
687 (eql *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL
688 (eq *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL
690 ;; and verify...
691 (print *indep-vars-1-matrix*)
692 (print *indep-vars-1a-matrix*)
694 (documentation 'lisp-matrix:bind2 'function) ; by which we mean:
695 (documentation 'bind2 'function)
696 (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :column) ; 2 col
697 (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :row) ; 1 long col
699 ;; the weird way
700 (defparameter *indep-vars-2-matrix*
701 (transpose (make-matrix 2 (length iron)
702 :initial-contents
703 (list
704 (mapcar #'(lambda (x) (coerce x 'double-float))
705 iron)
706 (mapcar #'(lambda (x) (coerce x 'double-float))
707 aluminum)))))
708 ;; *indep-vars-2-matrix*
710 ;; the "right"? way
711 (defparameter *indep-vars-2-matrix*
712 (make-matrix (length iron) 2
713 :initial-contents
714 (mapcar #'(lambda (x y)
715 (list (coerce x 'double-float)
716 (coerce y 'double-float)))
717 iron aluminum)))
718 ;; *indep-vars-2-matrix*
721 ;; The below FAILS due to coercion issues; it just isn't lispy, it's R'y.
723 (defparameter *dep-var* (make-vector (length absorbtion)
724 :initial-contents (list absorbtion)))
726 ;; BUT below, this should be the right type.
727 (defparameter *dep-var*
728 (make-vector (length absorbtion)
729 :type :row
730 :initial-contents
731 (list
732 (mapcar #'(lambda (x) (coerce x 'double-float))
733 absorbtion))))
734 ;; *dep-var*
737 (defparameter *dep-var-int*
738 (make-vector (length absorbtion)
739 :type :row
740 :element-type 'integer
741 :initial-contents (list absorbtion)))
743 (typep *dep-var* 'matrix-like) ; => T
744 (typep *dep-var* 'vector-like) ; => T
746 (typep *indep-vars-1-matrix* 'matrix-like) ; => T
747 (typep *indep-vars-1-matrix* 'vector-like) ; => T
748 (typep *indep-vars-2-matrix* 'matrix-like) ; => T
749 (typep *indep-vars-2-matrix* 'vector-like) ; => F
751 iron
752 ;; following fails, need to ensure that we work on list elts, not just
753 ;; elts within a list:
755 ;; (coerce iron 'real)
757 ;; the following is a general list-conversion coercion approach -- is
758 ;; there a more efficient way?
759 ;; (coerce 1 'real)
760 ;; (mapcar #'(lambda (x) (coerce x 'double-float)) iron)
762 (princ "Data Set up"))
766 (defparameter *my-df-1*
767 (make-instance 'dataframe-array
768 :storage #2A((1 2 3 4 5)
769 (10 20 30 40 50))
770 :doc "This is an interesting dataframe-array"
771 :case-labels (list "x" "y")
772 :var-labels (list "a" "b" "c" "d" "e")))
774 (setf (xref *my-df-1* 0 0) -1d0)
778 (make-dataframe #2A((1 2 3 4 5)
779 (10 20 30 40 50)))
781 (make-dataframe (rand 4 3))
785 (defparameter *my-df-1*
786 (make-dataframe #2A((1 2 3 4 5)
787 (10 20 30 40 50))
788 :caselabels (list "x" "y")
789 :varlabels (list "a" "b" "c" "d" "e")
790 :doc "This is an interesting dataframe-array"))
792 (caselabels *my-df-1*)
793 (varlabels *my-df-1*)
797 (progn ;; Data setup
799 (describe 'make-matrix)
801 (defparameter *indep-vars-2-matrix*
802 (make-matrix (length iron) 2
803 :initial-contents
804 (mapcar #'(lambda (x y)
805 (list (coerce x 'double-float)
806 (coerce y 'double-float)))
807 iron aluminum)))
810 (defparameter *dep-var*
811 (make-vector (length absorbtion)
812 :type :row
813 :initial-contents
814 (list
815 (mapcar #'(lambda (x) (coerce x 'double-float))
816 absorbtion))))
818 (make-dataframe *dep-var*)
819 (make-dataframe (transpose *dep-var*))
821 (defparameter *dep-var-int*
822 (make-vector (length absorbtion)
823 :type :row
824 :element-type 'integer
825 :initial-contents (list absorbtion)))
828 (defparameter *xv+1a*
829 (make-matrix
831 :initial-contents #2A((1d0 1d0)
832 (1d0 3d0)
833 (1d0 2d0)
834 (1d0 4d0)
835 (1d0 3d0)
836 (1d0 5d0)
837 (1d0 4d0)
838 (1d0 6d0))))
840 (defparameter *xv+1b*
841 (bind2
842 (ones 8 1)
843 (make-matrix
845 :initial-contents '((1d0)
846 (3d0)
847 (2d0)
848 (4d0)
849 (3d0)
850 (5d0)
851 (4d0)
852 (6d0)))
853 :by :column))
855 (m= *xv+1a* *xv+1b*) ; => T
857 (princ "Data Set up"))
861 ;;;; LM
863 (progn
865 (defparameter *y*
866 (make-vector
868 :type :row
869 :initial-contents '((1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0))))
872 (defparameter *xv+1*
873 (make-matrix
875 :initial-contents '((1d0 1d0)
876 (1d0 3d0)
877 (1d0 2d0)
878 (1d0 4d0)
879 (1d0 3d0)
880 (1d0 5d0)
881 (1d0 4d0)
882 (1d0 6d0))))
885 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
886 (defparameter *xtx-2* (m* (transpose *xv+1*) *xv+1*))
887 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
888 ;; 8.0d0 28.0d0
889 ;; 28.0d0 116.0d0>
891 (defparameter *xty-2* (m* (transpose *xv+1*) (transpose *y*)))
892 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
893 ;; 36.0d0
894 ;; 150.0d0>
896 (defparameter *rcond-2* 0.000001)
897 (defparameter *betahat-2* (gelsy *xtx-2* *xty-2* *rcond-2*))
898 ;; *xtx-2* => "details of complete orthogonal factorization"
899 ;; according to man page:
900 ;; #<LA-SIMPLE-MATRIX-DOUBLE 2 x 2
901 ;; -119.33147112141039d0 -29.095426104883202d0
902 ;; 0.7873402682880205d0 -1.20672274167718d0>
904 ;; *xty-2* => output becomes solution:
905 ;; #<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
906 ;; -0.16666666666668312d0
907 ;; 1.333333333333337d0>
909 *betahat-2* ; which matches R, see below
911 (documentation 'gelsy 'function)
914 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (2 x 1)
915 ;; -0.16666666666668312 1.333333333333337>
916 ;; 2)
918 ;; ## Test case in R:
919 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
920 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
921 ;; lm(y~x)
922 ;; ## => Call: lm(formula = y ~ x)
924 ;; Coefficients: (Intercept) x
925 ;; -0.1667 1.3333
927 ;; summary(lm(y~x))
928 ;; ## =>
930 ;; Call:
931 ;; lm(formula = y ~ x)
933 ;; Residuals:
934 ;; Min 1Q Median 3Q Max
935 ;; -1.833e+00 -6.667e-01 -3.886e-16 6.667e-01 1.833e+00
937 ;; Coefficients:
938 ;; Estimate Std. Error t value Pr(>|t|)
939 ;; (Intercept) -0.1667 1.1587 -0.144 0.89034
940 ;; x 1.3333 0.3043 4.382 0.00466 **
941 ;; ---
942 ;; Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
944 ;; Residual standard error: 1.291 on 6 degrees of freedom
945 ;; Multiple R-squared: 0.7619, Adjusted R-squared: 0.7222
946 ;; F-statistic: 19.2 on 1 and 6 DF, p-value: 0.004659
950 ;; which suggests one might do (modulo ensuring correct
951 ;; orientations). When this is finalized, it should migrate to
952 ;; CLS.
956 (defparameter *n* 20) ; # rows = # obsns
957 (defparameter *p* 10) ; # cols = # vars
958 (defparameter *x-temp* (rand *n* *p*))
959 (defparameter *b-temp* (rand *p* 1))
960 (defparameter *y-temp* (m* *x-temp* *b-temp*))
961 ;; so Y=Xb + \eps
962 (defparameter *rcond* (* (coerce (expt 2 -52) 'double-float)
963 (max (nrows *x-temp*) (ncols *y-temp*))))
964 (defparameter *orig-x* (copy *x-temp*))
965 (defparameter *orig-b* (copy *b-temp*))
966 (defparameter *orig-y* (copy *y-temp*))
968 (defparameter *lm-result* (lm *x-temp* *y-temp*))
969 (princ (first *lm-result*))
970 (princ (second *lm-result*))
971 (princ (third *lm-result*))
972 (v= (third *lm-result*)
973 (v- (first (first *lm-result*))
974 (first (second *lm-result*))))
979 ;; Some issues exist in the LAPACK vs. LINPACK variants, hence R
980 ;; uses LINPACK primarily, rather than LAPACK. See comments in R
981 ;; source for issues.
984 ;; Goal is to start from X, Y and then realize that if
985 ;; Y = X \beta, then, i.e. 8x1 = 8xp px1 + 8x1
986 ;; XtX \hat\beta = Xt Y
987 ;; so that we can solve the equation W \beta = Z where W and Z
988 ;; are known, to estimate \beta.
990 ;; the above is known to be numerically instable -- some processing
991 ;; of X is preferred and should be done prior. And most of the
992 ;; transformation-based work does precisely that.
994 ;; recall: Var[Y] = E[(Y - E[Y])(Y-E[Y])t]
995 ;; = E[Y Yt] - 2 \mu \mut + \mu \mut
996 ;; = E[Y Yt] - \mu \mut
998 ;; Var Y = E[Y^2] - \mu^2
1001 ;; For initial estimates of covariance of \hat\beta:
1003 ;; \hat\beta = (Xt X)^-1 Xt Y
1004 ;; with E[ \hat\beta ]
1005 ;; = E[ (Xt X)^-1 Xt Y ]
1006 ;; = E[(Xt X)^-1 Xt (X\beta)]
1007 ;; = \beta
1009 ;; So Var[\hat\beta] = ...
1010 ;; (Xt X)
1011 ;; and this gives SE(\beta_i) = (* (sqrt (mref Var i i)) adjustment)
1014 ;; from docs:
1016 (setf *temp-result*
1017 (let ((*default-implementation* :foreign-array))
1018 (let* ((m 10)
1019 (n 10)
1020 (a (rand m n))
1021 (x (rand n 1))
1022 (b (m* a x))
1023 (rcond (* (coerce (expt 2 -52) 'double-float)
1024 (max (nrows a) (ncols a))))
1025 (orig-a (copy a))
1026 (orig-b (copy b))
1027 (orig-x (copy x)))
1028 (list x (gelsy a b rcond))
1029 ;; no applicable conversion?
1030 ;; (m- (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1))
1031 ;; (#<FA-SIMPLE-VECTOR-DOUBLE (10 x 1)) )
1032 (v- x (first (gelsy a b rcond))))))
1035 (princ *temp-result*)
1037 (setf *temp-result*
1038 (let ((*default-implementation* :lisp-array))
1039 (let* ((m 10)
1040 (n 10)
1041 (a (rand m n))
1042 (x (rand n 1))
1043 (b (m* a x))
1044 (rcond (* (coerce (expt 2 -52) 'double-float)
1045 (max (nrows a) (ncols a))))
1046 (orig-a (copy a))
1047 (orig-b (copy b))
1048 (orig-x (copy x)))
1049 (list x (gelsy a b rcond))
1050 (m- x (first (gelsy a b rcond)))
1052 (princ *temp-result*)
1055 (defparameter *xv*
1056 (make-vector
1058 :type :row ;; default, not usually needed!
1059 :initial-contents '((1d0 3d0 2d0 4d0 3d0 5d0 4d0 6d0))))
1061 (defparameter *y*
1062 (make-vector
1064 :type :row
1065 :initial-contents '((1d0 2d0 3d0 4d0 5d0 6d0 7d0 8d0))))
1067 ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety)
1068 (defparameter *xtx-1* (m* *xv* (transpose *xv*)))
1069 (defparameter *xty-1* (m* *xv* (transpose *y*)))
1070 (defparameter *rcond-in* (* (coerce (expt 2 -52) 'double-float)
1071 (max (nrows *xtx-1*)
1072 (ncols *xty-1*))))
1074 (defparameter *betahat* (gelsy *xtx-1* *xty-1* *rcond-in*))
1076 ;; (#<LA-SIMPLE-VECTOR-DOUBLE (1 x 1)
1077 ;; 1.293103448275862>
1078 ;; 1)
1080 ;; ## Test case in R:
1081 ;; x <- c( 1.0, 3.0, 2.0, 4.0, 3.0, 5.0, 4.0, 6.0)
1082 ;; y <- c( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)
1083 ;; lm(y~x-1)
1084 ;; ## =>
1085 ;; Call:
1086 ;; lm(formula = y ~ x - 1)
1088 ;; Coefficients:
1089 ;; x
1090 ;; 1.293
1092 (first *betahat*))
1097 (type-of #2A((1 2 3 4 5)
1098 (10 20 30 40 50)))
1100 (type-of (rand 10 20))
1102 (typep #2A((1 2 3 4 5)
1103 (10 20 30 40 50))
1104 'matrix-like)
1106 (typep (rand 10 20) 'matrix-like)
1108 (typep #2A((1 2 3 4 5)
1109 (10 20 30 40 50))
1110 'array)
1112 (typep (rand 10 20) 'array)