X =显示何时掷出三枚硬币的头数。
找到 P(X = 1)和 E(X)。
说,我想使用sample()
和R中的replicate()
函数解决这个问题,即使有一个名为rbinom()
的函数。
我的尝试
noOfCoinTosses = 3;
noOfExperiments = 5;
mySamples <-replicate(noOfExperiments,
{mySamples <- sample(c("H", "T"), noOfCoinTosses, replace = T, prob=c(0.5, 0.5))
})
headCount = length(which(mySamples=="H"))
probOfCoinToss <- headCount / noOfExperiments # 1.6
meanOfCoinToss = ??
关于 P(X),我是否处于正确的轨道?如果是,如何找到 E(X)?
答案 0 :(得分:2)
mySamples
中的结果存储每列的实验,因此您必须计算每列的head发生次数。这样,概率就是实验的频率/ nr,而在这种情况下,平均值就是频率:
noOfCoinTosses = 3;
noOfExperiments = 5;
mySamples <-replicate(noOfExperiments,
{mySamples <- sample(c("H", "T"), noOfCoinTosses, replace = T, prob=c(0.5, 0.5))
})
headCount <- apply(mySamples,2, function(x) length(which(x=="H")))
probOfCoinToss <- length(which(headCount==1)) / noOfExperiments # 1.6
meanOfCoinToss <- length(which(headCount==1))
要计算真实均值时,可以将其放入函数中并重复n
次。然后,平均值将成为复制的meanOfCoinToss