# Chi squared in R

I am trying to do a chi squared test in R but I ma not sure it is being done correctly. I am using the Titanic dataset from Tidyverse and seeing if there was an effect of age on survival rate. Counts of a survivors by class (crew, 1st, 2nd and 3rd), age (child or adult) and gender (male or female) are listed.

library(plyr)
library(dplyr)
library(Tidyverse)
ship=data.frame(Titanic) #making df

kids=ship %>% filter(Age == "Child") #function from dplyr, makes new dataframe using rows which match criteria listed
dead_kids=sum(dead_kids$$Freq) living_kids=kids %>% filter(Survived == "Yes") living_kids=sum(living_kids$$Freq)

dead_grown_ups=sum(dead_grown_ups$$Freq) living_grown_ups=grown_ups %>% filter(Survived == "Yes") living_grown_ups=sum(living_grown_ups$$Freq)

rownames(Survival_by_age) = c('Lived', 'Died')


if I run chisq.test() I get...

> chisq.test(Survival_by_age)

Pearson's Chi-squared test with Yates' continuity correction

data:  Survival_by_age
X-squared = 20.005, df = 1, p-value = 7.725e-06


But I am not sure if this is correct. if you print the values in the table you will see...

> Survival_by_age
Lived    654       57
Died    1438       52


And I do not think there is much of an effect there, certainly not with such a high significance score.

> chisq.test(Survival_by_age$Child, Survival_by_age$Adult)
Error in Survival_by_age$Child :$ operator is invalid for atomic vectors


So am I correctly interpretting the first test? And do I need to run this differently? Why am I getting that error when I run the chisq.test on the child and adult columns?

• There's a huge, obvious difference: this test is concerned about relative numbers, not absolute numbers, and the proportions of children in the rows differ by a factor of 2:1. What, then, are you hoping the chi-squared test will tell you?
– whuber
Commented Jan 21, 2021 at 18:40
• You are doing it wrong: Survival_by_age <- xtabs(Freq ~ Age + Survived, data = ship) then chisq.test(Survival_by_age).
– user291827
Commented Jan 21, 2021 at 21:09
• I was just using chisq because I am dealing with count data. I just wanted to see if the survival rate was significantly different between the children and the adults. As it is categorical data rather than by age I thought chisq would be best. Commented Jan 22, 2021 at 9:03
• The problem is not using chisq.test, it's calling it incorrectly. And reinventing the wheel instead of simply using xtabs.
– user291827
Commented Jan 22, 2021 at 9:46
• @Jean-ClaudeArbaut thank you very much for explaining this to me. I'm new to R, being forced to use it for a class. Personally I prefer pandas in python. Also been out of school for 3 years, my stats are weak very very weak haha. Thanks so much you've really helped me. Commented Jan 22, 2021 at 10:07

I agree with @whuber that results are clear without a formal test. However, it you are interested in checking the validity of your testing procedure in R, some results from R may be helpful.

Data:

liv = c(654, 57); die = c(1438, 52)
tot = liv + die
prop = liv/tot
prop
[1] 0.3126195 0.5229358


Test of binomial proportions.

You can do a test to see if the two binomial proportions of about 31% survival for adults and about 52% for children are statistically significant in view of sample sizes. In particular, you could do a prop.test in R, which shows a very highly significant difference with a P-value near $$0.$$ (The continuity correction is used on account of the small numbers of children)

prop.test(liv, tot)

2-sample test for equality of proportions
with continuity correction

data:  liv out of tot
X-squared = 20.005, df = 1, p-value = 7.725e-06
alternative hypothesis: two.sided
95 percent confidence interval:
-0.3109899 -0.1096426
sample estimates:
prop 1    prop 2
0.3126195 0.5229358


The P-value is not meaningfully different, without the continuity correction--still very near $$0:$$

prop.test(liv, tot, cor=F)$p.val [1] 4.700752e-06  Chi-squared test on 2-by-2 table. Alternatively, you could do a chi-squared test on a $$2 \times 2$$ table with a Yates correction. There is still a very high level of statistical significance with P-value near $$0.$$ TBL = rbind(liv,die); TBL [,1] [,2] liv 654 57 die 1438 52 chisq.test(TBL) Pearson's Chi-squared test with Yates' continuity correction data: TBL X-squared = 20.005, df = 1, p-value = 7.725e-06  Without Yates' correction, the P-value is as follows: chisq.test(TBL, cor=F)$p.val
[1] 4.700752e-06


In this example, P-values (with or without correction) for prop.test and chisq.test are in exact agreement. Children survived at a significantly higher rate than adults.

Note: Because I don't have access to your data frame and how you are using it, I can't say why the $-notation is causing an Error message. Strictly speaking, debugging R code is off-topic on this site, but maybe another user will see an obvious answer and comment. • +1. The error message issued by R is clear: Survival_by_age  is a matrix, which cannot be addressed using the $ operator used for list objects.
– whuber
Commented Jan 21, 2021 at 22:21
• @whuber thanks for clearing that up Commented Jan 22, 2021 at 10:12