I run a regression model for two different models using the purrr package. The first model is "sales + cpi" and the second model is "sales + ndi". The dependent variable for both models is price. The code below show how i run the regressions of these two models across three different regions.
My question is how can I add coeftest() as a second step in the loop. This means that I would add coeftest() for each regression across the three regions. I show in the second step how to do it for one model.
I tried to include coeftest() using map2() in purrr package but I couldn't integrate it in the loop function. Could someone help?
In the first step below I show how to run the multiple regressions across regions:
library(plm)
library(lmtest)
library(broom)
library(purrr)
library(dplyr)
data(Cigar)
Cigar$region = rbinom(n=1380, size=3, prob=0.5)
Cigar$region = as.factor(Cigar$region)
model_by_region= Cigar %>%
ungroup() %>%
nest_by(region) %>%
mutate(model = list(map(c
( "sales + cpi",
"sales+ ndi"), ~
cbind(model = .x, tidy(plm(formula(paste0("price ~ ", .x)),
index=c("state", "year"), model="within", data = data), conf.int=TRUE)))))
In the second step i show how to run coeftest() for one model:
model<- plm(price ~ sales + cpi, index=c("state", "year"), model = 'within',
data = Cigar)
#Extract the robust standard errors
plot_coeftest = tidy(coeftest(model))
tidy(model) == tidy(coeftest(model)), you can test it on the last example above. Could you please elaborate a bit on your objective? - Waldimodel_by_region. My aim is to add robust standard error using thecoeftest()function for each of the 4 regression in step one. In the second step, i show how to do it for one model, but my aim is to do include thecoeftest()automatically across the four regions - Jad