1

我正在尝试提高脚本的效率,基本上,我运行了许多线性回归,并且对于每个拟合模型,我将估计的系数和标准误差结果存储在先前创建的数据框中,例如results.

因此,在存储任何回归系数之前,数据框results已经具有所需的维度。

此外,对于i我所做的每一个回归:

mod.fit <- plm(y ~ x1 + x2, index="group", sample)

然后我运行:

  results[i,1] <- summary(m.fit)$coefficients[1,1]
  results[i,2] <- summary(m.fit)$coefficients[2,1]
  results[i,3] <- summary(m.fit)$coefficients[1,2]
  results[i,4] <- summary(m.fit)$coefficients[2,2]

有没有办法使上述存储步骤更快?

.

4

1 回答 1

6

您可以使用矩阵索引:

results[i,1:4] <- summary(m.fit)$coefficients[matrix(c(1,2,1,2,1,1,2,2),ncol=2)]

如果results只有 4 列宽,您可以消除1:4左侧的。

交替

results[i,] <- summary(m.fit)$coefficients[1:2,1:2]

应该可以工作,因为 R 以列优先顺序存储矩阵。

我鼓励你使用coef()访问器而不是$coefficients,如果它是为summary.plm类定义的......

于 2013-01-05T19:46:06.243 回答