r-星际聚集器中的重新排序变量



当我试图重新排序变量时,我遇到了一些问题。首先,我定义了一个字符向量,指示我想要的顺序。

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

然后我称之为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")

以下是乳胶代码。

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}

事实证明,输出中的顺序没有改变。我的星际聚集器中的订单代码无效。我可以手动更改Texstudio的订单。但是,如果我们可以在星际合伙人中做到这一点,那会更好。

您没有提供可再现的示例,但这是一个带有虚拟数据的解决方案。

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现在按默认顺序。假设,我想以此顺序获取我的参数:

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

值得注意的是,观星参数 order是相对于观星输出的默认顺序。默认情况下,观星将截距位于底部,而它首先是parameters

order作为参数传递到星际合伙人,如果我们设置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

相关内容

  • 没有找到相关文章

最新更新