Simon Kiss
2011-Nov-10 20:48 UTC
[R] Listing tables together from random samples from a generated population?
. HI there, I'd like to show demonstrate how the chi-squared distribution works, so I've come up with a sample data frame of two categorical variables y<-data.frame(gender=sample(c('Male', 'Female'), size=100000, replace=TRUE, c(0.5, 0.5)), tea=sample(c('Yes', 'No'), size=100000, replace=TRUE, c(0.5, 0.5))) And I'd like to create a list of 100 different samples of those two variables and the resulting 2X2 contingency tables table(.y[sample(nrow(.y), 100), ]) How would I combine these 100 tables into a list? I'd like to be able to go in and find some of the extreme values to show how the sampling distribution of the chi-square values. I can already get a histogram of 100 different chi-squared values that shows the distribution nicely (see below), but I'd like to actually show the underlying tables, for demonstration's sake. .z<-vector() for (i in 1:100) { .z<-c(.z, chisq.test(table(.y[sample(nrow(.y), 200), ]))$statistic) } hist(.z, xlab='Chi-Square Value', main="Chi-Squared Values From 100 different samples asking\nabout gender and tea/coffee drinking") abline(v=3.84, lty=2) Thank you in advance, Simon Kiss ********************************* Simon J. Kiss, PhD Assistant Professor, Wilfrid Laurier University 73 George Street Brantford, Ontario, Canada N3T 2C9 Cell: +1 905 746 7606
Jean V Adams
2011-Nov-10 22:30 UTC
[R] Listing tables together from random samples from a generated population?
You could try something like this: tables <- lapply(seq(100), function(i) table(.y[sample(nrow(.y), 200), ])) Then you could conduct the chi-squared tests chisqs <- lapply(tables, chisq.test) and save the values .z <- sapply(chisqs, "[[", "statistic") Jean --- Simon Kiss wrote on 11/10/2011 15:48:38: HI there, I'd like to show demonstrate how the chi-squared distribution works, so I've come up with a sample data frame of two categorical variables y<-data.frame(gender=sample(c('Male', 'Female'), size=100000, replace=TRUE, c(0.5, 0.5)), tea=sample(c('Yes', 'No'), size=100000, replace=TRUE, c(0.5, 0.5))) And I'd like to create a list of 100 different samples of those two variables and the resulting 2X2 contingency tables table(.y[sample(nrow(.y), 100), ]) How would I combine these 100 tables into a list? I'd like to be able to go in and find some of the extreme values to show how the sampling distribution of the chi-square values. I can already get a histogram of 100 different chi-squared values that shows the distribution nicely (see below), but I'd like to actually show the underlying tables, for demonstration's sake. .z<-vector() for (i in 1:100) { .z<-c(.z, chisq.test(table(.y[sample(nrow(.y), 200), ]))$statistic) } hist(.z, xlab='Chi-Square Value', main="Chi-Squared Values From 100 different samples asking\nabout gender and tea/coffee drinking") abline(v=3.84, lty=2) Thank you in advance, Simon Kiss Simon J. Kiss, PhD Assistant Professor, Wilfrid Laurier University 73 George Street Brantford, Ontario, Canada N3T 2C9 Cell: +1 905 746 7606 [[alternative HTML version deleted]]
Dennis Murphy
2011-Nov-11 06:20 UTC
[R] Listing tables together from random samples from a generated population?
Hi: There are two ways you could go about this: lists or arrays. It's pretty easy to generate an array, a little more work to get the list. I'm assuming the objective is to extract a chi-square statistic from each table, so I'll show a couple of ways to do that, too. library('plyr') ## Start with the data: y<-data.frame(gender=sample(c('Male', 'Female'), size=100000, replace=TRUE, c(0.5, 0.5)), tea=sample(c('Yes', 'No'), size=100000, replace=TRUE, c(0.5, 0.5))) ## Function to produce a table: tabfun <- function(d) table(d[sample(seq_len(nrow(d)), 100), ]) x2stat <- function(m) chisq.test(m)$statistic ## Array version: tbarr <- replicate(100, tabfun(y)) # X^2 statistics using apply() from base R and # aaply() from plyr: u1 <- apply(tablist, 3, x2stat) u2 <- aaply(tablist, 3, x2stat) ## List version: tblst <- vector('list', 100) for(i in seq_along(tblst)) tblst[[i]] <- tabfun(y) v1 <- unname(do.call(c, lapply(tblst, x2stat))) v2 <- laply(tblst, x2stat)>From here, it's easy to do the histogram :)HTH, Dennis On Thu, Nov 10, 2011 at 12:48 PM, Simon Kiss <sjkiss at gmail.com> wrote:> . > HI there, > I'd like to show demonstrate how the chi-squared distribution works, so I've come up with a sample data frame of two categorical variables > y<-data.frame(gender=sample(c('Male', 'Female'), size=100000, replace=TRUE, c(0.5, 0.5)), tea=sample(c('Yes', 'No'), size=100000, replace=TRUE, c(0.5, 0.5))) > > And I'd like to create a list of 100 different samples of those two variables and the resulting 2X2 contingency tables > > table(.y[sample(nrow(.y), 100), ]) > > How would I combine these 100 tables into a list? I'd like to be able to go in and find some of the extreme values to show how the sampling distribution of the chi-square values. > > I can already get a histogram of 100 different chi-squared values that shows the distribution nicely (see below), but I'd like to actually show the underlying tables, for demonstration's sake. > > ?.z<-vector() > for (i in 1:100) { > .z<-c(.z, chisq.test(table(.y[sample(nrow(.y), 200), ]))$statistic) > } > hist(.z, xlab='Chi-Square Value', main="Chi-Squared Values From 100 different samples asking\nabout gender and tea/coffee drinking") > abline(v=3.84, lty=2) > > Thank you in advance, > Simon Kiss > > ********************************* > Simon J. Kiss, PhD > Assistant Professor, Wilfrid Laurier University > 73 George Street > Brantford, Ontario, Canada > N3T 2C9 > Cell: +1 905 746 7606 > > ______________________________________________ > 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. >