[英]R regression with factor: get coefficient for levels, not as difference from Intercept?
[英]Get Regression Coefficient Names with R Bootstrap
我正在R中使用boot
程序包来计算引导的SE和置信区间。 我试图找到一种优雅而有效的方法来获取参数名称以及其估计值的引导分布。 例如,考虑给出的简单例子在这里 :
# 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
特别是当涉及涉及各种因子变量的冗长而复杂的回归公式时,可能需要花一些工夫才能准确跟踪哪些指数与哪个系数估计一起使用。
我当然可以只在bootstrap函数之外重新拟合我的模型,并使用names(coef(fit))
东西提取名称,或者可能使用诸如调用model.matrix()
其他东西。 在额外的编码方面,在额外的CPU和内存资源方面,这些都显得很麻烦。
在这种情况下,如何更容易地获得系数名称的良好向量来与系数标准误差向量配对?
UPDATE
基于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)
在原始数据上调用“启动捆绑”功能(此处为lm
的估算值存储在名为“ t0”的列表中。
results$t0
(Intercept) wt disp
34.96055404 -3.35082533 -0.01772474
此对象保留原始函数调用中的估算值的名称,然后可以使用names
进行访问。
names(results$t0)
[1] "(Intercept)" "wt" "disp"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.