0
votes

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))
1
No sure to understand the question as tidy(model) == tidy(coeftest(model)), you can test it on the last example above. Could you please elaborate a bit on your objective? - Waldi
In the first step i ran 4 different regression models - each regression by region. You can check them in model_by_region. My aim is to add robust standard error using the coeftest() 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 the coeftest() automatically across the four regions - Jad
See my extended comment posted as an answer. I'll delete it if I misunderstood your question. - Waldi

1 Answers

2
votes

This could be as simple as:

model_by_country_with_coef_test = Cigar %>%
  ungroup()  %>%
  nest_by(region) %>%
  mutate(model = list(map(c
                          (  "sales + cpi",
                            "sales+ ndi"), ~
                            cbind(model = .x, tidy(coeftest(plm(formula(paste0("price ~ ", .x)), 
                                                        index=c("state", "year"), model="within", data = data)), conf.int=TRUE
                            )))))




model_by_country_with_coef_test[[3]][[1]]
[[1]]
        model  term   estimate  std.error statistic      p.value   conf.low  conf.high
1 sales + cpi sales -0.4716673 0.04863656 -9.697793 6.831817e-17 -0.5679327 -0.3754019
2 sales + cpi   cpi  1.0209728 0.02336462 43.697376 3.542571e-77  0.9747277  1.0672179

[[2]]
       model  term     estimate    std.error statistic      p.value     conf.low   conf.high
1 sales+ ndi sales -0.267552595 0.0500205394 -5.348855 4.111431e-07 -0.366557254 -0.16854794
2 sales+ ndi   ndi  0.008491882 0.0001944991 43.660261 3.911187e-77  0.008106914  0.00887685