条件概率码在R中不起作用

n3ipq98p  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(128)

考虑一个游戏,其中包括滚动三个骰子计算结果和概率的事件。

#sample space
install.packages("combinat")
library(combinat)
install.packages("https://cran.r-project.org/src/contrib/Archive/prob/prob_0.9-2.tar.gz", repos = NULL, dependencies = TRUE)
library(prob)
Prob <- prob::prob
s<-rolldie(3,makespace=TRUE)

#a sum of the rolls is greater than 3 but less than 8.

(a_outcomes<-subset(s,X1+X2+X3>3 & X1+X2+X3<8))

(a_probability<-Prob(a_outcomes))
#sum(a_outcomes$probs)

#b All the three rolls are identical
(b_outcomes<-subset(s,X1==X2 &X2==X3))

(b_probability<-Prob(b_outcomes))

round(b_probability<-prob(b_outcomes),5)

#c Only two of the three rolls are identical.

(c_outcomes<-subset(s,X1==X2 & X2!=X3 | X1==X3 & X1!=X2 |X2==X3 &X2!=X1))

(c_probability<-Prob(c_outcomes))

#d None of the three rolls are identical
(d_outcomes<-subset(s,X1!=X2 & X2!=X3 & X3!=X1))

(d_probability<-Prob(d_outcomes))

#e 2 of the 3 rolls are identical given that sum of the rolls is greater than
# 3 and less than 8
e_outcomes(c_outcomes,a_outcomes)
e_outcomes

e_prob<-Prob(c_outcomes,given=a_outcomes)
e_prob

字符串
我得到e_outcomes的错误.我能够得到概率为e,但无法得到结果.什么是错误的代码?其余的代码都工作正常,除了e_outcomes.请帮助

v64noz0r

v64noz0r1#

如果你愿意采取不同的方法:
模拟:

library(matrixStats)
m <- matrix(sample(6, 3e7, 1), 1e7, 3)
mean(abs(rowSums(m) - 5.5) < 2 & rowMaxs(rowTabulates(m)) > 1)
#> [1] 0.1018894

字符串
通过置换的精确概率:

m <- RcppAlgos::permuteGeneral(6, 3, TRUE)
mean(abs(rowSums(m) - 5.5) < 2 & rowMaxs(rowTabulates(m)) > 1)
#> [1] 0.1018519

相关问题