[R] lattice: bwplot and panel.lmline()

Deepayan Sarkar deepayan at stat.wisc.edu
Fri Sep 17 21:24:43 CEST 2004


On Friday 17 September 2004 13:52, RenE J.V. Bertin wrote:
> Hello again,
>
> I am doing regressions (using panel.lmline() (and panel.abline(
> rlm(...))) ) inside a panel method which I pass to bwplot().
>
> What I would like to do is create a boxplot of categorised data
> (binned on the independent variable), and superpose a regression line
> which is calculated using the non-categorised, raw data. I expect
> that would give more accurate regression results even if a bwplot
> panel used 'world' co-ordinates.
>
> My initial idea was to do something like
>
> > bwplot( y~x|cond, panel=bwpanel )

Does 

xyplot( y~x|cond, panel=bwpanel, horizontal = FALSE )

do any better?


>
> instead of
>
> > bwplot( y~ Classify(x, binwidth=0.2) | cond, panel=bwpanel )
>
> with Classify a function which bins x and returns the result in an
> ordered factor,
>
> and
>
> bwpanel <- function(x,y, ... )
> {
>  if( is.factor(x) ){
>   panel.bwplot(x,y, ... )
>   nx<-as.numeric(x)
>  }
>  else{
>   nx<-x
>   x<-Classify(nx, binwidth=0.2 )
>   panel.bwplot(x,y, ... )
>  }
>   # add a line showing the means:
>  panel.linejoin(x,y, fun=function(x) mean(x,na.rm=TRUE), 
>                      col="red", lwd=2, ...) 
>                      panel.lmline( nx, y, ... ) 
> # snip
> }
>
> But that doesn't work: bwplot seems to do work on/with x outside of
> the panel function which require x to be a factor.

Yes, it does. If x and y are both numeric, you should use xyplot. It's 
perfectly fine to use panel.bwplot as a panel function with xyplot.

> I then tried to copy the code from bwplot into a wrapper which would
> do the formula parsing, and call bwplot with x replaced by the
> properly categorised version, but got stuck along the way.
>
> I have thus written another wrapper, in which I basically do what I
> wanted to do in bwpanel, storing the 'raw' data AND the condition
> array in an environment. I then retrieve these inside bwpanel, make
> the proper selection using
>
> bwpanel(x, y, ... )
> {
>   # snip
>   xx<- xx[ cond == levels(cond)[ list(...)$panel.counter ] ]
>   # snip
>
> }

The 'right' way to do this would be 

bwpanel <- function(x, y, subscripts, ... )
{
  # snip
  xx<- xx[subscripts]
  # snip
}


> (xx will thus have the current-panel-appropriate subset of the raw
> independent data). Then I can do the regression with the raw
> observations (y being 'raw'), but now I have to transform the
> obtained coefficients so that they are plotted correctly in the
> viewport being used (basically, the smallest factor is mapped to 1,
> the next to 2, etc).
>
> I have this working (see http://rjvbertin.free.fr/RegrInBWPlot.pdf; I
> can send the code if somebody is interested), but I wonder if
>
> 1) something like this has not been foreseen already in lattice (in
> particular, panel.abline will in general not give correct results
> when called from a bwplot panel function!

Could you expand on that? Incorrect results in what sense? panel.abline 
just draws straight lines, it doesn't work with the panel data directly 
(so, for instance, whether or not x is a factor cannot affect it). For 
variables that are factors, the scales are set up so that the levels 
correspond to integers (in other words, as.numeric(x) should give the 
correct coordinates). Given that, I'm not sure how panel.abline can 
give incorrect results.

> 2) Am I doing the right thing to subset my raw independent value
> array -- in particular, there is also a list(...)$panel.number, which
> I have only seen having the same value as $panel.counter?

As mentioned above, you should probably be using subscripts.  
panel.number and panel.counter are going to be the same unless you mess 
with perm.cond and index.cond.

Deepayan




More information about the R-help mailing list