[R] Condional Density Plot from different data

Doran, Harold HDoran at air.org
Thu Dec 9 19:11:12 CET 2010


Perhaps I found a solution as:

datA$type <- gl(1, nrow(datA), label = 'PopA')
datB$type <- gl(1, nrow(datB), label = 'PopB')
dat <- rbind(datA, datB)
densityplot(~scores|condition, group = type, dat)

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Doran, Harold
> Sent: Thursday, December 09, 2010 12:40 PM
> To: r-help at r-project.org
> Subject: [R] Condional Density Plot from different data
> 
> 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
> 
> ______________________________________________
> 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