[R] Adding groups to regression line panel function in Lattice

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Sep 12 06:16:02 CEST 2011


On Fri, Sep 9, 2011 at 9:38 PM, Bigelow, Seth <sbigelow at fs.fed.us> wrote:
> I wish to display a single-panel Lattice figure with grouped data and fitted regression lines. I don't seem to be able to get the
> individual regression lines to display, e.g.;
>
> d <- data.frame(q = rep(1:6, each=10), cc = rep(seq(10,100, 10),6))                          # create data frame with group identifier 'q', independent variable cc
> d$ba = d$q*0.1*d$cc + 5*rnorm(nrow(d))                                                                           # create dependent variable 'ba'
>
> mypanel <- function(...){                                                                                                              # panel function
>                panel.lmline(d$cc, d$ba, groups = d$q)                                                                  #
>                panel.xyplot(...)
>                }

But panel.lmline() does not honour a 'groups' argument, so there is no
reason for this to work.

> xyplot(ba~cc,d,
>                groups=q,
>                panel = panel.superpose,
>                panel.groups=mypanel
>                )
>
> Can anyone suggest how to get lines to display by group?

How about

xyplot(ba~cc,d, groups=q, type=c("p", "r"))

> (and how to get lmline panel to fit line without estimating an intercept?)

xyplot(ba~cc,d, groups=q, panel = panel.superpose,
       panel.groups = function(x, y, ...) {
           panel.xyplot(x, y, ...)
           panel.abline(0, coef(lm(y ~ 0 + x)), ...)
       })

-Deepayan


More information about the R-help mailing list