[R] Listing tables together from random samples from a generated population?
Simon Kiss
sjkiss at gmail.com
Thu Nov 10 21:48:38 CET 2011
.
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
More information about the R-help
mailing list