# Condional Density Plot from different data

5 messages
Open this post in threaded view
|
Report Content as Inappropriate

## 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Condional Density Plot from different data

 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: [hidden email] [mailto:[hidden email]] On > Behalf Of Doran, Harold > Sent: Thursday, December 09, 2010 12:40 PM > To: [hidden email] > 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 > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Condional Density Plot from different data

 On Thu, Dec 9, 2010 at 10:11 AM, Doran, Harold <[hidden email]> wrote: > 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) Or, slightly shorter dat <- make.groups(datA, datB) densityplot(~scores|condition, groups = which, dat) > [...] >> library(lattice) >> densityplot(~datA\$scores + datB\$scores|datA\$condition) The problem here is that datA\$scores and datB\$scores don't have the same length. This requirement is implicit in the description of the extended formula interface:           'y1 + y2 ~ x | a * b' [...] would be taken to mean that the user wants to plot           both 'y1 ~ x | a * b' and 'y2 ~ x | a * b' [...] superposed in each panel. and the latter formulae would make sense only if x, a, b, and y1 (and y2) have the same length. -Deepayan ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Condional Density Plot from different data

 This post was updated on . Tried this and received this error: Error in hist.default(x = integer(0), plot = FALSE) :   invalid number of 'breaks' Edit: Nevermind, I restarted R and it works now.  Thank you!