1
votes

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?

.

1
You probably want to pre-allocate the df.results so it is on the required length to begin with, rather than grown with each iteration. That said, if you gave more details about what you are trying to do, there is probably a more efficient solution - James
Calling summary() 4 times per loop is silly. If you had to do this, do sm <- summary(m.fit) and then four calls of sm$coefficients[x,y]. - Gavin Simpson
(I don't think summary is even needed.) - MattBagg
@James, the dataframe where to store results is already of the required length. Also, I do not refit each model four times, I edited the question, sorry - Stefano Lombardi
@MattBagg No, I don't either, but it isn't 100% clear what m.fit is so... But if it is of class "lm" then you'll need a little more than just coef(m.fit), but not much more than sqrt(diag(vcov(m.fit))). - Gavin Simpson

1 Answers

6
votes

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 ...