[R] Condional Density Plot from different data

Doran, Harold HDoran at air.org
Thu Dec 9 18:40:19 CET 2010


I'm not certain I am using the lattice plot correctly here. Below is reproducible code. Suppose I have two data frames, such as: 

set.seed(1234)
datA <- data.frame(condition = gl(3, 100), scores = c(rnorm(100), rnorm(100, 1,1), rnorm(100, 2,1)))
datB <- data.frame(condition = gl(3, 1000), scores = c(rnorm(1000, 3,1), rnorm(1000, 4,1), rnorm(1000, 5,1)))

I would like to plot the empirical conditional densities (conditional on the variable "condition") in a fashion such as:

library(lattice)
densityplot(~datA$scores + datB$scores|datA$condition)

However, I don't think this is doing what I think it should be doing. For instance, if I compare it to:

AA <- subset(datA, condition == '1')
BB <- subset(datB, condition == '1')
plot(density(AA$scores))
lines(density(BB$scores))

Things are clearly different. Can anyone identify my error in the trellis plot code?

Thank you,
Harold


> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lattice_0.19-13

loaded via a namespace (and not attached):
[1] grid_2.12.0



More information about the R-help mailing list