简体   繁体   English

如何模拟50个随机样本并计算每个样本的均值和方差

[英]How to simulate 50 random samples and calculate mean and variance of each sample

When I simulate 50 random samples of a normal distribution and try to calculate the mean and the variance of each simulation I got this error: "There were 50 or more warnings (use warnings() to see the first 50)". 当我模拟50个正态分布的随机样本并尝试计算每个模拟的均值和方差时,我收到此错误:“有50个或更多警告(使用警告()查看前50个)”。

n=100
mean=100
sd=25 

sample=NULL
meansample=NULL
sdsample=NULL

for (i in 1:50)
  {

  sample[i]=rnorm(n,mean,sd)
  meansample[i]=mean(sample[i])
  sdsample[i]=sd(sample[i])

  }

sample
meansample
sdsample`

I want to ask how do I calculate correctly the mean and the standard deviation and why I get this error "There were 50 or more warnings (use warnings() to see the first 50)" when I execute my code. 我想问我在执行代码时如何正确计算平均值和标准偏差,以及为什么收到此错误“有50个或更多警告(请使用warnings()查看前50个)”。

I personally would do that is way. 我个人会这样做。

x <- replicate(50, rnorm(100, 100, 25), simplify = FALSE)
sapply(x, mean)
sapply(x, sd)

The issue with your current code is sample is going to contain 50 separate sets of data, so you'd probably want that to be a list are refer to it as sample[[i]]. 当前代码的问题是样本将包含50个单独的数据集,因此您可能希望将其作为一个列表,将其称为sample [[i]]。

n=100
mean=100
sd=25 

sample=list()
meansample=NULL
sdsample=NULL

for (i in 1:50)
{

  sample[[i]]=rnorm(n,mean,sd)
  meansample[i]=mean(sample[[i]])
  sdsample[i]=sd(sample[[i]])

}

sample
meansample
sdsample 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM