[R] Fwd: User defined panel functions in lattice

ilai keren at math.montana.edu
Fri Apr 20 17:38:37 CEST 2012


Duncan,
First off, I admit it is not clear to me what you are trying to
achieve and more importantly, why? by "why" I mean 1) I don't see the
advantage of writing one general panel function for completely
different situations (one/multiple smoothers, grouping levels etc.) 2)
your intended result as I understand it seems rather cluttered, google
<chartjunk>. 3) I am unfamiliar with locfit package, but are we
reinventing the wheel here ? i.e. will modifying settings in xyplot(y
~x, xx, groups = Farm, type=c('p','smooth')) achieve the same ?

With your initial reproducible example (thank you) it was easy to
eliminate the errors, but clearly the resulting plots are not what you
intended (continue inline):

On Thu, Apr 19, 2012 at 4:23 PM, Duncan Mackay <mackay at northnet.com.au> wrote:
<snip>
> 3. What I want to be able to add in the above is extra lines with different
> values of nn.
>   I think I will have to modify panel.Locfit so that it goes through
> different values of nn in each of the panels and groups if I want different
> colours for extra lines with different nn values

Yes you could. There are several options:
add group.number to the arguments of panel.locfit and use it to make
nn a vector, along the lines of
    panel.foo <- function(x,y,group.number,theta,...){
      smpar <- theta[group.number]
      panel.loess(x,y,smpar,...)
      panel.xyplot(x,y,...)
    }
 xyplot(y~x,xx,group=Farm,theta=c(4,1,.4),panel=panel.superpose,panel.groups=panel.foo)

 # or
xyplot(y~x|Farm,xx,group=Padd,theta=c(.6,1),
    panel=panel.superpose,panel.groups=panel.foo)

Here you will need to modify the Farm group to 6 levels - 3*two smoothers.

You could make nn a list and loop over it inside the panel function.
Looks like you tried something like that with specifying 2
panel.Locfit, one suggestion to your code:

                     panel.Locfit(x,y,...) # default 0.7
                        panel.Locfit(x,y,nn=0.9)   # i.e. remove the
... to avoid clashes

Finally, use ?trellis.focus to plot the second smoother "post-hoc".
also the latticeExtra package has many useful tools to create layers
of the same (or different) plot with different settings.

> 4 Produce an extra line for a fit for all the groups in 1/2+ panels.
>   As for 3 but I do not know how to group all the x and y's  for each of the
> panes using panel.groups

Why does it matter ? seems you have failed to learn the lesson from
the first post - the same functionality applies to 1 as to multiple
panels. Does each panel have a different grouping structure ? use
packet.number() for panels similar to group.number idea.

> I need to do this and then scale up for a panel function to include
> confidence bands

than expand the xlim,ylim or scales in ?xyplot

>
> For the record making Farm and Padd factors. With 1 panel and groups = Farm
> works with the extra line the same colour for its group
> a similar situation for the three panels when conditioned by Farm and groups
> = Pad

????

Like I said I am a little lost on this problem but I hope this helps
giving some direction.
Cheers


>
>  xyplot(y ~x, xx,
>         groups = Farm,
>
>         par.settings = list(strip.background = list(col = "transparent"),
>                             superpose.line   = list(col = c("black","grey"),
>                                                             lwd = c(1,2,3),
>                                                             lty = c(2,1,3)),
>                             superpose.symbol = list(cex = c(0.8, 0.7,0.7),
>                                                     col =
> c("red","black","blue"),
>                                                     pch = c(20,4,16))
>                   ),
>         auto.key=list(lines=T,points = T,rectangles=F),
>
>         panel  = panel.superpose,
>         panel.groups=function(x,y, ...){
>
>                        panel.xyplot(x,y,...)
>                        panel.Locfit(x,y,...) # default 0.7
>                        panel.Locfit(x,y,nn=0.9,...)
>
>                      }
>  ) ## xyplot
>
>
> Regards
>
> Duncan
>
>
> At 02:12 20/04/2012, you wrote:
>>
>> On Thu, Apr 19, 2012 at 2:30 AM, Duncan Mackay <mackay at northnet.com.au>
>> wrote:
>> > Hi
>> >
>> >  xyplot(y ~x|Farm,xx,
>> >         groups = Padd,
>> >         panel = panel.superpose,
>> >         panel.groups=function(x,y, ...){
>> >                        panel.Locfit(x,y,...)
>> >                        panel.xyplot(x,y,...)
>> >                      }
>> >  ) ## xyplot
>> >
>> > The above works nicely and also without par.setting giving lattice
>> > defaults.
>> > The par.setting is handy for a lot of graphs that I do.
>> >
>> > But when I tried a 1 panel plot I get the error message.
>> >
>> >  xyplot(y ~x,xx,
>> >         groups = Farm,
>> >         auto.key=TRUE,
>> >         panel = function(x,y, ...){
>> >
>> >                        panel.Locfit(x,y,...)
>> >                        panel.xyplot(x,y,...)
>> >                      }
>> >         )
>> >
>>
>> These two plots are NOT THE SAME. Did you want the same as the first
>> but with groups being Farm and Padd ignored ? in that case you (again)
>> need a panel.groups:
>>
>>  xyplot(y ~x,xx,
>>       groups = Farm,
>>       auto.key=TRUE,
>>       panel = panel.superpose,panel.groups=function(x,y,...){
>>                      panel.Locfit(x,y,...)
>>                      panel.xyplot(x,y,...)
>>                    }
>>       )
>>
>>
>> > If I want to plot another curve with different smoothing
>> > but gives an error message without par.settings if i want to add
>> >                        panel.Locfit(x,y,nn= 0.9,lwd = c(1,2,3), ...)
>> >
>> > Error using packet 1
>> > formal argument "Iwd" matched by multiple actual arguments
>>
>> It is all in the way you initially specified how to pass the arguments
>> for panel.Locfit. This works without error:
>>
>>  xyplot(y ~x,xx,
>>       groups = Farm,
>>       auto.key=TRUE,lwd=1:3,
>>       panel = panel.superpose,panel.groups=function(x,y,nn,...){
>>                      panel.Locfit(x,y,nn=.9,...)
>>                      panel.xyplot(x,y,...)
>>                    }
>>       )
>>
>>
>> HTH
>>
>>
>>
>> > I also need to plot a smoothed line for all groups trying groups,
>> > subscripts
>> > and panel.groups as arguments without success
>> >
>> > Any solutions to solve the above will be gratefully received and
>> > faithfully
>> > applied.
>> >
>> > Duncan
>> >
>> > sessionInfo()
>> > R version 2.15.0 (2012-03-30)
>> > Platform: i386-pc-mingw32/i386 (32-bit)
>> >
>> > locale:
>> > [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
>> > LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
>> > LC_TIME=English_Australia.1252
>> >
>> > attached base packages:
>> > [1] datasets  utils     stats     graphics  grDevices grid      methods
>> > base
>> >
>> > other attached packages:
>> > [1] locfit_1.5-7        R.oo_1.9.3          R.methodsS3_1.2.2
>> > foreign_0.8-49
>> >      chron_2.3-42        MASS_7.3-17 latticeExtra_0.6-19
>> > RColorBrewer_1.0-5
>> > [9] lattice_0.20-6
>> >
>> > loaded via a namespace (and not attached):
>> > [1] tools_2.15.0
>> >
>> >
>> >
>> > Duncan Mackay
>> > Department of Agronomy and Soil Science
>> > University of New England
>> > ARMIDALE NSW 2351
>> > Email home: mackay at northnet.com.au
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list