How to knit out table codes into table in R markdown - r-markdown

I am a basic-level learner of R. I am having a problem knitting out tables with a code my professor designed for the students. The code for table designs is set as below. I put this in my R markdown as below.
```{r, results="hide", message=FALSE, warning = FALSE, error = FALSE}
## my style latex summary of regression
jhp_report <- function(...){
output <- capture.output(stargazer(..., omit.stat=c("f", "ser")))
# The first three lines are the ones we want to remove...
output <- output[4:length(output)]
# cat out the results - this is essentially just what stargazer does too
cat(paste(output, collapse = "\n"), "\n")
}
```
After this, I tried printing this out with knitr.
```{r, message=FALSE, warning = FALSE, error = FALSE}
set.seed(1973)
N <- 100
x <- runif(N, 6, 20)
D <- rbinom(N, 1, .5)
t <- 1 + 0.5*x - .4*D + rnorm(N)
df.lm <- data.frame(y = y, x =x, D =D)
df.lm$D <- factor(df.lm$D, labels = c('Male', 'Female'))
##REGRESSION
reg.parallel <- lm(y ~ x + D, data = df.lm)
jhp_report(reg.parallel, title = "Result", label = "tab:D", dep.var.labels = "$y$")
```
As a result, instead of a table, it keeps on showing only the pure codes. I would like to know how I have to set up R markdown for it to print out the table instead of the codes. This is how the result looks like when I knit it.
I expected that there must be some setup options to print the table out. But I couldn't find the right one. Also, my assignment for class requires students to use this code. I did find other options like knitr::kable but I would like to use the given code for this assignment.
Thank you in advance!

Related

How to create a crosstab with variable labels for PDF output in R markdown

I would like to make a table in R markdown that prints a crosstabulation of two variables and includes the variable name above it and on the left side. Also, I need to print this to a PDF so I require code that is compatible with kable("latex").
Reproducible example:
set.seed(143)
x <- sample(x = c("yes", "no"), size = 20, replace = TRUE)
y <- sample(x = c("yes", "no"), size = 20, replace = TRUE)
table(x,y) %>%
kable("latex") %>%
pack_rows("X", 1, 2) %>%
add_header_above(c(" ", "Y" = 2))
Which gives the following output:
However I would like it to look like this (created in Word for example):

Knitting Rmarkdown to have AIC round to one's place while rounding regression coefficients and SE to tenth's place

I use the following function in the setup of Rmarkdown to make it so that in knitting everything rounds to two decimal places, but how can I alter the code to create a conditional such that for AIC (x>1000, for instance) it will round to the one's place?
Thanks!
Minimal reproducible example using mtcars data set. Looking at the effect of car weight on mpg with a random factor of cylinder. Make sure to knit everything below in Rmarkdown...if you just use the code in R, it will round the AIC.
---
output:
pdf_document: default
---
```{r echo = FALSE, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(scientific=FALSE)
scientific=FALSE
# knitr::clean_cache()
options(digits=3)
library(tidyverse)
library(lme4)
inline_hook <- function (x) {
if (is.numeric(x)) {
# ifelse does a vectorized comparison
# If integer, print without decimal; otherwise print two places
res <- ifelse(x == round(x),
sprintf("%d", x),
sprintf("%.3f", x)
)
paste(res, collapse = ", ")
}
}
knitr::knit_hooks$set(inline = inline_hook)
mpg <- lmer(mpg~wt + (1|cyl), mtcars, na.action = 'na.exclude', control = lmerControl(optimizer = "nloptwrap", calc.derivs = FALSE), REML = FALSE)
AIC(logLik(mpg))
coef(summary(mpg))[2]
```
AIC = `r AIC(logLik(mpg))`
Effect size = `r coef(summary(mpg))[2]`

How can I split a table so that it appears side by side in R markdown?

I'm writing a document with R markdown and I'd like to put a table. The problem is that this table only has two columns and takes a full page, which is not very beautiful. So my question is : is there a way to split this table in two and to place the two "sub-tables" side by side with only one caption ?
I use the kable command and I tried this solution (How to split kable over multiple columns?) but I could not do the cbind() command.
Here's my code to create the table :
---
title:
author:
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: pdf_document
indent: true
header-includes:
- \usepackage{indentfirst}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo = FALSE}
kable(aerop2, format = "markdown")
```
where aerop2 is my data frame with a list of country names in column 1 and the number of airports in each of these countries in column 2.
I have a long two-column table which is a waste of space. I would like to split this table in two sub-tables and put these sub-tables side by side with a caption that includes both of them.
This doesn't give a lot of flexibility in spacing, but here's one way to do it. I'm using the mtcars dataset as an example because I don't have aerop2.
---
output: pdf_document
indent: true
header-includes:
- \usepackage{indentfirst}
- \usepackage{booktabs}
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
```
The data are in Table \ref{tab:tables}, which will float to the top of the page.
```{r echo = FALSE}
rows <- seq_len(nrow(mtcars) %/% 2)
kable(list(mtcars[rows,1:2],
matrix(numeric(), nrow=0, ncol=1),
mtcars[-rows, 1:2]),
caption = "This is the caption.",
label = "tables", format = "latex", booktabs = TRUE)
```
This gives:
Note that without that zero-row matrix, the two parts are closer together. To increase the spacing more, put extra copies of the zero-row matrix into
the list.
The solution offered by 'user2554330' was very useful.
As I needed to split in more columns and eventually more sections, I further developed the idea.
I also needed to have the tables after the text, not floating to the top. I found a way using kableExtra::kable_styling(latex_options = "hold_position").
I am writing here to share the development and to ask minor questions.
1 - Why did you add the line - \usepackage{indentfirst}?
2 - What is the effect of label = "tables" as kable() input?
(The questions are related to Latex. I probably know to little to understand the explanation in kable() documentation: "label - The table reference label"!)
---
title: "Test-split.print"
header-includes:
- \usepackage{booktabs}
output:
pdf_document: default
html_document:
df_print: paged
---
```{r setup, include=FALSE}
suppressPackageStartupMessages(library(tidyverse))
library(knitr)
library(kableExtra)
split.print <- function(x, cols = 2, sects = 1, spaces = 1, caption = "", label = ""){
if (cols < 1) stop("cols must be GT 1!")
if (sects < 1) stop("sects must be GT 1!")
rims <- nrow(x) %% sects
nris <- (rep(nrow(x) %/% sects, sects) + c(rep(1, rims), rep(0, sects-rims))) %>%
cumsum() %>%
c(0, .)
for(s in 1:sects){
xs <- x[(nris[s]+1):nris[s+1], ]
rimc <- nrow(xs) %% cols
nric <- (rep(nrow(xs) %/% cols, cols) + c(rep(1, rimc), rep(0, cols-rimc))) %>%
cumsum() %>%
c(0, .)
lst <- NULL
spc <- NULL
for(sp in 1:spaces) spc <- c(spc, list(matrix(numeric(), nrow=0, ncol=1)))
for(c in 1:cols){
lst <- c(lst, list(xs[(nric[c]+1):nric[c+1], ]))
if (cols > 1 & c < cols) lst <- c(lst, spc)
}
kable(lst,
caption = ifelse(sects == 1, caption, paste0(caption, " (", s, "/", sects, ")")),
label = "tables", format = "latex", booktabs = TRUE) %>%
kable_styling(latex_options = "hold_position") %>%
print()
}
}
```
```{r, results='asis'}
airquality %>%
select(1:3) %>%
split.print(cols = 3, sects = 2, caption = "multi page table")
```

