gpt4 book ai didi

r - 伯努利试验的模拟测试以增加 R 中的样本量

转载 作者:行者123 更新时间:2023-12-04 09:46:34 24 4
gpt4 key购买 nike

在 R 软件中,我试图为不同样本量的伯努利试验生成一个模拟图,如下所示
Simulation of Bernoulli Trials for increasing sample size

我已经生成了给定样本大小的比例并重复了 1000 次这个过程。但我不明白,如何将图形绘制为给定的图形。

m=1000 ###### replications
n=10 ###### sample size
my=function(){
n=10 ######Trials
u=rbinom(n,1,0.5)
}
Simulated_data=t((replicate(m, my())))
Proportions<-apply(Simulated_data,1,sum)/n
Proportions

最佳答案

我想你想要的是这个。

R <- 50
n <- 1000

set.seed(42)
res <- replicate(R, rbinom(n, 1, .5))
p <- apply(res, 2, function(x) cumsum(x)/seq_along(x))
plot(1:1000, ylim=c(0, 1), type="n")
apply(p, 2, function(x) lines(1:1000, x, col="grey50"))
abline(h=.5)

enter image description here

关于r - 伯努利试验的模拟测试以增加 R 中的样本量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62085267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com