【问题标题】:Using a simulation in R to test coverage probability of a confidence interval使用 R 中的模拟来测试置信区间的覆盖概率
【发布时间】:2019-01-16 01:11:04
【问题描述】:

我有一个作业问题,要求我使用 R 中的模拟来测试置信区间的覆盖概率(作为上一个问题的一部分)。

我的代码试图从我拥有的样本数据中生成 1000 个随机样本(带替换),有效地将我的原始样本视为我的新总体。随机样本与我的总体大小相同。然后,我想找到每个随机样本的 95% 置信区间,并查看有多少包含“真实均值”(在问题陈述中给出)与“总体均值”(我的原始样本的均值)。

set.seed(1987)

iq <- rnorm(1000,91.08065,14.40393)

pop_mean <- mean(iq) #the mean of my sample is now considered the population mean
true_mean <- 100 #the true mean is 100, specified in question

sampSEs <- numeric() #create an empty vector to put the sample SEs in
sampMeans <- numeric() #create an empty vector to put the sample means in

get_conf_interval <- function(sample_measurements) {
  iqSE_samp <- 15/sqrt(length(iq)) #find the SE using an sd of 15
  iqMean_samp <- mean(sample_measurements) #take the mean of each sample
  upper <- iqMean_samp + 1.96*iqSE_samp #find the upper bound for a 95% CI
  lower <- iqMean_samp - 1.96*iqSE_samp #find the lower bound for a 95% CI
  list(lower=lower, upper=upper)
}

interval_contains_true_mean <- function(interval) { #check if the interval contains the true mean
  true_mean >= interval$lower && true_mean <= interval$upper
}
interval_contains_population_mean <- function(interval) { #check if the interval contains the population mean
  pop_mean >= interval$lower && pop_mean <= interval$upper
}

samples <- replicate(1000, sample(iq, size = 124, replace = T)) #take 1000 samples with replacement from my iq data

for(i in 1:1000) { #for each sample taken
  sampMeans[i] <- mean(samples[i]) #put the mean of it in the vector created previously
  sampSEs[i] <- 15/sqrt(length(iq)) #put the SE in a vector... these are all the same bc not finding the sample sd
}

intervals <- apply(samples, FUN=get_conf_interval, MARGIN=2) #call the function to find the confidence intervals

sampMeans #just check if worked
#sampSEs #ditto

percent_intervals_with_true_mean <- mean(sapply(intervals, FUN=interval_contains_true_mean)) * 100
cat("% Intervals Containing True Mean: ", percent_intervals_with_true_mean, "%\n")

percent_intervals_with_pop_mean <- mean(sapply(intervals, FUN=interval_contains_population_mean)) * 100
cat("% Intervals Containing Population Mean: ", percent_intervals_with_pop_mean, "%")

此代码报告我的样本的 0% 置信区间包含真实均值。这是不正确的;我查看了样本均值,其中有几个是真实均值。

【问题讨论】:

  • 你的iq 是什么?提供定义(例如iq &lt;- rnorm(1000,100,15))以使其可重现。当我在这样的iq 上运行您的代码时,我得到您的0% 答案。请提供minimal reproducible example
  • 对不起,我在作业中使用的 IQ 数据是一个单独的 csv 文件。我在上面的代码中添加了一条语句,它使用我文件中值的平均值和标准差生成 IQ 值,因为我认为我无法附加它。希望这对测试没问题?使用此代码,我得到 55.4% 的区间包含我的总体均值,但仍有 0% 的区间包含真实均值。有了这些新数据,我的随机样本的平均值都不是正好 100,但有些非常接近(例如 100.12716),所以我仍然认为这不是正确的。
  • 但是现在,既然你的iq 的平均值在91.08065 左右,为什么你认为“真正的平均值”是100?您的编辑使问题变得不那么有意义。
  • 这是一个更大的任务的一部分,这个样本(我的原始 csv)是从高铅暴露区域中选择的。在这种情况下,真正的平均值是美国人口的平均智商,我不希望它是这个样本的平均值。我不希望 100 在从该池中抽取的大多数样本的置信区间内,但我希望它在其中的非零百分比中。如果结构/术语令人困惑,我很抱歉,我只是想将其删减以发布,因为这大约是真实内容的 1/20。
  • 从该总体中抽取大小为 124 的样本时,我不希望 100 在 1000 个置信区间中的任何一个中。样本均值的预期值约为 91,标准偏差约为 15/sqrt(124),约为 1.3。请注意,9/1.3 接近 7,因此您的“真实均值”比预期样本均值高 6 到 7 个标准差。

标签: r confidence-interval


【解决方案1】:

1.- 我有两个解决方案,第一个解决方案是把逗号放在 'mean(samples[,i])' 和

'set.seed(1987)

sigma_M=14.40393

mu_M=91.08065

m=10

iq

pop_mean

samples

sampSEs

sampMeans

for(i in 1:m) { #for each sample

sampMeans[i]

sampSEs[i]

get_conf_interval

iqSE_samp

iqMean_samp

upper

lower

列表(下=下,上=上) }

interval_contains_population_mean

pop_mean >= interval$lower && pop_mean

intervals

sampMeans #只检查是否有效

sampSEs #ditto

percent_intervals_with_pop_mean

cat("% 包含人口平均值的区间:", percent_intervals_with_pop_mean, "%")'

2.- 第二种解决方案是更改代码,但我只做了“pop_mean”人口平均值 (并计算标准偏差)

'set.seed(1987)

sigma_M=14.40393

mu_M=91.08065

m=10

iq

pop_mean

samples

sampMeans=apply(samples, 2, mean)

iqSE_samp

iqMean_samp

upper

lower

intervals=cbind(下、上)

percent_intervals_with_pop_mean=mean(apply(intervals, 1, findInterval, x = pop_mean) == 1)*100

cat("% 包含人口平均值的区间:", percent_intervals_with_pop_mean, "%")'

决赛对我来说是80分

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2018-01-16
    • 2021-01-07
    • 2013-08-31
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多