[R] lattice with predicted values

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Oct 14 23:24:22 CEST 2005


On 10/14/05, Martin Henry H. Stevens <HStevens at muohio.edu> wrote:
> Dear lattice wizards,
>
> I am trying to figure out how to plot predicted values in xyplot,
> where the intercept, but not the slope, varies among conditioning
> factor levels. I am sure it involves the groups, but I have been
> unsuccessful in my search in Pinhiero and Bate, in the help files, or
> in the archive, or in my attempts on my own.
>
> My example follows:
>
> FACT is a factor with levels a,b,c
> COV is the covariate
>
> mod ~ lm(Y ~ COV + FACT)
>
>
> #The following draws the right predictions if the relation is the
> same for all factor levels, but I can't figure out how to have the
> same slopes but different intercepts.
>
> # Function to draw predictions in xyplot
>
> panel.predfinal <- function(mod, x, y) {
>      xfit <- seq(min(x), max(x), length=21)
>      yfit <- predict(mod, newdata=data.frame(COV=xfit))
>      llines(xfit,yfit,lty=1) }
>
> xyplot(Y ~ COV | FACT,
>              panel=function(x,y,groups,subscripts){
>              panel.xyplot(x,y)
>              panel.predfinal(mod,x,y) }

A not very satisfactory (but probably good enough for linear fits) is

pred <- predict(mod)

xyplot(Y ~ COV | FACT, pred = pred,
       panel = function(x, y, pred, subscripts, ...) {
           panel.xyplot(x,y,...)
           llines(x, pred[subscripts], ...)
       })

Deepayan




More information about the R-help mailing list