2011-01-11 91 views
0

我希望有新鮮眼睛的人能夠幫助我在這裏!我試圖發現一個實驗的力量,所以也做了以下內容: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和斜率值有關,但這可能是錯誤的?提前致謝。

+0

你願意縮進和註釋你的代碼嗎? – 2011-01-11 21:26:22

+1

您的泊松率(lambda)不能小於0. – 2011-01-11 22:06:03

回答

0

試試這個:之後for (j in 1:10) {放於browser() ...然後在命令行中輸入提交代碼之前:options(warn=2)

所以,當你得到一個錯誤或警告(我什麼,我認爲消息是),您可以檢查任何變量以獲取其當前值。警告的默認值爲1,完成後應將其設置回去。

(我懷疑,與你一起,你會發現,9 - ( - some_slope)* some_height給出了rpois呼叫負值(預期值rpois 必須是積極的,對嗎? )

1

你指出了錯誤get.p.value(-0.5)被發現。看裏面的功能顯示你正對這個(-0.5)值rpois,不能採取負數作爲參數。

我真的不知道是什麼你想這樣做,但是你可以用get.p.value(0.5)代替你的代碼。

祝你好運!

0

幾個意見:

在你有「分數」的glm功能,但沒有定義。你的意思是「社交」嗎? 在for循環中你有for(i in 1000),我想你的意思是for(i in 1:1000)。此外

p.vals[-0.5] <- get.p.value(-0.5) 

不超過ip.vals[]迭代表示索引...當你在談論的p.vals的-.5th元件,它沒有任何意義。

最後行

test.results$"P(>|Chi|)"[2] 

給我的3.16 * 10^-107的結果,這是真正的現實接近0。這對我來說很有意義,因爲我覺得你的代碼問:「如果我刪除模型中的所有信息,獲取任何信息的價值是什麼?「

我可以在這裏打基礎......但希望這可以幫助你!