R 不计数大于或等于随机数据正确

问题描述

我正在使用 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但不相等的值:它返回满足或超过0.56的情况有2个,但实际上有3个使用这个 set.seed()。第三种情况等于 0.56。我不知道为什么会这样:当我运行一个简单的测试示例时,我得到了正确的答案:

testvector <- c(0.57,0.57,0.56,0.55,0.55)
count(testvector>=0.56)

按预期返回 5。

非常感谢任何帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)