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?
Survival_by_age <- xtabs(Freq ~ Age + Survived, data = ship)
thenchisq.test(Survival_by_age)
. $\endgroup$