【发布时间】:2020-05-26 20:25:39
【问题描述】:
# Bootstrap 95% CI for R-Squared
library(boot)
# function to obtain R-Squared from the data
rsq <- function(formula, data, indices) {
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=rsq,
R=1000, formula=mpg~wt+disp)
# get 95% confidence interval
boot.ci(results, type="bca")
假设您运行此引导程序并获得 1000 个对截距、重量和分布变量的估计值,因此您希望将所有估计值放入数据框中。
dataframe = data.frame(results$t)
这样就可以了,但是您如何对其进行编码以确保列名获得正确的变量名?我是这样做的,它使列名“Var1”、“Var2”和“Var3”,但我希望它们是“拦截”、“重量”和“重量”,我知道我可以把它们改成这样;我想知道如何自动化它以确保列从启动时获得正确的名称。
【问题讨论】:
-
您可能需要
names(dataframe) <- names(results$t0) -
根据您拥有的功能,
t是一个没有名称属性的矩阵,而t0即从 'rsq` 返回的具有names并且您可以分配名称基于此