使用下面的代码,我设法生成下面的第一个表。但...
swiss2 <- swiss[1:20,]
m1 <- lm(Fertility ~ Agriculture, data = swiss)
m2 <- lm(Fertility ~ Examination, data = swiss)
m3 <- lm(Infant.Mortality ~ Education, data = swiss)
m4 <- lm(Infant.Mortality ~ Catholic, data = swiss)
m5 <- lm(Fertility ~ Agriculture, data = swiss2)
m6 <- lm(Fertility ~ Examination, data = swiss2)
m7 <- lm(Infant.Mortality ~ Education, data = swiss2)
m8 <- lm(Infant.Mortality ~ Catholic, data = swiss2)
stargazer(m1, m2, m3, m4, m5, m6, m7, m8,
type = "latex",
out="./table.tex",
omit.stat=c("LL","ser","f","adj.rsq"),
font.size="tiny",
column.labels = c("(M1)", "(M2)", "(M3)", "(M4)", "(M5)", "(M6)", "(M7)", "(M8)"),
model.names = FALSE,
model.numbers = FALSE,
star.cutoffs = c(0.05, 0.01, 0.001),
dep.var.labels = c("Outcome 1", "Outcome 2", "Outcome 1", "Outcome 2"))
...我想在下面制作这张表。唯一的区别是有一行而不是行“从属变量:”,其中两列表示相关数据集Swiss和Swiss2。我可以在Latex中手动执行此操作,但我需要/想要直接破解R,以便我的研究可以完全从Rmarkdown文件中重现。想法有人吗?谢谢!
答案 0 :(得分:1)
在stargazer()
函数中,您引用的行由dep.var.caption
选项控制。不幸的是,由于你想要在这一行中有多个列,所以如果没有一些修补,你就无法完成你想要的任务;如果你传递长度为&gt;的向量1到此选项,stargazer()
将抛出错误。因此,我们必须制作一个自定义函数来捕获stargazer()
的输出并在打印之前相应地修改它。
以下.Rmd文件对我来说很好(代码下面的输出):
---
title: "Stack Overflow Answer"
author: "duckmayr"
date: "November 3, 2017"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo=FALSE}
custom_table <- function(dataset_labels, ...) {
tbl <- capture.output(stargazer::stargazer(...))
pattern1 <- 'Dependent variable:'
pattern2 <- '(?<= \\& ).+(?= \\\\)'
first_row_index <- which(grepl(pattern=pattern1, x=tbl))
first_row <- tbl[first_row_index]
colspan <- as.numeric(gsub(pattern='[^0-9]+', replacement='', first_row))
colspan <- colspan / length(dataset_labels)
new_first_row <- sub('[0-9]+', colspan, first_row)
replacement <- rep(stringr::str_extract(new_first_row, pattern2), 2)
replacement <- stringr::str_replace(replacement, pattern1, dataset_labels)
replacement <- paste(replacement, collapse=' & ')
new_first_row <- stringr::str_replace(new_first_row, pattern2, replacement)
new_first_row <- stringr::str_replace_all(new_first_row, 'multi', '\\\\multi')
new_first_row <- stringr::str_replace_all(new_first_row, 'textit', '\\\\textit')
tbl[first_row_index] <- new_first_row
cat(tbl, sep='\n')
}
swiss2 <- swiss[1:20,]
m1 <- lm(Fertility ~ Agriculture, data = swiss)
m2 <- lm(Fertility ~ Examination, data = swiss)
m3 <- lm(Infant.Mortality ~ Education, data = swiss)
m4 <- lm(Infant.Mortality ~ Catholic, data = swiss)
m5 <- lm(Fertility ~ Agriculture, data = swiss2)
m6 <- lm(Fertility ~ Examination, data = swiss2)
m7 <- lm(Infant.Mortality ~ Education, data = swiss2)
m8 <- lm(Infant.Mortality ~ Catholic, data = swiss2)
```
```{r, echo=FALSE, results='asis'}
custom_table(c('Data: Swiss', 'Data: Swiss2'),
m1, m2, m3, m4, m5, m6, m7, m8,
type = "latex",
header=FALSE,
omit.stat=c("LL","ser","f","adj.rsq"),
font.size="tiny",
column.labels = c("(M1)", "(M2)", "(M3)", "(M4)", "(M5)", "(M6)", "(M7)", "(M8)"),
model.names = FALSE,
model.numbers = FALSE,
star.cutoffs = c(0.05, 0.01, 0.001),
dep.var.labels = c("Outcome 1", "Outcome 2", "Outcome 1", "Outcome 2"))
```
编辑:
如果您希望输出转到tex文件而不是(或除此之外)直接使用.Rmd文件中的输出,我们可以进行以下调整:
custom_table <- function(dataset_labels, ..., cat_output=TRUE, out_file=NULL) {
tbl <- capture.output(stargazer::stargazer(...))
pattern1 <- 'Dependent variable:'
pattern2 <- '(?<= \\& ).+(?= \\\\)'
first_row_index <- which(grepl(pattern=pattern1, x=tbl))
first_row <- tbl[first_row_index]
colspan <- as.numeric(gsub(pattern='[^0-9]+', replacement='', first_row))
colspan <- colspan / length(dataset_labels)
new_first_row <- sub('[0-9]+', colspan, first_row)
replacement <- rep(stringr::str_extract(new_first_row, pattern2), 2)
replacement <- stringr::str_replace(replacement, pattern1, dataset_labels)
replacement <- paste(replacement, collapse=' & ')
new_first_row <- stringr::str_replace(new_first_row, pattern2, replacement)
new_first_row <- stringr::str_replace_all(new_first_row, 'multi', '\\\\multi')
new_first_row <- stringr::str_replace_all(new_first_row, 'textit', '\\\\textit')
tbl[first_row_index] <- new_first_row
if ( cat_output ) {
cat(tbl, sep='\n')
}
if ( !is.null(out_file) ) {
cat(tbl, sep='\n', file=out_file)
}
}
然后,如果您在R脚本中运行下面的代码,或者将其放在Rmd文件的块中,您将把输出写入文件&#39; test_out.tex&#39;以及直接输出:
custom_table(c('Data: Swiss', 'Data: Swiss2'),
out_file='test_out.tex',
m1, m2, m3, m4, m5, m6, m7, m8,
type = "latex",
header=FALSE,
omit.stat=c("LL","ser","f","adj.rsq"),
font.size="tiny",
column.labels = c("(M1)", "(M2)", "(M3)", "(M4)", "(M5)", "(M6)", "(M7)", "(M8)"),
model.names = FALSE,
model.numbers = FALSE,
star.cutoffs = c(0.05, 0.01, 0.001),
dep.var.labels = c("Outcome 1", "Outcome 2", "Outcome 1", "Outcome 2"))
使用out
功能的stargazer()
选项无法正常工作,因为stargazer()
会在我们有out
之前将输出写入constructor(private authGuard: AuthGuard, private route: ActivatedRoute){}
upvoteClick(article: NewsArticle): void {
if (this.authGuard.canActivate(this.route.snapshot)) {
//logic requiring authentication
}
}
有机会对它进行修改,但这个调整有效。