【发布时间】:2021-08-09 06:08:46
【问题描述】:
我想一次使用一个预测变量来拟合回归模型。我总共有 7 个预测变量和 1 个响应变量。我想编写一段代码,从数据框中选择一个预测变量并拟合一个模型。我还想提取回归系数(不是截距)和它的符号,并将它们存储在 2 个向量中。这是我的代码-
for (x in (1:7))
{
fit <- lm(distance ~ FAA_unique_with_duration_filtered[x] , data=FAA_unique_with_duration_filtered)
coeff_values<-summary(fit)$coefficients[,1]
coeff_value<-coeff_values[2]
append(coeff_value_vector,coeff_value , after = length(coeff_value_vector))
append(RCs_sign_vector ,sign(coeff_values[2]) , after = length(RCs_sign_vector))
}
这里 x in 将使用第一列,然后是第二列,依此类推。但是,我收到以下错误。
model.frame.default 中的错误(公式 = 距离 ~ FAA_unique_with_duration_filtered[x],: 变量“FAA_unique_with_duration_filtered[x]”的类型(列表)无效
有没有办法使用循环来做到这一点?
【问题讨论】:
标签: r loops vector regression linear-regression