2017-04-22 61 views
1

我想計算結果的後驗概率,但找不到合適的函數。如何計算貝葉斯結果

這是我的代碼,我學會了從here

prior = c(D = .0001, not.D = 1 - .0001) 
like.D = c(pos=.99, neg=.01) 
like.not.D = c(pos=.01, neg=.99) 
likelihood = rbind(D = like.D, not.D = like.not.D) 
data="pos" 
bayes(prior, likelihood, data) 

但我無法找到「貝葉斯」的功能,所以我希望有人來幫助我。

回答

0

函數的鏈接(在您提供的鏈接處)似乎已經死亡,因此您需要聯繫頁面作者。在這種情況下,計算起來並不困難,因爲後驗=可能*之前。所以只需通過乘以表來計算。 (?病)(?測試)給出POS可能性似乎所需的計算是d的概率

> joint = likelihood * prior 
> # marginalise to calculate posterior (need to normalise) 
> cs = colSums(joint) 
> joint[,"pos"]/cs["pos"] 
      D  not.D 
0.009803922 0.990196078 

作爲替代方案,你可以鏡框爲貝葉斯網絡(疾病 - >試驗)使用gRain

> library(gRain) 
> d = cptable(~disease, values = prior, levels=names(prior)) 
> tt = cptable(~test+disease, values=t(likelihood), levels=colnames(likelihood)) 
> net = grain(compileCPT(list(d, tt))) 
> net2 = setEvi(net, evidence=list(test="pos")) 
> querygrain(net2) 
$disease 
disease 
      D  not.D 
0.009803922 0.990196078