Search code examples
rstargazer

Reorder variables in stargazer


I got some problem when I was trying to reorder variables in stargazer. First, I defined a character vector indicating the order I want.

> order
[1] "poly(log(fall_t), 2)1"         "poly(log(fall_t), 2)2"         "poly(winter_t, 2)1"            "poly(winter_t, 2)2"           
[5] "poly(log(spring_t), 2)1"       "poly(log(spring_t), 2)2"       "poly(log(fall_p), 2)1"         "poly(log(fall_p), 2)2"        
[9] "poly(log(winter_p), 2)1"       "poly(log(winter_p), 2)2"       "poly(log(spring_p), 2)1"       "poly(log(spring_p), 2)2"      
[13] "log(aot_fall)"                 "log(aot_winter)"               "log(aot_spring)"               "poly(log(aot_fall), 2)1"      
[17] "poly(log(aot_fall), 2)2"       "log(aot_fall):log(fall_t)"     "poly(log(aot_spring), 2)1"     "poly(log(aot_spring), 2)2"    
[21] "log(aot_spring):log(spring_t)" "poly(log(aot_winter), 2)1"     "poly(log(aot_winter), 2)2"     "log(aot_winter):winter" 

Then I called stargazer

stargazer(pmclimatevft, pmftsea, pmftqsea, pmftint, title = "Panel data with fixed time effect poly",
          model.names = F, model.numbers = F, object.names = F, dep.var.caption = "Yield", dep.var.labels.include = F,
          column.labels = c("Only climate variables","linear","quartic","Intersection"),
          omit = c("Year"), omit.labels = c("Time fixed effect"),
          order = order,
          dep.var.labels = "Yield", digits = 3, notes = "*Average and square temperature of Winter are not in log form due to negative values.",
          align = T, no.space = T, column.sep.width = "-10pt", omit.stat = "f")

Below is the LaTex code.

\begin{table}[!htbp] \centering 
  \caption{Panel data with fixed time effect poly} 
  \label{} 
\begin{tabular}{@{\extracolsep{-10pt}}lD{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} } 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{4}{c}{Yield} \\ 
\cline{2-5} 
 & \multicolumn{1}{c}{Only climate variables} & \multicolumn{1}{c}{linear} & \multicolumn{1}{c}{quartic} & \multicolumn{1}{c}{Intersection} \\ 
\hline \\[-1.8ex] 
 poly(log(fall\_t), 2)1 & -8.096^{***} & -7.946^{***} & -8.030^{***} & -10.271 \\ 
  & (1.173) & (1.184) & (1.187) & (9.071) \\ 
  poly(log(fall\_t), 2)2 & 0.443 & 0.350 & 0.534 & 0.340 \\ 
  & (0.312) & (0.323) & (0.329) & (0.372) \\ 
  poly(winter\_t, 2)1 & -7.065^{***} & -6.683^{***} & -6.879^{***} & 1.615 \\ 
  & (0.899) & (0.907) & (0.926) & (4.048) \\ 
  poly(winter\_t, 2)2 & -1.160^{***} & -1.072^{***} & -1.057^{***} & -1.073^{***} \\ 
  & (0.279) & (0.280) & (0.280) & (0.280) \\ 
  poly(log(spring\_t), 2)1 & -9.967^{***} & -10.428^{***} & -10.028^{***} & -19.885^{**} \\ 
  & (1.095) & (1.106) & (1.110) & (8.014) \\ 
  poly(log(spring\_t), 2)2 & -0.867^{***} & -0.924^{***} & -0.753^{***} & -0.996^{***} \\ 
  & (0.269) & (0.274) & (0.276) & (0.288) \\ 
  poly(log(fall\_p), 2)1 & 2.270^{***} & 2.431^{***} & 2.511^{***} & 2.337^{***} \\ 
  & (0.421) & (0.428) & (0.427) & (0.432) \\ 
  poly(log(fall\_p), 2)2 & -3.553^{***} & -3.582^{***} & -3.522^{***} & -3.653^{***} \\ 
  & (0.247) & (0.249) & (0.249) & (0.253) \\ 
  poly(log(winter\_p), 2)1 & 2.693^{***} & 2.630^{***} & 2.635^{***} & 2.519^{***} \\ 
  & (0.585) & (0.587) & (0.586) & (0.591) \\ 
  poly(log(winter\_p), 2)2 & -1.832^{***} & -1.786^{***} & -1.934^{***} & -1.775^{***} \\ 
  & (0.262) & (0.262) & (0.264) & (0.265) \\ 
  poly(log(spring\_p), 2)1 & 0.106 & 0.196 & 0.246 & 0.207 \\ 
  & (0.415) & (0.416) & (0.418) & (0.417) \\ 
  poly(log(spring\_p), 2)2 & -4.917^{***} & -4.793^{***} & -4.889^{***} & -4.747^{***} \\ 
  & (0.256) & (0.259) & (0.259) & (0.261) \\ 
  log(aot\_fall) &  & 0.049 &  & -0.081 \\ 
  &  & (0.033) &  & (0.461) \\ 
  log(aot\_winter) &  & -0.048^{**} &  & -0.038^{*} \\ 
  &  & (0.020) &  & (0.021) \\ 
  log(aot\_spring) &  & 0.090^{***} &  & -0.294 \\ 
  &  & (0.032) &  & (0.315) \\ 
  poly(log(aot\_fall), 2)1 &  &  & 0.372 &  \\ 
  &  &  & (0.571) &  \\ 
  poly(log(aot\_fall), 2)2 &  &  & -0.751^{**} &  \\ 
  &  &  & (0.302) &  \\ 
  poly(log(aot\_winter), 2)1 &  &  & -0.553 &  \\ 
  &  &  & (0.587) &  \\ 
  poly(log(aot\_winter), 2)2 &  &  & 1.321^{***} &  \\ 
  &  &  & (0.303) &  \\ 
  poly(log(aot\_spring), 2)1 &  &  & 0.868 &  \\ 
  &  &  & (0.618) &  \\ 
  poly(log(aot\_spring), 2)2 &  &  & -0.382 &  \\ 
  &  &  & (0.305) &  \\ 
  log(aot\_fall):log(fall\_t) &  &  &  & 0.052 \\ 
  &  &  &  & (0.180) \\ 
  log(aot\_winter):winter\_t &  &  &  & -0.013^{**} \\ 
  &  &  &  & (0.006) \\ 
  log(aot\_spring):log(spring\_t) &  &  &  & 0.152 \\ 
  &  &  &  & (0.128) \\ 
 \hline \\[-1.8ex] 
