chi squared - Why 2 outputs of chisq.test are different in R -
in following, why outputs of 2 chisq.test different, when data same:
> df1 count position 1 1 11 2 6 12 3 12 13 4 23 14 5 27 15 > df2 count position 1 1 11 2 4 12 3 9 13 4 24 14 5 24 15 > mm = merge(df1, df2, by='position') > mm position count.x count.y 1 11 1 1 2 12 6 4 3 13 12 9 4 14 23 24 5 15 27 24
first method:
> chisq.test(mm[2:3]) pearson's chi-squared test data: mm[2:3] x-squared = 0.6541, df = 4, p-value = 0.9569 warning message: in chisq.test(mm[2:3]) : chi-squared approximation may incorrect
second method:
> chisq.test(df1$count, df2$count) pearson's chi-squared test data: df1$count , df2$count x-squared = 15, df = 12, p-value = 0.2414 warning message: in chisq.test(df1$count, df2$count) : chi-squared approximation may incorrect >
edit: responding comment: following identical:
> mm[2:3] count.x count.y 1 1 1 2 6 4 3 12 9 4 23 24 5 27 24 > > mm[,2:3] count.x count.y 1 1 1 2 6 4 3 12 9 4 23 24 5 27 24
data:
> dput(df1) structure(list(count = c(1l, 6l, 12l, 23l, 27l), position = 11:15), .names = c("count", "position"), class = "data.frame", row.names = c(na, -5l)) > dput(df2) structure(list(count = c(1l, 4l, 9l, 24l, 24l), position = 11:15), .names = c("count", "position"), class = "data.frame", row.names = c(na, -5l))
see ?chisq : in first case, mm[2:3] taken contingency table, in second case, contingency table computed.
chisq.test(table(df1$count, df2$count)) pearson's chi-squared test data: table(df1$count, df2$count) x-squared = 15, df = 12, p-value = 0.2414 warning message: in chisq.test(table(df1$count, df2$count)) : chi-squared approximation may incorrect
so, really, calculated chisq of table :
1 4 9 24 1 1 0 0 0 6 0 1 0 0 12 0 0 1 0 23 0 0 0 1 27 0 0 0 1
Comments
Post a Comment