我需要使用Rstudio中的N(0,1)来估计n = 10、30和100的样本均值,中位数,中位数和中四分位数的分布中心。
我编写的模拟运行时没有错误消息,但是当尝试从n = 10更改为n = 30时,我得到了有关分位数函数的错误,我认为这就是我的问题所在。
#N(0,1)
variab.dist.n = function(m = 1000, n = 10) {
average = c(); med = c(); midrange = c(); midquartile = c();
for(i in 1:m) {
x = rnorm(n)
average[i] = mean(x)
med[i] = median(x)
midrange[i] = ((min(x) + max(x)) / 2)
midquartile[i] = (quantile(variab.dist.n, 0.25) + quantile(variab.dist.n, 0.75))/2
}
data.frame(average, med, midrange, midquartile)
}
variab.dist.n.30 = variab.dist.n(n = 30)
attach(variab.dist.n.30)
boxplot(average, med, midrange, midquartile,
names = c("Mean", "Median", "Midrange", "Midquartile"),
ylab = "Estimate",
main = "Sampling Distributions for N(0,1)")
detach(variab.dist.200)
我希望使用n = 30和n = 100时,中心要比n = 10更精确。