R For 循环 - 回归 - 矩阵

R For Loop - Regression - Matrix

我想弄清楚如何制作一个进行 10 次回归然后将其放入 10x11 矩阵的 for 循环。

这是我正在使用的代码,但由于某种原因它无法正常工作...

for (i in 1:10) {
 Pwdlm.list[[i]] = 
 lm(Pwd[,9+i]~logwprice1+logwprice2+logwprice3+logwprice4+
    logwprice5+logwprice6+logwprice7+logwprice8+logwprice9+logwprice10+
    Pwd$storeid, data=Pwd)
}
Pwdmat = matrix(Pwdlm.list[[i]]$coefficients, nrow = 10, ncol = 11)

我不断收到此警告:

Warning message: In matrix(Pwdlm.list[[i]]$coefficients, nrow = 10, ncol = 11) : data length [25] is not a sub-multiple or multiple of the number of rows [10]

但我需要 10 行的数据。

我知道没有看到数据很难,但我不确定它是否与 [9+i]

有关

您似乎没有从 lm 调用或对 Pwdlm.list 的赋值中收到错误。试试这个:

 Pwdmat = sapply(Pwdlm.list, coefficients)

当所有返回值的长度都相等时,sapply 函数会尝试返回一个矩阵。每个回归的值都将在一列中,这似乎是您希望获得的形式。