Time fixed effect & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\ 
\hline \\[-1.8ex] 
Observations & \multicolumn{1}{c}{2,979} & \multicolumn{1}{c}{2,979} & \multicolumn{1}{c}{2,979} & \multicolumn{1}{c}{2,979} \\ 
R$^{2}$ & \multicolumn{1}{c}{0.641} & \multicolumn{1}{c}{0.642} & \multicolumn{1}{c}{0.645} & \multicolumn{1}{c}{0.643} \\ 
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.621} & \multicolumn{1}{c}{0.623} & \multicolumn{1}{c}{0.625} & \multicolumn{1}{c}{0.623} \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{4}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
 & \multicolumn{4}{r}{*Average and square temperature of Winter are not in log form due to negative values.} \\ 
\end{tabular} 
\end{table}

It turned out that the order in the output did not change. My order code in stargazer did not work. I can manually change the order in TeXstudio. But it would be better if we can do that in stargazer.


Solution

  • You did not provide a reproducible example, but here is a solution with dummy data.

    library(stargazer)
    # silly lm with a lot of parameters
    lm(Sepal.Length ~  poly(log(Sepal.Width),4) +
                       poly(log(Petal.Length),5) + 
                       Petal.Width:Species, data=iris) -> foo
    
                   
    
    names(coef(foo)) -> parameters
    parameters
    > parameters
     [1] "(Intercept)"                   "poly(log(Sepal.Width), 4)1"
     [3] "poly(log(Sepal.Width), 4)2"    "poly(log(Sepal.Width), 4)3"
     [5] "poly(log(Sepal.Width), 4)4"    "poly(log(Petal.Length), 5)1"
     [7] "poly(log(Petal.Length), 5)2"   "poly(log(Petal.Length), 5)3"
     [9] "poly(log(Petal.Length), 5)4"   "poly(log(Petal.Length), 5)5"
    [11] "Petal.Width:Speciessetosa"     "Petal.Width:Speciesversicolor"
    [13] "Petal.Width:Speciesvirginica"
    

    parameters is now in default order. Let's say, I want to get my parameters in this order:

    desiredOrder <- c("poly(log(Sepal.Width), 4)4", "Petal.Width:Speciesvirginica",
    "poly(log(Sepal.Width), 4)1", "Petal.Width:Speciesversicolor",
    "poly(log(Petal.Length), 5)1", "poly(log(Sepal.Width), 4)3",
    "Petal.Width:Speciessetosa", "poly(log(Petal.Length), 5)3", "(Intercept)",
    "poly(log(Petal.Length), 5)4", "poly(log(Petal.Length), 5)2",
    "poly(log(Sepal.Width), 4)2", "poly(log(Petal.Length), 5)5")
    
    order <- match(desiredOrder, parameters)
    >order
     [1]  5 13  2 12  6  4 11  8  1  9  7  3 10
    

    It is worth noticing that the stargazer parameter order is relative to the default order of stargazer output. By default, stargazer places intercept in the bottom, whereas it is first in parameters.

    Passing order as a parameter to stargazer prints out the regression coefficients in desired order, if we set intercept.top=TRUE, intercept.bottom=FALSE.

    stargazer(foo, order=order, type='text', intercept.top=TRUE, intercept.bottom=FALSE)
    
    
    =========================================================
                                      Dependent variable:
                                  ---------------------------
                                         Sepal.Length
    ---------------------------------------------------------
    poly(log(Sepal.Width), 4)4               0.033
                                            (0.313)
    
    Petal.Width:Speciesvirginica           -0.376**
                                            (0.151)
    
    poly(log(Sepal.Width), 4)1             2.660***
                                            (0.464)
    
    Petal.Width:Speciesversicolor           -0.205
                                            (0.202)
    
    poly(log(Petal.Length), 5)1            13.503***
                                            (1.792)
    
    poly(log(Sepal.Width), 4)3              -0.030
                                            (0.319)
    
    Petal.Width:Speciessetosa                0.251
                                            (0.435)
    
    poly(log(Petal.Length), 5)3             1.108**
                                            (0.481)
    
    Constant                               6.167***
                                            (0.195)
    
    poly(log(Petal.Length), 5)4             -0.065
                                            (0.344)
    
    poly(log(Petal.Length), 5)2            3.828***
                                            (0.521)
    
    poly(log(Sepal.Width), 4)2              0.745**
                                            (0.337)
    
    poly(log(Petal.Length), 5)5              0.388
                                            (0.381)
    
    ---------------------------------------------------------
    Observations                              150
    R2                                       0.876
    Adjusted R2                              0.865
    Residual Std. Error                0.304 (df = 137)
    F Statistic                    80.690*** (df = 12; 137)
    =========================================================
    Note:                         *p<0.1; **p<0.05; ***p<0.01