[R] Lattice: problem using panel.superpose and panel.groups

Michael Braun braunm at MIT.EDU
Sat Aug 16 20:12:57 CEST 2008


Dieter:

Thank you for your response.  As you requested, I created a self- 
running example, pasted below.  It may be a little wordier than I  
would like, but it runs.
The data I am creating assumes that there are two datasets ("calib"  
and "hold"), with actual values actual.calib and actual.hold.  I then  
simulate data for three models, A, B and C.  I want to plot a 3 x 2  
lattice that has bwplots for each model-dataset combination in each  
panel.  On top of that, I want to superimpose a line that plots the  
actual data for calib in all three panels in the calib column (and the  
same for hold).

The data.frame sim.data is just the simulated data, and actual.data is  
just the actual data.  all.data combines them both, and the group  
variable identifies whether a record is from "sim" or "actual."  Note  
that there are 50 simulations, but only one actual, for each model- 
dataset combination.  The presentation in all.data is kind of  
wasteful, since I replicated the same actual data for all 3 models.   
So, ideally, I would work with sim.data and actual.data separately.   
But I can use all.data if necessary.

I apologize that I was not more claear in my earlier post, but  
hopefully this gives you enough information.  Thanks again for helping  
with this.

Michael



library(lattice)

# creating datasets


models <- c("A","B","C")
datasets <- c("calib","hold")
counts <- 1:10
n.sims <- 50
sim.data <- NULL
all.data <- NULL
actual.calib <- seq(0,5,length=10)
actual.hold <- seq(5,0,length=10)
cols <- c("model","dataset","count","value","group")

actual.data <- as.data.frame(cbind(c(rep("calib",10),rep("hold",10)),
                                     
c(1:10,1:10),c(actual.calib,actual.hold)))

set.names <- c(rep("calib",n.sims),rep("hold",n.sims))

for (mod in models) {
   for (y in counts) {

     x.calib <- rnorm(n=n.sims,mean=actual.calib[y],sd=1)
     x.hold <- rnorm(n=n.sims,mean=actual.hold[y],sd=1)

     x <- as.data.frame(cbind(rep(mod,2*n.sims), set.names, y,
                              c(x.calib,x.hold),rep("sim",2*n.sims)))

     colnames(x) <- cols
     sim.data <- rbind(sim.data, x)
   }

   rep.actual <- as.data.frame(cbind(rep(mod,20),
                                     actual.data,rep("actual",20)))
   colnames(rep.actual) <- cols
   all.data <- rbind(all.data,sim.data,rep.actual)
}

colnames(actual.data) <- c("dataset","count","value")

#  for some reason, value is encoded as a factor.
#  This changes value to numeric.
#  (is there a better way to do this?)

sim.data[,"value"] <- as.numeric(levels(sim.data[,"value"]) 
[sim.data[,"value"]])
all.data[,"value"] <- as.numeric(levels(all.data[,"value"]) 
[all.data[,"value"]])
actual.data[,"value"] <- as.numeric(levels(actual.data[,"value"]) 
[actual.data[,"value"]])


# at this point, there are three data frames worth considering:
# sim.data - simulated data
# actual.data - actual data
# all.data - sim.data, and the actual.data replicated for each model


# create panel function

panel.ppc.plot <- function(...,group.number) {

   if (group.number==1) {
     panel.bwplot(...)
   } else {

     panel.lines(...)
   }
}

# create and plot lattice object

obj <- bwplot(as.numeric(value) ~ as.factor(count) | dataset + model,
               data = all.data,
               panel = panel.superpose,
               groups=group,
               panel.groups = panel.ppc.plot
               )

plot(obj)

>
>
>
> ------------------------------
>
> Message: 119
> Date: Sat, 16 Aug 2008 08:14:36 +0000 (UTC)
> From: Dieter Menne <dieter.menne at menne-biomed.de>
> Subject: Re: [R] Lattice:  problem using panel.superpose and
> 	panel.groups
> To: r-help at stat.math.ethz.ch
> Message-ID: <loom.20080816T080831-746 at post.gmane.org>
> Content-Type: text/plain; charset=us-ascii
>
> Michael Braun <braunm <at> MIT.EDU> writes:
>
>>
>> I have some data that is split into two groups:  some "actual" data,
>> and some simulated data, generated from several different models.   
>> The
>> actual data come from two different datasets (calibration and
>> holdout), and the simulations were calibrated on each data set under
>> the various models.
>>
>> What I want to do is create a boxplot on the simulated data, and
>> superimpose a line representing the actual data.  This plot would
>> condition on dataset and model.  While the simulated data various
>> across model and dataset, the actual data varies only across dataset
>> and is common for all models.
>>
> Michael,
>
> to understand you problem, I started creating a data set that  
> simulates the
> data, but I got stuck with with your intended meaning of "groups.
>
> df =    data.frame(p=rnorm(100),count=rpois(100,5),dataset=c("A","B"),
>  model=rep(c("M1","M2"),each=50),groups= dont' know)
>
> Could you please (as the posting guide says) supply a self-running  
> example that
> shows the problem?
>
> You named your data.frame "data.frame"; it could work, but better  
> don't use
> common things like "c" or "data.frame" as variable name.
>
> Dieter
>
>



More information about the R-help mailing list