我正在尝试使用模拟,从总体中抽取1到1000个样本,平均值为50,标准差为10。计算每个样本的平均值,并绘制一个图,显示该平均值随您的变化而变化增加样本量。根据该图,均值是总体均值的有偏估计或无偏估计?
以下是我所做的:
sd_uncorrected<-function(x){ return(sqrt(sum((x-mean(x))^2)/length(x)))
}
population <- rnorm(n = 1000, mean = 50, sd = 10)
population_mean <- mean(population)
population_std <- sd_uncorrected(population)
paste('population mean=',population_mean)
paste('population std = ', population_std)
sample_size <- 1000 # how many elements we want to sample
sample_n <- sample(population, size = sample_size, replace = FALSE)
sample_n
mean(sample_n)
sd_uncorrected(sample_n)
n_experiments <- 1000 # we will sample 1000 times
sample_size <- 10 # how many elements we want to sample?
sample_means <- c()
library(ggplot2)
sample_means_df <- data.frame(means=sample_means)
ggplot(sample_means_df, aes(x=means)) + geom_histogram() +
geom_vline(xintercept = population_mean, color='red') + # population mean
geom_vline(xintercept = mean(sample_means_df$means), color='black')
我收到以下错误消息,但我不知道该怎么办。有人可以帮我吗?
Error in FUN(X[[i]], ...) : object 'means' not found
In addition: Warning message:
In mean.default(sample_means_df$means) :
argument is not numeric or logical: returning NA
答案 0 :(得分:0)
也许应该写
sample_means = replicate(sample_size,
mean(sample(population, size = sample_size, replace = FALSE) ))
一行之后
sample_means <- c()
如评论中所述,您声明了一个不包含sample_means内容的向量