首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包括星图表上的注释,扭曲柱间的距离

包括星图表上的注释,扭曲柱间的距离
EN

Stack Overflow用户
提问于 2020-06-12 18:56:43
回答 1查看 1K关注 0票数 1

我试图打印一些表,我的plm模型使用星体包。一切都很好,直到我想把笔记加到我的桌子上。不幸的是,当我添加注释时,输出表被扭曲了,每列之间都有很大的空白。有人能帮我修好吗?提前感谢!

注意:我还在学习Latex,所以欢迎一个同样适用于html的解决方案

代码语言:javascript
复制
#Here I create a simple data.frame just to replicate my data
vector <- rep(0,72)

for (i in 1:72) {
  vector[i] <- rnorm(1)
}

data <- as.data.frame(matrix (vector, nrow = 7))

#This outputs a normal table, with normal space width between columns
stargazer(data,
          type = "text",
          style = "AER")

#This is the note I want to print. However, it returns an error if I space it out like that.
stargazer(data,
          type = "text",
          notes = c("All models were estimated by fixed effects estimator. Errors are robust to heteroscedasticity and 
                    autocovariance. Numbers between parenthesis are the coefficient standard deviations. * represents 
                    significance at the 10 % level, ** represents significance at the 5% level, and *** represents significance 
                    at the 1% level. Models (1), (2) and (3) refer, respectively, to equations (7), (8) and (9)"),
          style = "AER")


#This outputs the table. However, there are big blank spaces between columns. How do I fix this?
stargazer(data,
          type = "text",
          notes = c("All models were estimated by fixed effects estimator. Errors are robust to heteroscedasticity and autocovariance. Numbers between parenthesis are the coefficient standard deviations. * represents significance at the 10 % level, ** represents significance at the 5% level, and *** represents significance at the 1% level. Models (1), (2) and (3) refer, respectively, to equations (7), (8) and (9)"),
          style = "AER")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-16 08:46:58

LaTeX解决方案

我也有过类似的问题,这是我对LaTeX的解决方案。

首先,您需要一个单独的函数来创建自己的小页面,其中包括便笺。

代码语言:javascript
复制
library(stargazer)

stargazernote <- function(starGazerCmd, outputfile, note){
  # inserts 'note' to the end of stargazer generated latex table
  ssn <- gsub(x=starGazerCmd, pattern='\\end{tabular}',
              replacement=paste('\\end{tabular}', '\n',
                                '\\centerline{\\begin{minipage}{0.95\\textwidth}~\\', '\n',
                                '\\footnotesize{' , note, 
                                '} \\end{minipage}}',  sep=''), fixed=T)

  cat(ssn,sep='\n',file=outputfile)
}

(您可能需要使用参数{0.95\\textwidth}。有些人喜欢音符稍宽或窄的音符。

创建您的星神乳胶表没有注意事项如下:

代码语言:javascript
复制
s <- stargazer(data,
          type = "latex",
          style = "AER",
          notes.append=FALSE,
          )

然后分别创建注释:

代码语言:javascript
复制
note <- "All models were estimated by fixed effects estimator. Errors are robust to heteroscedasticity and autocovariance. Numbers between parenthesis are the coefficient standard deviations. * represents significance at the 10 \\% level, ** represents significance at the 5\\% level, and *** represents significance at the 1\\% level. Models (1), (2) and (3) refer, respectively, to equations (7), (8) and (9)"

这是第一个问题:%符号被LaTeX解释为评论符号,所以您需要用\\作为%的前缀。

现在,您可以使用上面的函数来整理备注和LaTeX表:

代码语言:javascript
复制
stargazernote(s, note=note, outputfile='foo.tex')

输出如下所示:

第二个问题是您需要记住在LaTeX文件中包含graphicx。下面是一个.tex文件的样子:

代码语言:javascript
复制
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\usepackage{graphicx} %% note: the table will not render without this package. 

\begin{document}
\include{foo.tex}
\end{document}

文本解决方案

您可以在这样的注释中强制行更改(这是从this SO question复制的):

代码语言:javascript
复制
stargazer(data,
          type = "text",
          notes= c("All models were estimated by fixed effects estimator.", 
                    "Errors are robust to heteroscedasticity and autocovariance.", 
                    "Numbers between parenthesis are the coefficient standard deviations.", 
                    "* represents significance at the 10 % level", 
                    "** represents significance at the 5% level,", 
                    "and *** represents significance at the 1% level.", 
                     "Models (1), (2) and (3) refer, respectively, to equations (7), (8) and (9)"),
          style = "AER")

输出:

代码语言:javascript
复制
=================================================================================
Statistic             N       Mean   St. Dev.   Min    Pctl(25) Pctl(75)   Max   
---------------------------------------------------------------------------------
V1                    7      0.184    0.380    -0.230   -0.004   0.231    0.957  
V2                    7      -0.083   1.018    -1.246   -0.827   0.631    1.322  
V3                    7      -0.058   0.752    -0.733   -0.610   0.495    1.025  
V4                    7      -0.316   1.106    -2.223   -0.725   0.412    1.120  
V5                    7      -0.242   0.927    -1.777   -0.809   0.397    0.743  
V6                    7      -0.020   1.403    -1.453   -1.202   0.756    2.348  
V7                    7      0.565    1.130    -1.459   0.156    1.224    2.023  
V8                    7      0.840    0.938    -0.493   0.334    1.302    2.256  
V9                    7      -0.191   0.761    -1.012   -0.766   0.236    1.107  
V10                   7      0.530    0.643    -0.222   0.144    0.680    1.768  
V11                   7      0.263    0.396    -0.230   -0.004   0.432    0.957  
---------------------------------------------------------------------------------
All models were estimated by fixed effects estimator.                            
Errors are robust to heteroscedasticity and autocovariance.                      
Numbers between parenthesis are the coefficient standard deviations.             
* represents significance at the 10 % level                                      
** represents significance at the 5% level,                                      
and *** represents significance at the 1% level.                                 
Models (1), (2) and (3) refer, respectively, to equations (7), (8) and (9)   
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62350748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档