Re: [程式] R程式錯? 還是我哪裡錯?
修改一下您的程式,發現可能是dnorm這個函數定義域的問題,
或是integrate與dnorm兩函數結合時的問題.
可參考我修改您的程式如下,執行下面的程式碼即可看出端倪!
MU=c(10,20,30)
par(mfrow=c(1,length(MU)))
for(mu in MU){
B=function(y,mean=mu,sig=sqrt(0.24))
{
InteOut = integrate( function(x){ dnorm(x,mean,sig) },y,Inf)$value
InteOut
}
X=seq(mu-20,mu+5,.2); plot(X,sapply(X,B))
}
另外,您想寫的程式我修改如下,
只要您想求的P(X>x)中的x,不要小於mean太多,
應該是不會發生類似的問題!!
B=function(y,mean=11,sig=sqrt(0.24))
{
InteOut = integrate( function(x){ dnorm(x,mean,sig) },y,Inf)$value
InteOut
}
B(1)
B(0.1)
B(0.0610746)
B(0.0610745)
X=seq(9,13,.1); plot(X,sapply(X,B))
X=seq(-15,15,.2); plot(X,sapply(X,B))
如有錯誤,歡迎指教討論,謝謝!
※ 引述《aflilfesy (...)》之銘言:
: A=function()
: {
: InteOut = integrate( function(x)
: {
: dnorm(x,10,sqrt(0.24))
: },0,Inf)$value
: InteOut
: }
: A()
: 1-pnorm(0,10,sqrt(0.24))
: ----
: A() 結果是 1
: 1-pnorm(0,10,sqrt(0.24)) 結果是 1
: ----
: B=function()
: {
: InteOut = integrate( function(x)
: {
: dnorm(x,11,sqrt(0.24))
: },0,Inf)$value
: InteOut
: }
: B()
: 1-pnorm(0,11,sqrt(0.24))
: ----
: B() 結果是 3.707784e-05
: 1-pnorm(0,11,sqrt(0.24)) 結果是 1
: A()函數與B()函數的結果照理說要非常接近,但答案卻完全相反。
: 高手們,可以解釋一下 B()這地方結果怎麼變成這樣嗎?
: 我只有更改黃色部份而已
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.73.70.8
→
05/23 13:18, , 1F
05/23 13:18, 1F
→
05/23 13:19, , 2F
05/23 13:19, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):