我的任务是绘制10000个大小分别为n = 10、20、50、100、200和100的样本,取这些样本的均值和标准差,绘制每个样本的均值直方图,最后绘制样本标准偏差与样本量的关系。到目前为止,这是我的代码:
set.seed(1234)
N = 10000
s = 6
r = .6
sample1 = sample(N, 10)
sample2 = sample(N, 20)
sample3 = sample(N, 50)
sample4 = sample(N, 100)
sample5 = sample(N, 200)
sample6 = sample(N, 1000)
sample.mean1 = mean(sample1)
sample.mean2 = mean(sample2)
sample.mean3 = mean(sample3)
sample.mean4 = mean(sample4)
sample.mean5 = mean(sample5)
sample.mean6 = mean(sample6)
sample.means = c(sample.mean1, sample.mean2, sample.mean3, sample.mean4, sample.mean5, sample.mean6)
hist(rgamma(sample.mean1, shape = s, rate = r), breaks = 100, freq = FALSE)
hist(rgamma(sample.mean2, shape = s, rate = r), breaks = 100, freq = FALSE)
hist(rgamma(sample.mean3, shape = s, rate = r), breaks = 100, freq = FALSE)
hist(rgamma(sample.mean4, shape = s, rate = r), breaks = 100, freq = FALSE)
hist(rgamma(sample.mean5, shape = s, rate = r), breaks = 100, freq = FALSE)
hist(rgamma(sample.mean6, shape = s, rate = r), breaks = 100, freq = FALSE)
sample.sd1 = sd(sample.mean1)
sample.sd2 = sd(sample.mean2)
sample.sd3 = sd(sample.mean3)
sample.sd4 = sd(sample.mean4)
sample.sd5 = sd(sample.mean5)
sample.sd6 = sd(sample.mean6)
sample.sds = c(sample.sd1, sample.sd2, sample.sd3, sample.sd4, sample.sd5, sample.sd6)
sample.sizes = c(10, 20, 50, 100, 200, 1000)
plot(sample.sds, sample.sizes)
运行最后一行时,出现此错误:
Error in plot.window(...) : need finite 'xlim' values
将帮助解决此问题。
更正: 我实际上是要计算10000个样本中样本均值的标准偏差。然后的错误是标准偏差为NA。我的代码仍然是一样的,尽管现在我知道为什么会出现xlim错误了。现在,在获取每个样本均值的标准差并返回非NA值时,我需要帮助。