【发布时间】:2020-06-03 03:36:18
【问题描述】:
所以我尝试从均匀分布中抽样 1000 次,每次计算来自所述均匀分布的 20 个随机样本的平均值。
Now let's loop through 1000 times, sampling 20 values from a uniform distribution and computing the mean of the sample, saving this mean to a variable called sampMean within a tibble called uniformSampleMeans.
{r 2c}
unif_sample_size = 20 # sample size
n_samples = 1000 # number of samples
# set up q data frame to contain the results
uniformSampleMeans <- tibble(sampMean = runif(n_samples, unif_sample_size))
# loop through all samples. for each one, take a new random sample,
# compute the mean, and store it in the data frame
for (i in 1:n_samples){
uniformSampleMeans$sampMean[i] = summarize(uniformSampleMeans = mean(unif_sample_size))
}
我成功生成了一个 tibble,但是值是“NaN”。此外,当我进入我的 for 循环时,我得到一个错误。
Error in summarise_(.data, .dots = compat_as_lazy_dots(...)) : argument ".data" is missing, with no default
任何见解将不胜感激!
【问题讨论】:
-
您在帖子的主体中说的是“正态分布”(两次),但使用
runif从 uniform 分布中采样。是不是打错字了? -
对于均匀分布的随机抽样 (
runif()),您需要三个参数:样本数n、最小值min和最大值max。您不能仅使用两个参数从runif()生成随机样本。 -
@AdamB.,
runif(1,-1)正常工作。 -
不知道,干杯。我只是意识到
runif(1)也有效。但这些似乎是非常具体的默认值 - 例如runif(1000, 20)不起作用,即使它起作用(使用与runif(1, -1)相同的逻辑),它也不会做@Salma Abdel-Raheem 似乎希望它做的事情 -
是的,这是一个错字,很抱歉它来自统一分布。我已经更新了我的帖子,以反映我的教授在作业中发布的问题。
标签: r loops dplyr normal-distribution