# [S] Summary: adding fits to multipanel display

Franz-Josef Mueter (mueter@ims.uaf.edu)
Sat, 15 Aug 1998 13:29:59 -0800 (AKDT)

Thanks to Prof. Ripley I was able to add the desired fit. My goal was to
add fitted points for a fixed level of a model including one or more
covariates to a multipanel display. The trick was to use the subscripts
argument to xyplot.

Contrary to expectations, this worked using
Version 3.3 Release 1 for Sun SPARC, SunOS 4.1.x

The model was actually:

lm(Y~A/(X+Z))

The fit for a fixed level of Z (let's say Zi) can be displayed as follows:

xyplot(Y ~ X | A, subscripts = T,
panel = function(x, y, subscripts, ...)
{
panel.xyplot(x,y)
z <- Z[subscripts]
fit <- lm(y~x+z)
pred <- predict(fit, newdata = data.frame(x = x,
z = rep(Zi, length(x))))
panel.xyplot(x, pred)
}
)

Original query:

> Let's say I have a model:
>
> lm(Y ~ A/X+Z)
>
> where a is a factor with many levels and z is an important covariate.
> Is there a simple way using trellis graphics to add the fit from this model
> to each panel in the following plot:
>
> xyplot(Y~X | A)
>
> I'm fiddling with something like:
>
> panel = function(x,y) {
> panel.xyplot(x,y)
> panel.xyplot(x,lm(y~x+z))
> }
>
> The problem is of course little z, as it is not part of the formula defining
> the response and predictors and is thus not recognized.
> How can I pass the appropriate Z's to each call to panel.xyplot?
>
> Thanks for any help!
>
> Franz
>
> ***************************************************************
>
> Since all models are wrong, the scientist must be alert
> to what is importantly wrong. It is inappropriate to be
> - Box, 1976
>
> ***************************************************************
> Franz-Josef Mueter | Voice: (907) 474-7839
> Institute of Marine Sciences | (907) 479-8815
> School of Fisheries and Ocean Sciences | Fax: (907) 474-1943
> University of Alaska | e-mail: