【问题标题】:Bootstrapping Proportions of Categorical Variables in R or StataR 或 Stata 中分类变量的自举比例
【发布时间】:2013-08-14 17:52:28
【问题描述】:

在 R 或 Stata 软件中进行引导时,我需要帮助。我想计算说是和否的人的比例,例如政策的有效性

在 Stata 我有这个代码

bs "summarize y1" "r(mean)", reps(200) size(770)

r(mean) 的值应该是多少才能估计比例?

另外,我在 R 中有这段代码:

test <- function (q13){
    test13 <- table(q13)
    rel_freq <- test13/sum(test13)
    return(rel_freq)
      }

results <- boot(data=q13, statistic=test,
                R=200)

如何更正代码?我收到了错误

统计错误(数据,原始,...):未使用的参数(原始)

【问题讨论】:

  • 对于 R,请仔细阅读 ?boot 中的示例 - 您的函数 test 需要至少接受两个参数,其中第二个应该是一组索引,用于重新采样基础数据。

标签: r stata statistics-bootstrap


【解决方案1】:

Stata 中,如果变量有两个以上的类别,您可以使用 proportion

//样本数据

sysuse auto, clear
keep if (headroom==2.0 |headroom==2.5)
gen prop=.
replace prop=0 if headroom==2.0
replace prop=1 if headroom==2.5

//说0是是,1是不是

set seed 123
bootstrap _b, reps(100):proportion prop

根据@Nick 更新:对于二进制变量,以下就足够了

bootstrap r(mean), reps(100): summarize prop, meanonly

.................................................. ..................................................... ..................................................... .....................

R 中,您可以使用boot 包和 mtcars 数据执行以下操作:

library(boot)
set.seed(123)
x<-mtcars$vs
myprop<-function(x,i){
sum(x[i]==0)/length(x)
}

bootprop <- boot(x,myprop,100)

【讨论】:

  • 如果一个变量是二进制的,那么在Stata中summarize, meanonly返回1的比例为r(mean)
  • 感谢尼克考克斯和@Metrics!我运行 R 代码但我没有得到结果。我收到“引导统计:警告:t1* 的所有值都是 NA”这是我想做引导的示例数据摘要。数据有缺失值。我不确定这是否会影响结果。 > describe(q10testfactor) q10testfactor n 缺少唯一 254 516 2 0 (58, 23%), 1 (196, 77%)
  • 这很奇怪。我将 R 值增加到 R=5000。我得到了标准值。错误,但估计仍然有 NA。调用:boot(data = x, statistic = myprop, R = 5000) Bootstrap Statistics:原始偏差标准。错误 t1* NA NA 0.01670186
  • 当然,您必须首先处理丢失的数据,因为您是从数据中采样的。你用Stata跑了吗?
  • 谢谢! :-D 不知道该怎么做。但现在我知道了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 2022-01-14
  • 2011-07-28
  • 2023-03-10
  • 1970-01-01
  • 2016-03-02
  • 2020-04-10
相关资源
最近更新 更多