2
$\begingroup$

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=kids %>% filter(Survived == "No")
dead_kids=sum(dead_kids$Freq)
living_kids=kids %>% filter(Survived == "Yes")
living_kids=sum(living_kids$Freq)

grown_ups=ship %>% filter(Age == "Adult")
dead_grown_ups=grown_ups %>% filter(Survived == "No")
dead_grown_ups=sum(dead_grown_ups$Freq)
living_grown_ups=grown_ups %>% filter(Survived == "Yes")
living_grown_ups=sum(living_grown_ups$Freq)


Survival_by_age <- matrix(c(living_grown_ups,living_kids,dead_grown_ups,dead_kids),ncol=2,byrow=TRUE)
colnames(Survival_by_age) = c('Adults', 'Children')
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
      Adults Children
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?

$\endgroup$
5
  • 1
    $\begingroup$ 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? $\endgroup$
    – whuber
    Commented Jan 21, 2021 at 18:40
  • 1
    $\begingroup$ You are doing it wrong: Survival_by_age <- xtabs(Freq ~ Age + Survived, data = ship) then chisq.test(Survival_by_age). $\endgroup$
    – user291827
    Commented Jan 21, 2021 at 21:09
  • $\begingroup$ 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. $\endgroup$ Commented Jan 22, 2021 at 9:03
  • $\begingroup$ The problem is not using chisq.test, it's calling it incorrectly. And reinventing the wheel instead of simply using xtabs. $\endgroup$
    – user291827
    Commented Jan 22, 2021 at 9:46
  • $\begingroup$ @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. $\endgroup$ Commented Jan 22, 2021 at 10:07

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ +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. $\endgroup$
    – whuber
    Commented Jan 21, 2021 at 22:21
  • 1
    $\begingroup$ @whuber thanks for clearing that up $\endgroup$ Commented Jan 22, 2021 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.