Psych 285: Computational Statistics
and Statistical Visualization

Professor Forrest Young

Lisp Programming Examples

These short examples follow Luke Tierney's text. You may copy this page and work your way through these examples without having to type them in.


  • Examples from Chapter 2
  • 
    ;
    ;-=-=-=-=-=-=-=
    ;Combine plots and regression
    ;-=-=-=-=-=-=-=
    ;
    
    ;Create a plot and send regression coefficients to it to show the regression line
    
    (setf plot (plot-points travel-space separation))
    (def bikes (regression-model travel-space separation))
    (setf coefs (send bikes :coef-estimates))
    (send plot :abline (first coefs) (second coefs))
    
    ;Do the same for quadratic regression
    
    (def bikes2 (regression-model 
                 (list travel-space (^ travel-space 2)) separation))
    (setf coefs2 (send bikes2 :coef-estimates))
    (def x (rseq 12 22 50))
    (def y (+ (first coefs2) (* (second coefs2) x) (* (third coefs2) (^ x 2))))
    (send plot :add-lines x y)
    
    ;Look at the residuals of both
    
    (send bikes :plot-residuals)
    (send bikes2 :plot-residuals)
    
    ;
    ;-=-=-=-=-=-=-=
    ;Dynamic histogram controlled by a slider
    ;-=-=-=-=-=-=-=
    ;
    
    (def h (histogram (normal-rand 20)))
    
    (defmeth h :new-sample ()
      (send self :clear :draw nil)
      (send self :add-points (normal-rand 20)))
    
    (dotimes (i 50)
             (send h :new-sample))
    
    (defmeth h :new-sample ()
      (send self :add-points (normal-rand 20)))
    
    (dotimes (i 50)
             (send h :new-sample))
    
    (send h :clear)
    
    ;-=-=-=-=-=-=-=
    
    (defun bc (x p)
      (let* ((bcx (if (< (abs p) .0001)
                      (log x)
                      (/ (^ x p) p)))
             (min (min bcx))
             (max (max bcx))
             )
        (/ (- bcx min) (- max min))))
    
    (def precip (sort-data precipitation))
    
    (def nq (normal-quant (/ (iseq 1 30) 31)))
    
    (plot-points (iseq 1 30) nq)
    
    (def plot (plot-points nq (bc precip 1)))
    
    (defun change-power (p)
      (send plot :clear :draw nil)
      (send plot :add-points nq (bc precip p)))
    
    (change-power .5)
    
    (sequence-slider-dialog (rseq -5 5 71) :action #'change-power)