简体   繁体   中英

How Do I Output Regression Result Tables to Word from Knitr / R RStudio? (mtable, memisc)

I would like to output regression result tables to Word from knitr but I'm having trouble. Outputting mtable s to \\LaTeX works if I tweak the options but I'm stuck when it comes to Word. My MWE is below.

---
output:
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(memisc)
```

## This section contains ugly text, not a table
```{r eval = T, include = T, echo = F}
lm0 <- lm(sr ~ pop15 + pop75,              data = LifeCycleSavings)
lm1 <- lm(sr ~                 dpi + ddpi, data = LifeCycleSavings)
lm2 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)

mt01 <- mtable(lm0,lm1,summary.stats=c("R-squared","N"))
mt12 <- mtable(lm1,lm2,summary.stats=c("R-squared","F","N"))

c("Group 1"=mt01,
"Group 2"=mt12)
```

I've devised an ugly hack that works. I hope it can help someone else in my position. I am fairly certain that an obvious and simpler way to achieve this exists.

---
output:
  word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(memisc)
```

## This section contains a table, not ugly text
```{r eval = T, include = T, echo = F}
lm0 <- lm(sr ~ pop15 + pop75,              data = LifeCycleSavings)
lm1 <- lm(sr ~                 dpi + ddpi, data = LifeCycleSavings)
lm2 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)

mt01 <- mtable(lm0,lm1,summary.stats=c("R-squared","N"))
mt12 <- mtable(lm1,lm2,summary.stats=c("R-squared","F","N"))

x = c("Group 1"=mt01,"Group 2"=mt12)

x = memisc::mtable_format_delim(x)
writeLines(x,"table.csv")
x = read.delim("table.csv", header = F, stringsAsFactors = F)
colnames(x) = x[1,]
x = x[-1,]
rownames(x) = NULL
knitr::kable(x)
```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM