找到了R中卡方的意义

时间:2015-02-21 19:57:01

标签: r kruskal-wallis dunn.test

我需要从变量中提取/复制Kruskal-Wallis chi square 的重要性( p-value )来自dunn.test函数dunn.test包,但不会返回。问题是:如果我有 chi square 和相关的自由度,那么可以计算 p值以及如何计算?

我的数据:

dat <- structure(list(IQ_social_1 = c(2, 5, 6, 6, 5, 6, 7, 4, 6, 6, 3, 8, 8, 6, 8, 7, 3, 10, 2, 7, 4, 7, 6, 5, 6, 5, 8, 10, 6, 9, 6, 7, 6, 9, 7, 7, 9, 9, 7, 7, 5, 7, 5, 6, 9, 8, 5, 9, 8, 7, 6, 5, 8, 8, 6, 5, 10, 6, 6, 7, 6, 3, 9, 9, 5, 7, 8, 6, 6, 5, 7, 6, 7, 5, 6, 6, 8, 5, 4, 5, 7, 5, 3, 6, 6, 6, 4, 5, 5, 2, 5, 2, 5, 6, 8, 5, 6, 6, 4, 5, 2, 7, 2, 7, 4, 8, 7, 7, 6, 4, 5, 6, 5, 5, 9, 7, 3, 7, 5, 2, 7, 6, 8, 4, 8, 7, 4, 11, 7, 4, 8, 7, 9, 8, 8, 8, 11, 11, 9, 6, 8, 7, 6, 7, 9, 4, 9, 7, 8, 10, 8, 4, 6, 8, 4, 8, 4, 6, 5, 4, 6, 5, 7, 5, 7, 9, 4, 6, 6, 1, 5, 9, 4, 7, 5, 6, 6, 5, 5, 5, 4, 4, 6, 8, 5, 5, 7, 7, 7, 7, 6, 4, 8, 8, 6, 7, 6, 11, 7, 7, 4, 3, 7, 5, 4, 7, 7, 5, 9, 4, 9, 4, 6, 6, 4, 2, 3, 7, 6, 6), IS_nivel = structure(c(2L, 1L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 2L, 2L, 1L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 1L, 3L, 1L, 1L, 2L, 2L, 3L, 1L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 1L, 3L, 3L, 1L, 3L, 2L, 1L, 1L, 2L, 1L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 3L, 3L, 1L, 3L, 1L, 3L, 3L, 2L, 1L, 2L, 3L, 2L, 3L, 1L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 3L, 2L, 1L, 3L, 2L, 3L, 3L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 2L, 1L, 2L, 3L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 3L, 1L), .Label = c("1", "2", "3"), class = "factor")), .Names = c("IQ_social_1", "IS_nivel"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L, 220L, 221L), class = "data.frame")

我的代码:

kw <- dunn.test(dat[[1]], as.factor(dat[[2]]), method = "sidak")
kw <- kw$chi2
kw <- cbind(colnames(dat[1]), kw, length(levels(dat[[2]]))-1, "")
kw <- as.data.frame(kw)
colnames(kw) <- c("Variable", "chi2", "df", "p")
kw

1 个答案:

答案 0 :(得分:0)

感谢@ ben-bolker和@dickoa,这是我的问题的答案:

kw <- dunn.test(dat[[1]], as.factor(dat[[2]]), method = "sidak", kw = FALSE)
chi2 <- kw$chi2
df <- length(levels(dat[[2]]))-1
P <- pchisq(chi2, df,lower.tail=FALSE)
kw <- cbind(colnames(dat[1]), chi2, df, P)
kw <- as.data.frame(kw)
colnames(kw) <- c("Variable", "chi2", "df", "p")
kw