如何从我的stargazer输出中省略回归公式?

jmo0nnb3  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(140)

我使用下面的代码来生成我的latex表:

data0 <- data.frame(temp_below18 = c(3,1,4,5,6,67,50,4,7,8),
                       temp_above33 = c(3,0,0,3,4,8,0,1,3,7),
                       female = c(0,0,0,1,1,1,1,0,1,0),
                       yeargc = c(2000,2010,2000,2010,2000,2010,2000,2010,2000,2010),
                       std_english = rnorm(10),
                       std_math = rnorm(10),
                       prec_0_0.01 =rnorm(10),
                       prec_0.01_0.02 = rnorm(10), 
                       prec_above0.02 = rnorm(10),
                       schoolname_unique = c(1,1,1,1,1,2,2,2,2,2),
                       stream = c(0,0,1,0,1,1,0,1,0,1))

formula4.em = paste0(" ~ female + temp_below18 + temp_above33 + 
                             prec_0_0.01 + prec_0.01_0.02 + 
                             prec_above0.02 | yeargc + stream + schoolname_unique | 0 | schoolname_unique")
formula4.em.gender = paste0(" ~ female*temp_below18 + female*temp_above33 + 
    prec_0_0.01 + prec_0.01_0.02 + 
    prec_above0.02 | yeargc + schoolname_unique + stream | 0 | schoolname_unique")

#regressing scores for each subject
for(subject in c("english","math")) {
  
  m11 <- lfe::felm(data = data0, 
                     as.formula(paste0("std_",subject,formula4.em)))

  
  assign(paste0("m11_",subject),m11)
  
  mean_outcome = round(mean(data0[[paste0("std_",subject)]], na.rm =T),2)
  
  assign(paste0("mean_",subject),mean_outcome)
  rm(mean_outcome)
  
}

#By gender

for(subject in c("math","english")) {
  
  new_mod = lfe::felm(data = data0, 
                      as.formula(paste0("std_",subject,formula4.em.gender))
  )
  
  assign(paste0("m11_",subject,".gender"),new_mod)
  rm(new_mod)
  
}

gender.vars.order <- c("temp_above33",
                       "temp_below18","female:temp_above33",
                       "female:temp_below18",
                       "female")
stargazer(m11_english,m11_english.gender,
          m11_math,m11_math.gender,
          type = "latex",
          header=FALSE,
          omit.stat=c("LL","ser","f","adj.rsq"), 
          font.size="tiny", 
          column.labels = c("English","Math"),
          column.separate = c(2, 2),
          model.names = FALSE,
          model.numbers = FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          add.lines=list(c("Outcome Mean:", 
                           mean_english,mean_english,
                           mean_math, mean_math),
                         c("Year FE", "X","X", "X","X"), 
                         c("School FE", "X","X", "X","X")),
          table.placement="H",
          order=paste0("^", gender.vars.order , "$"),
          keep = gender.vars.order,
          covariate.labels = c("Temp > 33C", "Temp < 18C","Female * Temp > 33C", 
                               "Female * Temp < 18C", "Female"),
          label = paste0("tab:mainsub"),
          out = paste0(out_filepath,"Tables/table.tex"),
          dep.var.caption = "Dependent Variable: Standardised Score")

我得到了以下输出,但无法从该tex文件中省略公式(带有'paste0 ...'的部分):

\begin{table}[H] \centering 
  \caption{} 
  \label{tab:mainsub} 
\tiny 
\begin{tabular}{@{\extracolsep{5pt}}lcccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{4}{c}{Dependent Variable: Standardised Score} \\ 
\cline{2-5} 
\\[-1.8ex] & paste0("std\_", subject, formula4.em) & paste0("std\_", subject, formula4.em.gender) & paste0("std\_", subject, formula4.em) & paste0("std\_", subject, formula4.em.gender) \\ 
 & \multicolumn{2}{c}{English} & \multicolumn{2}{c}{Math} \\ 
\hline \\[-1.8ex] 
 Temp > 33C & 0.424 & $-$0.410 & $-$0.077 & 0.353 \\ 
  &  &  &  &  \\ 
  & & & & \\ 
 Temp < 18C & 0.001 & 1.027 & 0.031 & $-$0.613 \\ 
  &  &  &  &  \\ 
  & & & & \\ 
 Female * Temp > 33C &  & 1.367 &  & $-$0.731 \\ 
  &  &  &  &  \\ 
  & & & & \\ 
 Female * Temp < 18C &  & $-$1.049 &  & 0.655 \\ 
  &  &  &  &  \\ 
  & & & & \\ 
 Female & $-$1.865 &  & 1.194 &  \\ 
  &  &  &  &  \\ 
  & & & & \\ 
\hline \\[-1.8ex] 
Outcome Mean: & -0.26 & -0.26 & 0.22 & 0.22 \\ 
Year FE & X & X & X & X \\ 
School FE & X & X & X & X \\ 
Observations & 10 & 10 & 10 & 10 \\ 
R$^{2}$ & 1.000 & 1.000 & 1.000 & 1.000 \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{4}{r}{$^{*}$p$<$0.05; $^{**}$p$<$0.01; $^{***}$p$<$0.001} \\ 
\end{tabular} 
\end{table}

我曾尝试过以下几种想法:
1.添加省略="公式"(根据ChatGPT的建议)
1.添加"空格数= T"
然而,这些都不能修复这个错误。有什么想法我可以修复这个错误吗?
谢谢大家!

rryofs0p

rryofs0p1#

似乎可以按如下方式自定义控制输出文本:

m11_english$call$formula[2] <- 'All'
m11_english.gender$call$formula[2] <- 'By Gender'
m11_math$call$formula[2] <- 'All'
m11_math.gender$call$formula[2] <- 'By Gender'

这会将先前生成的文本更改为自定义指定的字符串。在您的情况下,对于更多的模型,您可以在循环中最初更正标题,例如在赋值之前更正m11$call$formula[2] <- 'model group title'

相关问题