From 8dd1aab1ba86f60e658ee7c78110e3469ef45939 Mon Sep 17 00:00:00 2001 From: AJ Rossini Date: Mon, 6 Apr 2009 17:38:35 +0200 Subject: [PATCH] set up to work on regression modeling. Regression models work, BUT WRONG. Needs to correct, now that it executes. Signed-off-by: AJ Rossini --- TODO.lisp | 116 ++++++++++++++++++---------------------- src/stat-models/regression.lisp | 30 +++++++---- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/TODO.lisp b/TODO.lisp index 027c517..32b0718 100644 --- a/TODO.lisp +++ b/TODO.lisp @@ -1,19 +1,19 @@ ;;; -*- mode: lisp -*- -;;; Time-stamp: <2009-04-02 16:22:00 tony> +;;; Time-stamp: <2009-04-03 17:37:08 tony> ;;; Creation: <2008-09-08 08:06:30 tony> ;;; File: TODO.lisp ;;; Author: AJ Rossini ;;; Copyright: (c) 2007-2008, AJ Rossini . BSD. -;;; Purpose: Stuff that needs to be made working sits inside the progns... - +;;; Purpose: Stuff that needs to be made working sits inside the +;;; progns... This file contains the current challenges to +;;; solve, including a description of the setup and the work +;;; to solve.... + ;;; What is this talk of 'release'? Klingons do not make software ;;; 'releases'. Our software 'escapes', leaving a bloody trail of ;;; designers and quality assurance people in its wake. -;;; This file contains the current challenges to solve, including a -;;; description of the setup and the work to solve.... - ;;; SET UP (in-package :cl-user) @@ -23,21 +23,59 @@ (in-package :lisp-stat-unittests) ;; tests = 79, failures = 7, errors = 15 - -(describe (run-tests :suite 'lisp-stat-ut)) (run-tests :suite 'lisp-stat-ut) +(describe (run-tests :suite 'lisp-stat-ut)) -#| - ;; FIXME: Example: currently not relevant, yet - (describe - (lift::run-test - :test-case 'lisp-stat-unittests::create-proto - :suite 'lisp-stat-unittests::lisp-stat-ut-proto)) -|# +;; FIXME: Example: currently not relevant, yet +;; (describe (lift::run-test :test-case 'lisp-stat-unittests::create-proto +;; :suite 'lisp-stat-unittests::lisp-stat-ut-proto)) (describe 'lisp-stat-ut) + (in-package :ls-user) +(progn ;; FIXME: Regression modeling (some data future-ish) + + (defparameter *m* nil "holding variable.") + (def *m* (regression-model (list->vector-like iron) ;; BROKEN + (list->vector-like absorbtion))) + + (def m (regression-model (list->vector-like iron) + (list->vector-like absorbtion) :print nil)) + ;;Good + (send m :print) + (send m :own-slots) + (send m :own-methods) + ;; (lsos::ls-objects-methods m) ; bogus? + (send m :show) + + (def m (regression-model (list->vector-like iron) + (list->vector-like absorbtion))) + + (def m (regression-model (listoflists->matrix-like (list iron aluminum)) + (list->vector-like absorbtion) :print nil)) + + (send m :compute) + (send m :sweep-matrix) + (format t "~%~A~%" (send m :sweep-matrix)) + + ;; need to get multiple-linear regression working (simple linear regr + ;; works)... to do this, we need to redo the whole numeric structure, + ;; I'm keeping these in as example of brokenness... + + (send m :basis) ;; this should be positive? + (send m :coef-estimates) ) + +#+nil +(progn ;; FIXME: Need to clean up data examples, licenses, attributions, etc. + ;; The following breaks because we should use a package to hold + ;; configuration details, and this would be the only package outside + ;; of packages.lisp, as it holds the overall defsystem structure. + (load-data "iris.lsp") ;; (the above partially fixed). + (variables) + diabetes ) + + (progn ;; dataframe (describe (lift::run-tests :suite 'lisp-stat-ut-dataframe)) @@ -208,54 +246,6 @@ ) -#+nil -(progn ;; FIXME: Regression modeling - - ;; data setup in previous FIXME - (defparameter *m* nil - "holding variable.") - ;; need to make vectors and matrices from the lists... - - ;; BROKEN - (def *m* (regression-model (list->vector-like iron) - (list->vector-like absorbtion))) - - (def m (regression-model (list->vector-like iron) - (list->vector-like absorbtion) :print nil)) - ;;Good - (send m :print) - (send m :own-slots) - (send m :own-methods) - ;; (lsos::ls-objects-methods m) ; bogus? - (send m :show) - - (def m (regression-model (list->vector-like iron) - (list->vector-like absorbtion))) - - (def m (regression-model (listoflists->matrix-like (list iron aluminum)) - (list->vector-like absorbtion) :print nil)) - - - (send m :compute) - (send m :sweep-matrix) - (format t "~%~A~%" (send m :sweep-matrix)) - - ;; need to get multiple-linear regression working (simple linear regr - ;; works)... to do this, we need to redo the whole numeric structure, - ;; I'm keeping these in as example of brokenness... - - (send m :basis) ;; this should be positive? - (send m :coef-estimates) ) - -#+nil -(progn ;; FIXME: Need to clean up data examples, licenses, attributions, etc. - ;; The following breaks because we should use a package to hold - ;; configuration details, and this would be the only package outside - ;; of packages.lisp, as it holds the overall defsystem structure. - (load-data "iris.lsp") ;; (the above partially fixed). - (variables) - diabetes ) - diff --git a/src/stat-models/regression.lisp b/src/stat-models/regression.lisp index eb453ef..4ea3e37 100644 --- a/src/stat-models/regression.lisp +++ b/src/stat-models/regression.lisp @@ -330,11 +330,12 @@ Recomputes the estimates. For internal use by other messages" ;; (* 0.001 (reduce #'* (mapcar #'standard-deviation (list-of-columns x)))) )) (format t - "~%REMVME: regr-mdl-prto :compute~%x= ~A~%y= ~A~% tss= ~A~% tol= ~A~% w= ~A~% n= ~A~% res= ~A~%" - x y tss tol w n p res) + "~%REMVME: regr-pr :compute~%x= ~A~%y= ~A~% tss= ~A~% tol= ~A~% w= ~A~% n= ~A~% res= ~A p=~A ~% " + x y tss tol w n res p) - ;; (send self :beta-coefficents (lm x y)) ;; FIXME! - ;; (send self :xtxinv (xtxinv x)) ;; not settable? + (setf (proto-slot-value 'betahat) + (first (lm (send self :x) + (send self :y)))) ;; FIXME! (setf (proto-slot-value 'total-sum-of-squares) tss) (setf (proto-slot-value 'residual-sum-of-squares) @@ -352,10 +353,13 @@ update the model fits." (null (proto-slot-value 'betahat))) (defmeth regression-model-proto :display () -"Message args: () - + "Message args: () Prints the least squares regression summary. Variables not used in the fit are marked as aliased." + (send self :x) + (format nil "Computing Regression Proto :display")) + +#| (let ((coefs (vector-like->list (send self :coef-estimates))) (se-s (send self :coef-standard-errors)) (x (send self :x)) @@ -366,7 +370,7 @@ are marked as aliased." (when (send self :intercept) (format t "Constant ~10f ~A~%" (car coefs) (list (car se-s))) - (setf coefs (cdr coefs)) + (setf coefs (cdr coefs)) (setf se-s (cdr se-s))) (dotimes (i (array-dimension x 1)) (cond @@ -383,6 +387,7 @@ are marked as aliased." (format t "Number of cases used: ~10d~%" (send self :num-included))) (format t "Degrees of freedom: ~10d~%" (send self :df)) (format t "~%"))) +|# ;;; Slot accessors and mutators @@ -649,14 +654,19 @@ basis." (defmeth regression-model-proto :xtxinv () "Message args: () Returns ((X^T) X)^(-1) or ((X^T) W X)^(-1)." - (xtxinv (send self x))) + (xtxinv (send self :x))) (defmeth regression-model-proto :coef-standard-errors () "Message args: () Returns estimated standard errors of coefficients. Entries beyond the intercept correspond to entries in basis." - (let ((s (send self :sigma-hat))) - (if s (* (send self :sigma-hat) (sqrt (diagonalf (send self :xtxinv))))))) + (let ((s (send self :sigma-hat)) + (v (map-vec #'sqrt (diagonalf (send self :xtxinv))))) + (if s + (etypecase s + (double (axpy s v (make-vector (nelts v) :type :column :initial-element 0d0))) + (vector-like (v* (send self :sigma-hat) v))) + v))) (defmeth regression-model-proto :studentized-residuals () "Message args: () -- 2.11.4.GIT