简体   繁体   中英

Efficient regression coefficients storage in an existing data frame

I am trying to increase the efficiency of a script where, basically, I run a number of linear regressions and for each fitted model I store the estimated coefficients and standard errors results in a previously created data frame, say results .

Hence the data frame results is already with the required dimensions before storing any regression coefficient.

Also, for each i -th regression I make:

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

and then I run:

  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]

Is there a way for making the above storage step faster?

.

You could use matrix indexing:

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

If results is only 4 columns wide you could eliminate the 1:4 on the left side.

Alternately

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

should work, because R stores matrices in column-first order.

I would encourage you to use the coef() accessor rather than $coefficients , if it is defined for the summary.plm class ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM