我希望有新鮮眼睛的人能夠幫助我在這裏!我試圖發現一個實驗的力量,所以也做了以下內容:R電源與信號強度代碼
height <- seq(0, 151)
social <- rpois(length(height), 9 + 0.2 * (height))
m2 <- glm(score ~ height, family = poisson)
summary(m2)
m3 <- update(m2, ~. - height)
anova(m2, m3, test = "Chi")
test.results <- anova(m2, m3, test = "Chi")
names(test.results)
test.results$"P(>|Chi|)"
test.results$"P(>|Chi|)"[2]
get.p.value <- function(slope) {
social <- rpois(length(height), 9 + slope * (height))
m2 <- glm(score ~ height, family = poisson)
m3 <- update(m2, ~. - r.hand)
anova(m2, m3, test = "Chi")$"P(>|Chi|)"[2]
}
p.vals <- numeric(1000)
for (i in 1000) {
p.vals[-0.5] <- get.p.value(-0.5)
}
p.vals
power.of.test <- length(p.vals[p.vals < 0.05])/length(p.vals)
power.of.test
slope.line <- seq(-0.2, -1.1, -0.1)
p.vals <- numeric(100)
power.of.test <- numeric(10)
for (j in 1:10) {
for (i in 1:100) p.vals[i] <- get.p.value(slope.line[j])
power.of.test[j] <- length(p.vals[p.vals < 0.05])/length(p.vals)
}
plot(slope.line, power.of.test)
然而,這將產生:
In rpois(length(height), 9 + slope * (height)) : NAs produced
我顯然犯了愚蠢的錯誤的地方,並花了一整天重新輸入它以確保我不會錯過括號等,但一切似乎都是按順序的。我有一種感覺,這與我從glm獲得的9和斜率值有關,但這可能是錯誤的?提前致謝。
你願意縮進和註釋你的代碼嗎? – 2011-01-11 21:26:22
您的泊松率(lambda)不能小於0. – 2011-01-11 22:06:03