這是隻有一個變量(教育)和可變答案(通過):
# get the prior probabilities
prior<- c(prior_no, prior_yes)
# get contingency table of values for mydata
edu_table<- with(mydata,table(mydata$pass, mydata$education))
# get the sum across (pass)
tots<- apply(edu_table,1,sum)
# create matrix of 0's
ppn<- edu_table*0
post<- edu_table*0
# use a loop to get the prior probabilities& posterior probabilities
for(i in 1:length(tots)){
for(j in 1: 4){
ppn[i,j]=edu_table[i,j]/tots[i]
post[i,j]=prior[i]*ppn[i,j]/(prior[1]*ppn[1,j]+prior[2]*ppn[2,j])
}
}
ppn # probability of education=j given y=i
post # posterior probability of y=i given education=j
撇開,這是一個不可思議的模糊問題。有*許多* R包做各種形式的貝葉斯估計; '庫( 「SOS」); findFn(「貝葉斯」)會給你太多的結果。這個問題可能會很快關閉,但如果你可以提供更多的上下文和細節,有人可能會幫助... –
我編輯了我的問題,希望它是明確 – meme
它好多了。你爲什麼不嘗試'(你的數據,tapply(年齡,教育程度,等級> 50),通過,平均)/平均(通過))'看看這是否給你任何想法? –