【问题标题】:Get Regression Coefficient Names with R Bootstrap使用 R Bootstrap 获取回归系数名称
【发布时间】:2017-01-22 21:20:49
【问题描述】:

我在 R 中使用 boot 包来计算自举 SE 和置信区间。我试图找到一种优雅而有效的方法来获取我的参数名称以及它们的估计值的引导分布。例如,考虑给定here 的简单示例:

# Bootstrap 95% CI for regression coefficients 
library(boot)
# function to obtain regression weights 
bs = function(data, indices, formula) {
    d = data[indices,] # allows boot to select sample 
    fit = lm(formula, data=d)
    return(coef(fit))
}
# bootstrapping with 1000 replications 
results = boot(
    data=mtcars, 
    statistic=bs, 
    R=1000, 
    formula=mpg~wt+disp)

这很好用,只是结果只是显示为数字索引:

# view results
results
Bootstrap Statistics :
       original        bias    std. error
t1* 34.96055404  0.1559289371 2.487617954
t2* -3.35082533 -0.0948558121 1.152123237
t3* -0.01772474  0.0002927116 0.008353625

特别是当涉及到涉及各种因子变量的冗长复杂的回归公式时,可能需要一些工作来准确跟踪哪些指数与哪些系数估计值相符。

我当然可以在引导函数之外再次重新拟合我的模型,并使用names(coef(fit)) 或其他内容提取名称,或者可能使用其他内容,例如调用model.matrix()。这些看起来很麻烦,无论是在额外的编码方面,还是在额外的 CPU 和内存资源方面。

在这种情况下,如何更轻松地获得系数名称的漂亮向量来配对系数标准误差向量?

更新

根据 lmo 的出色回答,这是我获取基本回归表的基本代码:

Names = names(results$t0)
SEs = sapply(data.frame(results$t), sd)
Coefs = as.numeric(results$t0)
zVals = Coefs / SEs
Pvals = 2*pnorm(-abs(zVals))

Formatted_Results = cbind(Names, Coefs, SEs, zVals, Pvals)

【问题讨论】:

  • attr(results$t0, "names") ;从看str(results)

标签: r statistics-bootstrap


【解决方案1】:

调用“bootstrapped”函数(此处为lm)对原始数据的估计值存储在名为“t0”的列表元素中。

results$t0
(Intercept)          wt        disp 
34.96055404 -3.35082533 -0.01772474

此对象保留原始函数调用的估计名称,然后您可以使用 names 访问。

names(results$t0)
[1] "(Intercept)" "wt"          "disp"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2014-11-28
    • 1970-01-01
    • 2017-08-15
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多