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]]
______________________________________________
[email protected] 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.