楼主: janyiyi
1193 0

Stacking Regressions: Latex Tables with R and stargazer [推广有奖]

  • 3关注
  • 17粉丝

讲师

27%

还不是VIP/贵宾

-

威望
0
论坛币
3206 个
通用积分
5056.6800
学术水平
539 点
热心指数
537 点
信用等级
538 点
经验
10157 点
帖子
300
精华
2
在线时间
90 小时
注册时间
2010-10-3
最后登录
2024-4-6

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

In my paper on the impact of the shale oil and gas boom in the US, I run various instrumental variables specifications. For these, it is nice to stack the regression results one on the other – in particular, to have one row for the IV results, one row for the Reduced Form and maybe one row for plain OLS to see how the IV may turn coefficients around.

I found that as of now – there is no way to do that directly; please correct me if I am wrong.

The layout I have in mind is as in the screenshot of my table.

In Stata, this can be accomplished through the use of esttab with the fragment, and append feature. This appends successive rows to an existing table and you can label the rows using the “refcat” option.

However, in R this is not possible as of yet. I have mainly worked with stargazer, as Marek has added felm objects for high dimensional fixed effects to be handled by his stargazer package.

The following functions are a “hack” that extracts the particular rows from the generated latex code by the stargazer package.

You can then piece the table together by combining the individual elements. The idea is that you have a single stargazer command that is passed to the various functions that extract the different features. Obviuously, this can be done a lot more elegant as the code is extremely hacky, but it does work

Marek has said that he is thinking of incorporating a stacking option into stargazer, but for now, my hack works reasonably well. The key thing to realise is that stargazer calls have the option to return

The following character strings can be used in the table.layout and omit.table.layout arguments of the stargazer command.
“-”single horizontal line
“=”double horizontal line
“-!”mandatory single horizontal line
“=!”mandatory double horizontal line
“l”dependent variable caption
“d”dependent variable labels
“m”model label
“c”column labels
“#”model numbers
“b”object names
“t”coefficient table
“o”omitted coefficient indicators
“a”additional lines
“n”notes
“s”model statistics


The following functions will simply extract the rows that are being returned.

  1. stargazer.keepcolnums<-function(call="") {
  2. command<-gsub("\\)$",", table.layout='#'\\)",command)
  3. call<-eval(parse(text=command))
  4. bounds<-grep("tabular",call)
  5. row1<- call[(bounds[1]+1):(bounds[2]-1)]
  6. row1<-gsub("^\\\\\\\\\\[-1\\.8ex\\]\\\\hline $", "", row1)
  7. row1<-gsub("^\\\\hline \\\\\\\\\\[-1\\.8ex\\] $","",row1)
  8. row1<-row1[row1!=""]
  9. }
  10. stargazer.keeprow<-function(call="") {
  11. command<-gsub("\\)$",", table.layout='t'\\)",command)
  12. call<-eval(parse(text=command))
  13. bounds<-grep("tabular",call)
  14. row1<- call[(bounds[1]+1):(bounds[2]-1)]
  15. row1<-gsub("^\\\\\\\\\\[-1\\.8ex\\]\\\\hline $", "", row1)
  16. row1<-gsub("^\\\\hline \\\\\\\\\\[-1\\.8ex\\] $","",row1)
  17. row1<-row1[row1!=""]
  18. }

  19. stargazer.keepstats<-function(call="") {
  20. command<-gsub("\\)$",", table.layout='s'\\)",command)
  21. call<-eval(parse(text=command))
  22. row1<-gsub("(.*)\\\\\\\\\\[-1\\.8ex\\](.*)(\\\\end\\{tabular\\})(.*)","\\2",paste(call,collapse=" "))
  23. row1
  24. }
  25. stargazer.begintable<-function(call="") {
  26. command<-gsub("\\)$",", table.layout='m'\\)",command)
  27. call<-eval(parse(text=command))
  28. row1<-paste("\\begin{tabular}",gsub("(.*)\\\\begin\\{tabular\\}(.*)(\\\\end\\{tabular\\})(.*)","\\2",paste(call,collapse="\n")),sep="")
  29. row1
  30. }
  31. stargazer.varlabels<-function(call="") {
  32. command<-gsub("\\)$",", table.layout='d'\\)",command)
  33. call<-eval(parse(text=command))
  34. row1<-paste("\\begin{tabular}",gsub("(.*)\\\\begin\\{tabular\\}(.*)(\\\\end\\{tabular\\})(.*)","\\2",paste(call,collapse="\n")),sep="")
  35. row1
  36. }

  37. stargazer.keepcollabels<-function(call="") {
  38. command<-gsub("\\)$",", table.layout='c'\\)",command)
  39. call<-eval(parse(text=command))
  40. row1<-gsub("(.*)\\\\\\\\\\[-1\\.8ex\\](.*)(\\\\end\\{tabular\\})(.*)","\\2",paste(call,collapse=" "))
  41. row1
  42. }

  43. stargazer.keepomit<-function(call="") {
  44. command<-gsub("\\)$",", table.layout='o'\\)",command)
  45. call<-eval(parse(text=command))
  46. row1<-gsub("(.*)\\\\\\\\\\[-1\\.8ex\\](.*)(\\\\end\\{tabular\\})(.*)","\\2",paste(call,collapse="\n"))
  47. row1
  48. }It easiest to see how you can use these functions to construct stacked regression output by giving a simple example.###the global command to be passed to the hacky ###functions that extract the individual bits
  49. ## OLS is a list of OLS results from running the
  50. ## lfe command
  51. command<-'stargazer(OLS , keep=c("post08shale:directutilityshare","post08shale"), covariate.labels=c("Energy Intensity x Shale","Shale"), header=FALSE, out.header=FALSE, keep.stat=c("n","adj.rsq"))'

  52. begintable<-stargazer.begintable(command)
  53. ###some multicolumn to combine
  54. collcombine<-c("& \\multicolumn{4}{c}{Tradable Goods Sector Only} & \\multicolumn{3}{c}{Additional Sectors} & \\cmidrule(lr){2-5} \\cmidrule(lr){6-8}")
  55. collabel<-stargazer.keepcollabels(command)
  56. colnums<-stargazer.keepcolnums(command)
  57. ##plain OLS row
  58. row1<-stargazer.keeprow(command)
  59. ##the stats part for the OLS (number of Obs, R2)
  60. stats<-stargazer.keepstats(command)
  61. ##the rows for the Fixed effect indicators
  62. omitted<-stargazer.keepomit(command)

  63. ##the IV command passing a list of IV results in ## IV object
  64. command<-'stargazer(IV , keep=c("post08anywell:directutilityshare","post08anywell"), covariate.labels=c("Energy Intensity x Anywell","Anywell"), header=FALSE, out.header=FALSE, keep.stat=c("n","adj.rsq"))'

  65. ##IV row
  66. row2<-stargazer.keeprow(command)
  67. footer<-c("\\end{tabular}\n" )

  68. ###now combine all the items
  69. cat(begintable,collcombine,collabel,colnums,"\\hline \\\\\\emph{Reduced Form} \\\\",row1,""\\hline \\\\\\emph{Reduced Form} \\\\",row2,stats,"\\hline\\hline",footer, file="Draft/tables/energyintensity.tex", sep="\n")
复制代码

I know the solution is a bit hacky, but it works and does the trick.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Regressions regression regressio regress tables particular directly correct please impact

已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
oliyiyi + 80 + 40 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 80  论坛币 + 40  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-24 00:17