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

问题描述 投票:0回答:1

考虑一个涉及掷三个骰子的游戏 计算结果和该事件的概率。

#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 之外,其余代码都工作正常。请帮忙

r statistics
1个回答
0
投票

如果您愿意采取不同的方法:

模拟:

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
© www.soinside.com 2019 - 2024. All rights reserved.