我正在使用 R 中的镶嵌包教授一个基于模拟的推理(单比例测试)单元,我无法让 R 计算模拟比例大于或等于观察数据的模拟次数.
具体来说,我有一个问题,对 600 人进行了关于他们是否计划在 7 月 4 日点燃烟花的调查,56% 的人回答他们这样做。我想测试这个比例是否大于 p_0=0.5 的零假设。这是我当前的代码:
library(mosaic)
set.seed(25)
# Set up a data frame of 600 responses, 56% of which are "Fireworks", remaining "No Fireworks"
FireworksData <- c(rep("Fireworks",600*.56),rep("No Fireworks",600*(0.44)))
Fireworks_df <- data.frame('FourthOfJulyPlans'=FireworksData)
# Simulation: Run 1000 resamples of the data, and calculate proportion that are "Fireworks"
Fireworks.Null <- do(1000)*(prop( ~ FourthOfJulyPlans,
data = resample(Fireworks_df),
success="Fireworks")+(0.50-0.56))
# The +(0.50-0.56) at the end centers this distribution at p_0=0.5
#Count simulated proportions that meet or exceed 0.56, and calculate p-value
count(Fireworks.Null>=.56)
count(Fireworks.Null>=.56)/simulations
问题是 count(Fireworks.Null>=.56) 只找到大于 0.56 的值,但不等于:它返回有 2 个 case 满足或超过 0.56,但实际上有 3 个与此集合。种子()。第三种情况等于 0.56。我不知道为什么会这样:当我运行一个简单的测试示例时,我得到了正确的答案:
testvector <- c(0.57,0.57,0.57,0.56,0.56,0.55,0.55)
count(testvector>=0.56)
按预期返回 5。
任何帮助深表感谢!