【发布时间】:2020-03-08 20:45:45
【问题描述】:
我正在使用重采样计算多个线性模型的系数。之前我是使用boot函数,但是以后需要在分析中加入新的统计数据所以我觉得这种方式比较好。一个可复制的例子:
iris <- iris[,1:4]
nboots <- 100
ncol = ncol(iris)
boot.r.squared <- numeric(nboots)
boot.p_model <- numeric(nboots)
boot.coef_p <- numeric(nboots)
boot.coef_estimate <- matrix(nrow= nboots,ncol=ncol)
boot.coef_error <- matrix(nrow= nboots,ncol=ncol)
boot.coef_t <- matrix(nrow= nboots,ncol=ncol)
boot.coef_p <- matrix(nrow= nboots,ncol=ncol)
for(i in 1:nboots){
boot.samp <- iris[sample(nrow(iris),size = 100, replace = TRUE,), ]
model <- lm(boot.samp$Sepal.Length ~ .,boot.samp)
model.sum <- summary(model)
boot.r.squared[i] <- model.sum$r.squared
stat <- model.sum$fstatistic
boot.p_model[i] <- pf(stat[1], stat[2], stat[3], lower.tail = FALSE)
boot.coef_estimate[i, 1:length(model$coefficients)] <- model$coefficients[1]
boot.coef_error[i, 1:length(model$coefficients)] <- model$coefficients[2]
boot.coef_t[i, 1:length(model$coefficients)] <- model$coefficients[3]
boot.coef_p[i, 1:length(model$coefficients)] <- model$coefficients[4]
}
但我无法正确保存矩阵形式的系数。我想保存 4 个矩阵,每个矩阵包含:参数、误差、统计 t 和 p 值。
使用此代码,所有列都相同。我尝试 put [,1] 保存第一列,但发生此错误。我该如何解决这个错误?
模型 $ 系数 [, 1] 中的错误:维数不正确
【问题讨论】:
标签: r regression resampling statistics-bootstrap