How to exclude standard errors from stargazer table?

Amazing R gurus,
I am just wondering if there is any way to exclude standard errors from stargazer table.
Here is a quick reproducible example:
---
title: "Test regression"
output: html_document
date: "`r format(Sys.time(), '%d %B, %Y')`"
---
```{r setup, echo=FALSE, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(cashe = TRUE)
rm(list=ls())
library(stargazer)
library(ggplot2)
```
```{r, results='asis', echo=FALSE}
fit <- lm(price ~ carat + table + x + y + z, data = diamonds)
stargazer(fit, title="Diamonds Regression",
single.row = TRUE, type ="html", header = FALSE, df=FALSE, digits=2, se = NULL)
```
I would like to see results without standard error like shown in the following screenhsot.
Your time and help is much appreciated.
I just wanted to achieve the same thing, and found the report argument in the stargazer documentation, wich can be used to control the elements shown (and the order) in the output table.
If used like this:
fit <- lm(price ~ carat + table + x + y + z, data = diamonds)
stargazer(fit, title="Diamonds Regression",
single.row = TRUE,
type ="html",
report = "vc*",
header = FALSE,
df=FALSE,
digits=2,
se = NULL
)
It produces the desired output without the need to capture the output first (or any other additional code).
Here is a simple way:
```{r, results='asis', echo=FALSE}
fit <- lm(price ~ carat + table + x + y + z, data = diamonds)
mytab <- capture.output(stargazer(fit, title="Diamonds Regression",
single.row = TRUE, type ="html", header = FALSE, df=FALSE,
digits=2,
apply.se = function(x) { 0 }))
cat(paste(gsub("\\(0.00\\)", "", mytab), collapse = "\n"), "\n")
```
We first capture the output of stargazer and suppress automatic printing. In stargazer we set all standard errors to be 0 (makes the following replacement more failsave). Lastly, we print the output and replace these standard errors.

read.dta convert.dates not working?

I have a Stata dataset, call it dataset.dta. I want to read it in R. I am using the package foreign. Problem is it fails to parse/convert Stata dates to R dates.
It goes something like this:
df <- read.dta( 'dataset.dta', convert.dates = TRUE )
# Check attributes
attr( df, "formats")
"%9s" "%8.0g" "%12.0g" "%12.0g" "%9.0g" "%21s" "%31s" "%td" "%td"
# Last two columns are dates i.e. %td
str( df )
... # Only showing last two columns
$ start_sample: num 15494 14246 14246 14670 14245 ...
$ end_sample : num 18262 18262 18262 18262 18262 ...
I was expecting Date class for these, instead of num. When I look into the source code of read.dta I find this.
if (convert.dates) {
ff <- attr(rval, "formats")
dates <- grep("%-*d", ff)
base <- structure(-3653, class = "Date")
for (v in dates) rval[[v]] <- base + rval[[v]]
}
Changing the third line here to dates <- grep( "%*d", ff) seems to take care of the issue. I changed the regex. I'm using Stata version 13.0.
Am I missing something? This just a bug or am I doing something woefully wrong here?
Two quick fixes/hacks. The first is
#### Convert to dates ####
datelookup <- format(seq(as.Date("1960-01-01"), as.Date("2015-12-31"), by = "1 day"))
df$start_sample_ dates <- datelookup[ df$start_sample + 1]
df$start_sample_dates <- datelookup[ df$end_sample + 1]
Stata uses 01/01/1960 as the base. The second is
#### Stealing from foreign package ####
ff <- attr(df, "formats")
dates <- grep("%*d", ff)
base <- structure(-3653, class = "Date")
for (v in dates) df[[v]] <- base + df[[v]]
Why structure(-3653, class = "Date") ? See comment #Dimitriy V. Masterov above. This issue could be specific to Stata version 13.0. See comment #dickoa above. Thanks for your help.