[R] Fwd: User defined panel functions in lattice

David Winsemius dwinsemius at comcast.net
Sat Apr 21 01:29:51 CEST 2012


On Apr 20, 2012, at 6:36 PM, Duncan Mackay wrote:

> Hi ilai
>
> Thank you for your advice I think I can now get what I need from  
> what you have said here.
> I think I may have to get involved in packet.number but the original  
> packet.number with its arguments has stuck in my mind and I have not  
> used it.
> I find locfit better than loess etc for a lot of the data I work with.

I'm a bit puzzled by this exchange. I know there is a 'panel.locfit',  
but you two are spelling it differently. Can you explain why you are  
doing so?

 > ?panel.Locfit
No documentation for ‘panel.Locfit’ in specified packages and libraries:
you could try ‘??panel.Locfit’

 > ?panel.locfit

{locfit}	R Documentation
Locfit panel function

Description

This panel function can be used to add locfit fits to plots generated  
by trellis.


> I am trying to construct a function/s to cover as many of the normal  
> situations as possible.
> Usually I have to amend colours lines etc to distinguish the data.
>
> I want to cover a number of situations
> 1 Conditioned by panel no groups
> 2 Conditioned by panel and groups.
> 3 Multiple values for above - to show colleagues (EDA)
> 4 Conditioned by panel and groups + an overall fit for all the data  
> within a panel
> 5 Several y values in a panel eg Y1+Y2 and outer = FALSE with a fit  
> for each of Y1 and Y2
>
> I am trying to cover as many of the above situations in 1 function  
> before resulting to trellis.focus or
> overlaying. The graphs that I normally create are not simple,  
> generally involving useOuterStrips
> which may have different y scales for panel rows (combindeLimits/ 
> manual) and different panel row heights.
>
> locfit is like loess but 2 arguments for smoothing; the degree of  
> smoothing produced by the defaults
> is approximately that of loess but I normally need less smoothing  
> (the same would be apply for loess).
>
> Most of the questions to Rhelp are for 1 with just a small number  
> for 5 and they are not applicable here
> and understanding the requirements for passing arguments in these  
> different situations I find difficult.
> I would like to reduce the number of panel functions to the minimum  
> to cover the general situaltions because
> my graphs are usually not normal and then add to them for a  
> particular situation.
>
> Regards
>
> Duncan
>
>
> At 01:38 21/04/2012, you wrote:
>> 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
>> >> >
>> >> >
>> >> >

>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list