home *** CD-ROM | disk | FTP | other *** search
- (require "graphics")
-
- ;;;;
- ;;;;
- ;;;;
- ;;;; Regression Demo
- ;;;;
- ;;;; Examines the effect of deleting and moving points on a simple linear
- ;;;; regression. This version does not
- ;;;;
- ;;;;
-
- ;;; REGRESSION-DEMO-PROTO - instance variables are
- ;;; x, y - regression variables
- ;;; a, b - regression intercept and slope
- ;;;
- (defproto regression-demo-proto '() () scatterplot-proto)
- (send regression-demo-proto :add-mouse-mode 'point-moving
- :title "Point Moving"
- :cursor 'finger
- :click :do-point-moving)
-
- ;;;;
- ;;;; Overrides for standard plot messages
- ;;;;
-
- ;;;
- ;;; :ISNEW method - only allows two dimensional plot and installs
- ;;; data in the plot.
- ;;;
- (defmeth regression-demo-proto :isnew (x y)
- (call-next-method 2)
- (send self :new-menu)
- (send self :mouse-mode 'point-moving)
- (send self :add-points x y)
- (send self :show-all-points)) ; line added JKL
-
- ;;;
- ;;; :DO-POINT-MOVING method - if there is a point close to
- ;;; the mouse drag it and recalculate the regression line.
- ;;;
- (defmeth regression-demo-proto :do-point-moving (x y a b)
- (let ((p (send self :drag-point x y :draw nil)))
- (if p (send self :set-regression-line))))
-
- ;;;
- ;;; :ADD-POINTS method - add points to the plot, append them to the
- ;;; instance variables x and y, and recalculate the regression
- ;;;
- (defmeth regression-demo-proto :add-points (x y)
- (call-next-method x y :draw nil)
- (send self :adjust-to-data :draw nil)
- (send self :set-regression-line))
-
- ;;;
- ;;; :SHOW-ALL-POINTS - show all points in the plot and recalculate the
- ;;; regression
- ;;;
- (defmeth regression-demo-proto :show-all-points ()
- (call-next-method)
- (send self :set-regression-line))
-
- ;;;
- ;;; :ERASE-SELECTION - erase selection from the plot and recalculate the
- ;;; regression
- ;;;
- (defmeth regression-demo-proto :erase-selection ()
- (call-next-method)
- (send self :set-regression-line))
-
- ;;;;
- ;;;; REGRESSION-DEMO-PROTO Specific Methods
- ;;;;
-
- ;;;
- ;;; :SET-REGRESSION-LINE installs the regression line in the plot
- ;;;
- (defmeth regression-demo-proto :set-regression-line ()
- (let ((coefs (send self :calculate-coefficients)))
- (send self :clear-lines :draw (null coefs))
- (if coefs (apply #'send self :abline coefs))))
-
- ;;;
- ;;; :CALCULATE-COEFFICIENTS calculates the new coefficients and returns them
- ;;; as a list. If there ar not at least two points with different x values
- ;;; NIL is returned
- ;;;
- (defmeth regression-demo-proto :calculate-coefficients ()
- (let ((i (which (send self :point-showing
- (iseq 0 (- (send self :num-points) 1))))))
- (if (<= (length i) 1)
- nil
- (let* ((x (send self :point-coordinate 0 i))
- (y (send self :point-coordinate 1 i))
- (x-bar (mean x))
- (y-bar (mean y))
- (x1 (- x x-bar))
- (y1 (- y y-bar))
- (sxx (sum (* x1 x1)))
- (sxy (sum (* x1 y1)))
- a
- b)
- (when (> sxx 0)
- (setq b (/ sxy sxx))
- (setq a (- y-bar (* b x-bar)))
- (list a b))))))
-
- ;;;;
- ;;;; Regression Demo Function
- ;;;;
- (defun regression-demo (x y)
- (let ((w (send regression-demo-proto :new x y)))
- w))
-