John Sorkin
2011-Apr-22 18:37 UTC
[R] Create 2x2 table from summary data and run chi square test.
R 2.12 windows 7 I am summary data that I would like to make into a 2x2 table representing counts positive vs. negative counts: 28/289 20/276 My table should look something like the following: group1 group2 Positive 28 20 Negative 289 276 How can a (1) create the 2x2 table (2) run a chi square test on the table? I have tried the following code, but I don't know if it is correct, and it does not give me an explicit 2x2 table:> xx <- c(28,20) > pp <- c(317/(317+296), 296/(317+296)) > chisq.test(xx,p=pp)Chi-squared test for given probabilities data: xx X-squared = 0.8425, df = 1, p-value = 0.3587 John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:6}}
Marc Schwartz
2011-Apr-22 18:44 UTC
[R] Create 2x2 table from summary data and run chi square test.
On Apr 22, 2011, at 1:37 PM, John Sorkin wrote:> > R 2.12 > windows 7 > > I am summary data that I would like to make into a 2x2 table representing counts positive vs. negative counts: > 28/289 20/276 > > My table should look something like the following: > > group1 group2 > Positive 28 20 > Negative 289 276 > > How can a (1) create the 2x2 table > (2) run a chi square test on the table? > > I have tried the following code, but I don't know if it is correct, and it does not give me an explicit 2x2 table: > >> xx <- c(28,20) >> pp <- c(317/(317+296), 296/(317+296)) >> chisq.test(xx,p=pp) > > Chi-squared test for given probabilities > > data: xx > X-squared = 0.8425, df = 1, p-value = 0.3587 >Hi John, This is just a matrix. Thus, if you have the counts: x <- matrix(c(28, 20, 289, 276), byrow = TRUE, 2, 2)> x[,1] [,2] [1,] 28 20 [2,] 289 276> chisq.test(x)Pearson's Chi-squared test with Yates' continuity correction data: x X-squared = 0.6491, df = 1, p-value = 0.4204 or as some might prefer: # No Yates correction, which is conservative> chisq.test(x, correct = FALSE)Pearson's Chi-squared test data: x X-squared = 0.9141, df = 1, p-value = 0.339 HTH, Marc Schwartz