3 ;;;; A Simple GLIM Implementation
13 ;;;; Links are objects responding to three messages:
15 ;;;; :eta takes a set of mean values and returns the linear
17 ;;;; :means takes a set of linear predictor values and returns
18 ;;;; the mean values (the inverse of :eta)
19 ;;;; :derivs takes a set of mean values and returns the values of
20 ;;;; the derivatives of the linear predictors at the mean
23 ;;;; The arguments should be sequences. The glim-link-proto prototype
24 ;;;; implements an identity link. Links for binomial errors are defined
25 ;;;; for means in the unit interval [0, 1], i. e. for n = 1 trials.
28 (defproto glim-link-proto
)
30 (defmeth glim-link-proto
:eta
(mu)
32 Returns linear predictor values at MU."
35 (defmeth glim-link-proto
:means
(eta)
37 Returns mean values for linear predictor ETA."
40 (defmeth glim-link-proto
:derivs
(mu)
42 Returns d(eta)/d(mu) values at MU."
43 (repeat 1 (length mu
)))
45 (defmeth glim-link-proto
:print
(&optional
(stream t
))
46 (format stream
"#<Glim Link Object: ~s>" (slot-value 'proto-name
)))
48 (defmeth glim-link-proto
:save
()
49 (let ((proto (slot-value 'proto-name
)))
50 (if (eq self
(eval proto
)) proto
`(send ,proto
:new
))))
53 ;;;; Identity Link Prototype
56 (defproto identity-link
() () glim-link-proto
)
59 ;;;; Log Link Prototype
62 (defproto log-link
() () glim-link-proto
)
64 (defmeth log-link
:eta
(mu) (log mu
))
65 (defmeth log-link
:means
(eta) (exp eta
))
66 (defmeth log-link
:derivs
(mu) (/ mu
))
69 ;;;; Inverse-link-prototype
72 (defproto inverse-link
() () glim-link-proto
)
74 (defmeth inverse-link
:eta
(mu) (/ mu
))
75 (defmeth inverse-link
:means
(eta) (/ eta
))
76 (defmeth inverse-link
:derivs
(mu) (- (/ (^ mu
2))))
82 (defproto sqrt-link
() () glim-link-proto
)
84 (defmeth sqrt-link
:eta
(mu) (sqrt mu
))
85 (defmeth sqrt-link
:means
(eta) (^ eta
2))
86 (defmeth sqrt-link
:derivs
(mu) (/ 0.5 (sqrt mu
)))
89 ;;;; Power Link Prototype
92 (defproto power-link-proto
'(power) () glim-link-proto
)
94 (defmeth power-link-proto
:isnew
(power) (setf (slot-value 'power
) power
))
95 (defmeth power-link-proto
:power
() (slot-value 'power
))
97 (defmeth power-link-proto
:print
(&optional
(stream t
))
98 (format stream
"#<Glim Link Object: Power Link (~s)>" (send self
:power
)))
100 (defmeth power-link-proto
:save
()
101 `(send power-link-proto
:new
,(send self
:power
)))
103 (defmeth power-link-proto
:eta
(mu) (^ mu
(send self
:power
)))
104 (defmeth power-link-proto
:means
(eta) (^ eta
(/ (slot-value 'power
))))
105 (defmeth power-link-proto
:derivs
(mu)
106 (let ((p (slot-value 'power
)))
107 (* p
(^ mu
(- p
1)))))
110 ;;;; Logit Link Prototype
113 (defproto logit-link
() () glim-link-proto
)
115 (defmeth logit-link
:eta
(p) (log (/ p
(- 1 p
))))
116 (defmeth logit-link
:means
(eta)
117 (let ((exp-eta (exp eta
)))
118 (/ exp-eta
(+ 1 exp-eta
))))
119 (defmeth logit-link
:derivs
(p) (+ (/ p
) (/ (- 1 p
))))
122 ;;;; Probit Link Prototype
125 (defproto probit-link
() () glim-link-proto
)
127 (defmeth probit-link
:eta
(p) (normal-quant p
))
128 (defmeth probit-link
:means
(eta) (normal-cdf eta
))
129 (defmeth probit-link
:derivs
(p) (/ 1 (normal-dens (normal-quant p
))))
132 ;;;; Complimentary Log-Log Link Prototype
135 (defproto cloglog-link
() () glim-link-proto
)
137 (defmeth cloglog-link
:eta
(p) (log (- (log (- 1 p
)))))
138 (defmeth cloglog-link
:means
(eta) (- 1 (exp (- (exp eta
)))))
139 (defmeth cloglog-link
:derivs
(p)
145 ;;;; The General GLIM Prototype
146 ;;;; (Uses Normal Errors and an Identity Link)
151 '(yvar link offset pweights scale est-scale
152 epsilon epsilon-dev count-limit verbose recycle
155 regression-model-proto
)
161 (defmeth glim-proto
:yvar
(&optional
(new nil set
))
162 "Message args: (&optional new)
163 Sets or returns dependent variable."
165 (setf (slot-value 'yvar
) new
)
166 (send self
:needs-computing t
))
169 (defmeth glim-proto
:link
(&optional
(new nil set
))
170 "Message args: (&optional new)
171 Sets or returns link object."
173 (setf (slot-value 'link
) new
)
174 (send self
:needs-computing t
))
177 (defmeth glim-proto
:offset
(&optional
(new nil set
))
178 "Message args: (&optional (new nil set))
179 Sets or returns offset values."
181 (setf (slot-value 'offset
) new
)
182 (send self
:needs-computing t
))
183 (slot-value 'offset
))
185 (defmeth glim-proto
:pweights
(&optional
(new nil set
))
186 "Message args: (&optional (new nil set))
187 Sets or returns prior weights."
189 (setf (slot-value 'pweights
) new
)
190 (send self
:needs-computing t
))
191 (slot-value 'pweights
))
193 ;; changing the scale does not require recomputing the estimates
194 (defmeth glim-proto
:scale
(&optional
(new nil set
))
195 "Message args: (&optional (new nil set))
196 Sets or returns value of scale parameter."
197 (if set
(setf (slot-value 'scale
) new
))
200 (defmeth glim-proto
:estimate-scale
(&optional
(val nil set
))
201 "Message args: (&optional (val nil set))
202 Sets or returns value of ESTIMATE-SCALE option."
203 (if set
(setf (slot-value 'est-scale
) val
))
204 (slot-value 'est-scale
))
206 (defmeth glim-proto
:epsilon
(&optional new
)
207 "Message args: (&optional new)
208 Sets or returns tolerance for relative change in coefficients."
209 (if new
(setf (slot-value 'epsilon
) new
))
210 (slot-value 'epsilon
))
212 (defmeth glim-proto
:epsilon-dev
(&optional new
)
213 "Message args: (&optional new)
214 Sets or returns tolerance for change in deviance."
215 (if new
(setf (slot-value 'epsilon-dev
) new
))
216 (slot-value 'epsilon-dev
))
218 (defmeth glim-proto
:count-limit
(&optional new
)
219 "Message args: (&optional new)
220 Sets or returns maximum number of itrations."
221 (if new
(setf (slot-value 'count-limit
) new
))
222 (slot-value 'count-limit
))
224 (defmeth glim-proto
:recycle
(&optional
(new nil set
))
225 "Message args: (&optional new)
226 Sets or returns recycle option. If option is not NIL, current values
227 are used as initial values by :COMPUTE method."
229 (setf (slot-value 'recycle
) new
))
230 (slot-value 'recycle
))
232 (defmeth glim-proto
:verbose
(&optional
(val nil set
))
233 "Message args: (&optional (val nil set))
234 Sets or returns VERBOSE option. Iteration info is printed if option
236 (if set
(setf (slot-value 'verbose
) val
))
237 (slot-value 'verbose
))
239 (defmeth glim-proto
:eta
()
241 Returns linear predictor values for durrent fit."
244 (defmeth glim-proto
:set-eta
(&optional val
)
246 (setf (slot-value 'eta
) val
)
247 (setf (slot-value 'eta
)
248 (+ (send self
:offset
) (send self
:fit-values
)))))
250 (defmeth glim-proto
:deviances
()
252 Returns deviances for durrent fit."
253 (slot-value 'deviances
))
255 (defmeth glim-proto
:set-deviances
()
256 (setf (slot-value 'deviances
)
257 (send self
:fit-deviances
(send self
:fit-means
))))
260 ;;;; Overrides for Regression Methods
263 ;; A variant of this method should work for any object whose slot values
264 ;; have valid printed representations.
265 (defmeth glim-proto
:save
()
266 (let* ((proto (slot-value 'proto-name
))
267 (slots (remove 'link
(send self
:own-slots
)))
268 (values (mapcar #'slot-value slots
)))
269 `(let ((object (make-object ,proto
))
272 (flet ((add-slot (s v
) (send object
:add-slot s v
)))
273 (mapcar #'add-slot slots values
)
274 (add-slot 'link
,(send (send self
:link
) :save
)))
277 (defmeth glim-proto
:sigma-hat
() (sqrt (send self
:scale
)))
279 ;; This override is only used to modify the documentation string.
280 (defmeth glim-proto
:fit-values
()
282 Returns Xb, the linear predictor values without the offset.
283 The :fit-means method returns fitted means for the current estimates."
286 ;; this should be merged with the regression-model method
287 (defmeth glim-proto
:x
(&optional x
)
291 ((vectorp x
) (list x
))
292 ((and (consp x
) (numberp (car x
))) (list x
))
294 (call-next-method (if (matrixp x
) x
(apply #'bind-columns x
)))))
297 (defmeth glim-proto
:raw-residuals
()
299 Returns the raw residuals for a model."
300 (- (send self
:yvar
) (send self
:fit-means
)))
302 ;; This override is needed because regression-model-proto defines its
303 ;; residuals in terms of :raw-residuals.
304 (defmeth regression-model-proto
:residuals
()
306 Returns the Pearson residuals."
307 (let ((raw-residuals (- (send self
:y
) (send self
:fit-values
)))
308 (weights (send self
:weights
)))
309 (if weights
(* (sqrt weights
) raw-residuals
) raw-residuals
)))
312 ;;;; Computing methods
315 (defmeth glim-proto
:compute
()
316 (let* ((epsilon (send self
:epsilon
))
317 (epsilon-dev (send self
:epsilon-dev
))
318 (maxcount (send self
:count-limit
))
319 (low-lim (* 2 (/ machine-epsilon epsilon
)))
320 (verbose (send self
:verbose
)))
321 (unless (and (send self
:eta
) (send self
:recycle
))
322 (send self
:initialize-search
))
323 (send self
:compute-step
)
324 (do ((count 1 (+ count
1))
325 (beta 0 (send self
:coef-estimates
))
327 (dev 0 (send self
:deviance
))
329 ((or (> count maxcount
)
330 (< (max (abs (/ (- beta last-beta
)
331 (pmax (abs last-beta
) low-lim
))))
333 (< (abs (- dev last-dev
)) epsilon-dev
)))
335 (format t
"Iteration ~d: deviance = ~,6g~%"
336 count
(send self
:deviance
)))
337 (send self
:compute-step
))))
339 (defmeth glim-proto
:compute-step
()
341 Executes one iteratively reweighted least squares step."
342 (let* ((yvar (send self
:yvar
))
343 (offset (send self
:offset
))
344 (eta (send self
:eta
))
345 (mu (send self
:fit-means eta
))
346 (d-eta (send self
:fit-link-derivs mu
))
347 (z (- (+ eta
(* (- yvar mu
) d-eta
)) offset
))
348 (v (send self
:fit-variances mu
))
349 (w-inv (* d-eta d-eta v
))
350 (pw (send self
:pweights
)))
352 (send self
:weights
(if pw
(/ pw w-inv
) (/ w-inv
)))
353 (call-method regression-model-proto
:compute
)
355 (send self
:set-deviances
)
356 (if (send self
:estimate-scale
)
357 (send self
:scale
(send self
:fit-scale
)))))
359 (defmeth glim-proto
:deviance
()
361 Returns deviance for included cases."
362 (sum (if-else (send self
:included
) (send self
:deviances
) 0)))
364 (defmeth glim-proto
:mean-deviance
()
366 Returns mean deviance for included cases, adjusted for degrees of
368 (/ (send self
:deviance
) (send self
:df
)))
370 (defmeth glim-proto
:initialize-search
(&optional eta
)
372 (if eta eta
(send (send self
:link
) :eta
(send self
:initial-means
))))
373 (send self
:needs-computing t
))
375 (defmeth glim-proto
:fit-means
(&optional
(eta (send self
:eta
)))
376 "Message args: (&optional (eta (send self :eta)))
377 Retruns mean values for current or supplied ETA."
378 (send (send self
:link
) :means eta
))
380 (defmeth glim-proto
:fit-link-derivs
(mu)
382 Returns link derivative values at MU."
383 (send (send self
:link
) :derivs mu
))
385 (defmeth glim-proto
:display
()
387 Prints the IRWLS regression summary. Variables not used in the fit are
389 (let ((coefs (coerce (send self
:coef-estimates
) 'list
))
390 (se-s (send self
:coef-standard-errors
))
392 (p-names (send self
:predictor-names
)))
393 (if (send self
:weights
)
394 (format t
"~%Weighted Least Squares Estimates:~2%")
395 (format t
"~%Least Squares Estimates:~2%"))
396 (when (send self
:intercept
)
397 (format t
"Constant~25t~13,6g~40t(~,6g)~%" (car coefs
) (car se-s
))
398 (setf coefs
(cdr coefs
))
399 (setf se-s
(cdr se-s
)))
400 (dotimes (i (array-dimension x
1))
402 ((member i
(send self
:basis
))
403 (format t
"~a~25t~13,6g~40t(~,6g)~%"
404 (select p-names i
) (car coefs
) (car se-s
))
405 (setf coefs
(cdr coefs
) se-s
(cdr se-s
)))
406 (t (format t
"~a~25taliased~%" (select p-names i
)))))
408 (if (send self
:estimate-scale
)
409 (format t
"Scale Estimate:~25t~13,6g~%" (send self
:scale
))
410 (format t
"Scale taken as:~25t~13,6g~%" (send self
:scale
)))
411 (format t
"Deviance:~25t~13,6g~%" (send self
:deviance
))
412 (format t
"Number of cases:~25t~9d~%" (send self
:num-cases
))
413 (if (/= (send self
:num-cases
) (send self
:num-included
))
414 (format t
"Number of cases used:~25t~9d~%" (send self
:num-included
)))
415 (format t
"Degrees of freedom:~25t~9d~%" (send self
:df
))
420 ;;;; Error-Dependent Methods (Normal Errors)
423 (defmeth glim-proto
:initial-means
()
425 Returns initial means estimate for starting the iteration."
428 (defmeth glim-proto
:fit-variances
(mu)
430 Returns variance function values at MU."
431 (repeat 1 (length mu
)))
433 (defmeth glim-proto
:fit-deviances
(mu)
435 Returns deviance values at MU."
436 (let ((raw-dev (^
(- (send self
:yvar
) mu
) 2))
437 (pw (send self
:pweights
)))
438 (if pw
(* pw raw-dev
) raw-dev
)))
440 (defmeth glim-proto
:fit-scale
()
442 Returns estimate of scale parameter."
443 (send self
:mean-deviance
))
446 ;;;; Initial values for the prototype
449 (send glim-proto
:scale
1.0)
450 (send glim-proto
:offset
0.0)
451 (send glim-proto
:link identity-link
)
452 (send glim-proto
:estimate-scale t
)
453 (send glim-proto
:epsilon
.000001)
454 (send glim-proto
:epsilon-dev
.001)
455 (send glim-proto
:count-limit
30)
456 (send glim-proto
:verbose t
)
462 (defmeth glim-proto
:isnew
(&key x
478 (if link
(send self
:link link
))
479 (send self
:offset offset
)
480 (send self
:intercept intercept
)
481 (send self
:pweights pweights
)
482 (send self
:recycle recycle
)
483 (send self
:verbose verbose
)
484 (if included
(send self
:included included
))
485 (if predictor-names
(send self
:predictor-names predictor-names
))
486 (if response-name
(send self
:response-name response-name
))
487 (if (or y case-labels
) (send self
:case-labels case-labels
)) ; needs fixing
488 (if print
(send self
:display
)))
491 ;;;; Some Additional Residual Methods
494 (defmeth glim-proto
:chi-residuals
()
496 Returns the components of Pearson's chi-squared residuals."
497 (send self
:residuals
))
499 (defmeth glim-proto
:standardized-chi-residuals
()
501 Returns the components of Standardized Pearson Residuals (Williams, 1987)."
502 (send self
:studentized-residuals
))
504 (defmeth glim-proto
:deviance-residuals
()
506 Returns the components of deviance residuals for non binomial models."
507 (let* ((dev (sqrt (send self
:deviances
)))
508 (sign (if-else (< (send self
:yvar
) (send self
:fit-means
)) -
1 1)))
511 (defmeth glim-proto
:standardized-deviance-residuals
()
513 Returns the standardized deviance residuals, (Davison and Tsai, 1989)."
514 (let* ((dev (send self
:deviance-residuals
))
515 (inc (send self
:included
))
516 (h (send self
:leverages
)))
518 (/ dev
(sqrt (* (send self
:scale
) (- 1 h
))))
519 (/ dev
(sqrt (* (send self
:scale
) (+ 1 h
)))))))
521 (defmeth glim-proto
:g2-residuals
()
523 Returns a weighted combination of the standardized deviance and chi
524 residuals, (Davison and Tsai, 1989)."
525 (let* ((dev (send self
:standardized-deviance-residuals
))
526 (chi (send self
:standardized-chi-residuals
))
527 (inc (send self
:included
))
528 (h (send self
:leverages
))
529 (sign (if-else (< dev
0) -
1 1)))
530 (* sign
(sqrt (+ (* (- 1 h
) (^ dev
2))
535 ;;;; Normal Regression Model Prototype
539 (defproto normalreg-proto
() () glim-proto
)
542 ;;;; Normal Model Constructor Function
545 (defun normalreg-model (x y
&rest args
)
546 "Args: (x y &rest args)
547 Returns a normal regression model. Accepts :LINK, :OFFSET and :VERBOSE
548 keywords in addition to the keywords accepted by regression-model."
549 (apply #'send normalreg-proto
:new
:x x
:y y args
))
553 ;;;; Poisson Regression Model Prototype
557 (defproto poissonreg-proto
() () glim-proto
)
560 ;;;; Error-Dependent Methods (Poisson Errors)
563 (defmeth poissonreg-proto
:initial-means
() (pmax (send self
:yvar
) 0.5))
565 (defmeth poissonreg-proto
:fit-variances
(mu) mu
)
567 (defmeth poissonreg-proto
:fit-deviances
(mu)
568 (flet ((log+ (x) (log (if-else (< 0 x
) x
1)))) ; to prevent log of zero
569 (let* ((y (send self
:yvar
))
570 (raw-dev (* 2 (- (* y
(log+ (/ y mu
))) (- y mu
))))
571 (pw (send self
:pweights
)))
572 (if pw
(* pw raw-dev
) raw-dev
))))
575 ;;;; Initial values for the prototype
578 (send poissonreg-proto
:estimate-scale nil
)
579 (send poissonreg-proto
:link log-link
)
582 ;;;; Poisson Model Constructor Functions
585 (defun poissonreg-model (x y
&rest args
)
586 "Args: (x y &rest args)
587 Returns a Poisson regression model. Accepts :LINK, :OFFSET and :VERBOSE
588 keywords in addition to the keywords accepted by regression-model."
589 (apply #'send poissonreg-proto
:new
:x x
:y y args
))
591 (defun loglinreg-model (x y
&rest args
)
592 "Args: (x y &rest args)
593 Returns a Poisson regression model with a log link. Accepts :OFFSET and
594 :VERBOSE keywords in addition to the keywords accepted by regression-model."
595 (apply #'send poissonreg-proto
:new
:x x
:y y
:link log-link args
))
599 ;;;; Binomial Regression Model Prototype
603 (defproto binomialreg-proto
'(trials) () glim-proto
)
609 (defmeth binomialreg-proto
:trials
(&optional new
)
611 Sets or retruns number of trials for each observation."
613 (setf (slot-value 'trials
) new
)
614 (send self
:needs-computing t
))
615 (slot-value 'trials
))
618 ;;;; Overrides for link-related methods to incorporate trials
621 (defmeth binomialreg-proto
:fit-means
(&optional
(eta (send self
:eta
)))
622 (let ((n (send self
:trials
))
623 (p (call-next-method eta
)))
626 (defmeth binomialreg-proto
:fit-link-derivs
(mu)
627 (let* ((n (send self
:trials
))
628 (d (call-next-method (/ mu n
))))
631 (defmeth binomialreg-proto
:initialize-search
(&optional eta
)
633 (if eta eta
(send (send self
:link
) :eta
(send self
:initial-probs
)))))
636 ;;;; Error-Dependent Methods (Binomial Errors)
639 (defmeth binomialreg-proto
:initial-probs
()
640 (let* ((n (send self
:trials
))
641 (p (/ (pmax (pmin (send self
:yvar
) (- n
0.5)) 0.5) n
)))
644 (defmeth binomialreg-proto
:initial-means
()
645 (* (send self
:trials
) (send self
:initial-probs
)))
647 (defmeth binomialreg-proto
:fit-variances
(mu)
648 (let* ((n (send self
:trials
))
652 (defmeth binomialreg-proto
:fit-deviances
(mu)
653 (flet ((log+ (x) (log (if-else (< 0 x
) x
1)))) ; to prevent log of zero
654 (let* ((n (send self
:trials
))
655 (y (send self
:yvar
))
658 (pw (send self
:pweights
))
659 (raw-dev (* 2 (+ (* y
(log+ (/ y mu
)))
660 (* n-y
(log+ (/ n-y n-mu
)))))))
661 (if pw
(* pw raw-dev
) raw-dev
))))
667 (defmeth binomialreg-proto
:fit-probabilities
()
669 Returns the fitted probabilities for the model."
670 (/ (send self
:fit-means
) (send self
:trials
)))
676 (defmeth binomialreg-proto
:isnew
(&rest args
&key trials
)
677 (send self
:trials trials
)
678 (apply #'call-next-method args
))
681 ;;;; Initial values for the prototype
684 (send binomialreg-proto
:estimate-scale nil
)
685 (send binomialreg-proto
:link logit-link
)
688 ;;;; Binomial Model Constructor Functions
691 (defun binomialreg-model (x y n
&rest args
)
692 "Args: (x y n &rest args)
693 Returns a binomial regression model. Accepts :LINK, :OFFSET and :VERBOSE
694 keywords in addition to the keywords accepted by regression-model."
695 (apply #'send binomialreg-proto
:new
:x x
:y y
:trials n args
))
697 (defun logitreg-model (x y n
&rest args
)
698 "Args: (x y n &rest args)
699 Returns a logistic regression model (binomial regression model with logit
700 link). Accepts :OFFSET and :VERBOSE keywords in addition to the keywords
701 accepted by regression-model."
702 (apply #'send binomialreg-proto
:new
703 :x x
:y y
:trials n
:link logit-link args
))
705 (defun probitreg-model (x y n
&rest args
)
706 "Args: (x y n &rest args)
707 Returns a probit regression model (binomial regression model with probit
708 link). Accepts :OFFSET and :VERBOSE keywords in addition to the keywords
709 accepted by regression-model."
710 (apply #'send binomialreg-proto
:new
711 :x x
:y y
:trials n
:link probit-link args
))
715 ;;;; Gamma Regression Model Prototype
719 (defproto gammareg-proto
() () glim-proto
)
722 ;;;; Error-Dependent Methods
725 (defmeth gammareg-proto
:initial-means
() (pmax (send self
:yvar
) 0.5))
727 (defmeth gammareg-proto
:fit-variances
(mu) (^ mu
2))
729 (defmeth gammareg-proto
:fit-deviances
(mu)
730 (let* ((y (send self
:yvar
))
731 (pw (send self
:pweights
))
732 (raw-dev (* 2 (+ (- (log (/ y mu
))) (/ (- y mu
) mu
)))))
733 (if pw
(* raw-dev pw
) raw-dev
)))
736 ;;;; Initial values for the prototype
739 (send gammareg-proto
:link inverse-link
)
742 ;;;; Gamma Model Constructor Function
745 (defun gammareg-model (x y
&rest args
)
746 "Args: (x y &rest args)
747 Returns a Gamma regression model. Accepts :LINK, :OFFSET and :VERBOSE
748 keywords in addition to the keywords accepted by regression-model."
749 (apply #'send gammareg-proto
:new
:x x
:y y args
))
753 ;;;; Some Simple Design Matrix Tools
757 (defun indicators (x &key
(drop-first t
) (test #'eql
))
758 "Args: (x &key (drop-first t) (test #'eql))
759 Returns a list of indicators sequences for the levels of X. TEST is
760 used to check equality of levels. If DROP-FIRST is true, the indicator
761 for the first level is dropped."
762 (let ((levels (remove-duplicates (coerce x
'list
))))
763 (mapcar #'(lambda (lev) (if-else (map-elements test lev x
) 1 0))
764 (if drop-first
(rest levels
) levels
))))
766 (defun cross-terms (x &rest args
)
767 "Args: (x &rest args)
768 Arguments should be lists. Returns list of cross products, with the first
769 argument list varying slowest."
771 (0 (error "too few arguments"))
772 (1 (let ((y (first args
)))
774 (mapcar #'(lambda (a) (mapcar #'(lambda (b) (* a b
)) y
)) x
))))
775 (t (cross-terms x
(apply #'cross-terms args
)))))
777 (defun level-names (x &key
(prefix "") (drop-first t
))
778 "Args: (x &key (prefix \"\") (drop-first t))
779 Constructs name strings using unique levels in X and PREFIX."
780 (let ((levels (remove-duplicates (coerce x
'list
))))
781 (mapcar #'(lambda (x) (format nil
"~a(~a)" prefix x
))
782 (if drop-first
(rest levels
) levels
))))
784 (defun cross-names (x &rest args
)
785 "Args: (x &rest args)
786 Arguments should be lists. Constructs cross products of names, separated
787 by dots. First index varies slowest."
788 (flet ((paste (x y
) (format nil
"~a.~a" x y
)))
790 (0 (error "too few arguments"))
791 (1 (let ((y (first args
)))
793 (mapcar #'(lambda (a) (mapcar #'(lambda (b) (paste a b
)) y
))
795 (t (cross-names x
(apply #'cross-names args
))))))