12/17/2017
Reports, Slides and Dashboards with R (3)
Reports, Slides and Dashboards with R
Dhafer Malouche
http://dhafermalouche.net
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
1/46
12/17/2017
Reports, Slides and Dashboards with R (3)
We will learn how to create
Scientific reproducible reports, books, papers…
Slides
Statistical Dashboards
With only FREE tools
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
2/46
12/17/2017
Reports, Slides and Dashboards with R (3)
LaTeX
Download Miktex (for Widows users) and Mactex (for Mac users)
Online and collaborative tools : Overleaf and Sharelatex
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
3/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Overleaf
Create your own account
For your first document choose one template and change it! (Here’s a demo)
We will show:
How to add a figure and a table: Caption, position in the text, reference and labels?
How to add Bibliography: .bib file, several options: bibtex code: Jabref (for windows users) and Bibdeck (for Mac users) Or Google
scholar
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
4/46
12/17/2017
Reports, Slides and Dashboards with R (3)
rmarkdown: Examples:
Document
HTML (for websites)
PDF (in Latex)
Word document
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
5/46
12/17/2017
Reports, Slides and Dashboards with R (3)
rmarkdown: Examples:
Presentation
HTML: 2 types isoslides and slidy
PDF: beamer presentations
PowerPoint (see Example below)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
6/46
12/17/2017
Reports, Slides and Dashboards with R (3)
rmarkdown: Examples:
From template
Revealjs (Presentation)
Tufte (HTML and PDF)
Bioconductor (PDF)
Flexdashboard
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
7/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Flexdashboard (from Template)
YAML
--title: "rbokeh iris dataset"
author: "Ryan Hafen"
output:
flexdashboard::flex_dashboard:
orientation: columns
social: menu
source_code: embed
---
Loading packages that will be used
library(rbokeh)
library(flexdashboard)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
8/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Flexdashboard (from Template)
Part 1
Declare the column
Column {data-width=600}
-----------------------------------------------------------------------
+ Make your plot
figure(width = NULL, height = NULL) %>%
ly_points(Sepal.Length, Sepal.Width, data = iris, color = Species)
# figure() %>%
#
ly_points(Sepal.Length, Sepal.Width, data = iris,
#
color = Species, glyph = Species)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
9/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Flexdashboard
Species
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
Species (Quantile)
Petal Width
10/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Flexdashboard (from Template)
Part 2
Declare 2nd column
Column {data-width=400}
-----------------------------------------------------------------------
Figure 2
figure(width = NULL, height = NULL, legend_location = "top_left") %>%
ly_quantile(Sepal.Length, group = Species, data = iris)
and
figure(width = NULL, height = NULL) %>%
ly_points(Sepal.Length, Sepal.Width, data = iris,
color = Petal.Width)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
11/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Flexdashboard with Shiny app
YAML
--title: "Iris K-Means Clustering"
output:
flexdashboard::flex_dashboard:
orientation: columns
social: menu
source_code: embed
runtime: shiny
---
Loading packages
library(datasets)
data(iris)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
12/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Flexdashboard with Shiny app
Column
X Variable
Sepal.Length
Y Variable
Sepal.Width
Cluster count
3
K Means
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
13/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Flexdashboard with Shiny app (sidebar panel)
Declaring the sidebar
Column {.sidebar}
-----------------------------------------------------------------------
Input part (UI)
selectInput('xcol', 'X Variable', names(iris))
selectInput('ycol', 'Y Variable', names(iris),
selected=names(iris)[[2]])
numericInput('clusters', 'Cluster count', 3,
min = 1, max = 9)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
14/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Flexdashboard with Shiny app (Main panel)
Declaring the Main panel
Column
-----------------------------------------------------------------------
Displaying result
palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
# Combine the selected variables into a new data frame
selectedData <- reactive({
iris[, c(input$xcol, input$ycol)]
})
clusters <- reactive({
kmeans(selectedData(), input$clusters)
})
renderPlot({
par(mar = c(5.1, 4.1, 0, 1))
plot(selectedData(),
col = clusters()$cluster,
pch = 20, cex = 3)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
15/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Power Point with R (Example)
Installing packages:
> library(devtools)
> devtools::install_github('davidgohel/ReporteRsjars')
> devtools::install_github('davidgohel/ReporteRs')
Loading the packages
>
>
>
>
>
library(ReporteRs)
library(haven)
library(vcd)
library(ggplot2)
library(reshape2)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
16/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Power Point with R (Example)
Importing data (It can be downloaded in this [link])(“http://wiki.qresearchsoftware.com/images/9/94/GSSforDIYsegmentation.sav)
> dat<-read_sav("GSSforDIYsegmentation.sav")
Now we give a name to the PPTX file and a Title to the presentation
> filename = "mypresentation.pptx"
> mypresentation= pptx(title = "Significant crosstabs")
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
17/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Power Point with R (Example)
Creating first slides : They contain mosaic-plots with the variable privbiz on the dependent variables:
dependent.variable.names = c("wrkstat", "marital", "sibs", "age", "educ")
Write a function for the mosaic-plot.
DrawMP<-function(v1,v2){
l1 = attr(v1, "label")
l2 = attr(v2, "label")
l1a = attr(v1, "labels")
l2a = attr(v2, "labels")
l1a=l1a[l1a%in%na.omit(unique(v1))]
v1a=mapvalues(as.factor(v1),from=na.
omit(unique(v1)),to=names(l1a))
l2a=l2a[l2a%in%na.omit(unique(v2))]
v2a=mapvalues(as.factor(v2),
from=na.omit(unique(v2)),to=names(l2a))
}
x = xtabs(~v1a + v2a)
x = x[rowSums(x) > 0, colSums(x) > 0]
ch = chisq.test(x)
crosstab = prop.table(x, 2) * 100
melted = melt(crosstab)
melted$position = 100 - as.numeric(apply(crosstab, 2, cumsum) - 0.5 * crosstab)
p = ggplot(melted, aes(x = v2a, y = value,fill = as.factor(v1a))) + geom_bar(stat='identity')
p = p + geom_text(data = melted, aes(x = v2a, y = position, label = paste0(round(value, 0),"%")), size=4)
p = p + scale_fill_manual(l1,values = rainbow(length(l1a)))
p = p + labs(x = l2, y = l1)+theme_bw()
colnames(crosstab) = paste0(colnames(crosstab), "%")
list(graph=p,crosstab=crosstab,chisqtest=ch)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
18/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Power Point with R (Example)
Computing the first mosaic-plot
>
>
>
>
>
v1=dat$privbiz
v2=dat$marital
X=DrawMP(v1,v2)
l1 = attr(v1, "label")
l2 = attr(v2, "label")
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
19/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Power Point with R (Example)
Adding first graph and table to the Slide 1
>
>
>
+
>
+
+
>
+
+
+
>
+
+
+
+
filename = "mypresentation.pptx" # the mypresentation to produce
mypresentation = pptx(title = "Significant crosstabs")
mypresentation = addSlide(mypresentation,
slide.layout = "Title and Content" )
mypresentation = addTitle(mypresentation,
paste0("Standardized residuals and chart: ",
l1, " by ", l2))
mypresentation = addPlot(doc = mypresentation,
fun = print, x = X$graph,
offx = 3, offy = 1, width = 6,
height = 5 )
mypresentation = addFlexTable(doc = mypresentation,
FlexTable(round(X$chisqtest$stdres, 1),
add.rownames = TRUE),
offx = 8, offy = 2,
width = 4.5, height = 3 )
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
20/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Power Point with R (Example)
Saving the PPTX document
> writeDoc(mypresentation, file = filename )
> file.exists(filename)
[1] TRUE
Go to the work directory and you will the file mypresentation.pptx.
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
21/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Plots in LaTeX format: tikzDevice
We want to change this figure into LaTeX code
>
>
>
>
library(ggpubr)
data("ToothGrowth")
df <- ToothGrowth
ggboxplot(df, x = "dose", y = "len", width = 0.8)
> library(tikzDevice)
> tikz("fig.tex",standAlone=TRUE)
> ggboxplot(df, x = "dose", y = "len", width = 0.8)
Measuring dimensions of: \char77
Measuring dimensions of: \char100
Measuring dimensions of: \char111
....
> dev.off()
null device
1
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
22/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Plots in LaTeX format: tikzDevice
Latex file is created in your work directory
% Created by tikzDevice version 0.10.1 on 2017-11-21 18:43:00
% !TEX encoding = UTF-8 Unicode
\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage[active,tightpage,psfixbb]{preview}
\PreviewEnvironment{pgfpicture}
\setlength\PreviewBorder{0pt}
\begin{document}
\begin{tikzpicture}[x=1pt,y=1pt]
\definecolor{fillColor}{RGB}{255,255,255}
.... # truncated output.
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
23/46
12/17/2017
Reports, Slides and Dashboards with R (3)
HTML tables: DT package
>
>
>
+
+
+
+
install.packages('DT')
library(DT)
datatable(iris, options = list(
searching = FALSE,
pageLength = 5,
lengthMenu = c(5, 15, 35, 50)
))
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
24/46
12/17/2017
Reports, Slides and Dashboards with R (3)
HTML tables: DT package
Show
5
entries
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
Species
1
5.1
3.5
1.4
0.2
setosa
2
4.9
3
1.4
0.2
setosa
3
4.7
3.2
1.3
0.2
setosa
4
4.6
3.1
1.5
0.2
setosa
5
5
3.6
1.4
0.2
setosa
Showing 1 to 5 of 150 entries
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
Previous
1
2
3
4
5
…
30
Next
25/46
12/17/2017
Reports, Slides and Dashboards with R (3)
DT tables with options: color in one column
> datatable(iris, options = list(pageLength = 5)) %>%
+
formatStyle('Sepal.Length', color = 'red', backgroundColor = 'orange', fontWeight = 'bold')
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
26/46
12/17/2017
Reports, Slides and Dashboards with R (3)
DT tables with options: color in one column
Show
5
entries
Search:
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
Species
1
5.1
3.5
1.4
0.2
setosa
2
4.9
3
1.4
0.2
setosa
3
4.7
3.2
1.3
0.2
setosa
4
4.6
3.1
1.5
0.2
setosa
5
5
3.6
1.4
0.2
setosa
Showing 1 to 5 of 150 entries
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
Previous
1
2
3
4
5
…
30
Next
27/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Adding buttons to download the table
> datatable(
+
iris, extensions = 'Buttons', options = list(
+
dom = 'Bfrtip',
+
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
+
)
+ )
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
28/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Adding buttons to download the table
Copy
CSV
Excel
PDF
Sepal.Length
Search:
Print
Sepal.Width
Petal.Length
Petal.Width
Species
1
5.1
3.5
1.4
0.2
setosa
2
4.9
3
1.4
0.2
setosa
3
4.7
3.2
1.3
0.2
setosa
4
4.6
3.1
1.5
0.2
setosa
5
5
3.6
1.4
0.2
setosa
6
5.4
3.9
1.7
0.4
setosa
7
4.6
3.4
1.4
0.3
setosa
8
5
3.4
1.5
0.2
setosa
9
4.4
2.9
1.4
0.2
setosa
10
4.9
3.1
1.5
0.1
setosa
Showing 1 to 10 of 150 entries
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
Previous
1
2
3
4
5
…
15
Next
29/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Reordering columns
You can click and drag the table header to move a certain column to a different place
> datatable(iris2, extensions = 'ColReorder', options = list(colReorder = TRUE))
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
30/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Reordering columns
Show
10
entries
Search:
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
Species
1
5.1
3.5
1.4
0.2
setosa
2
4.9
3
1.4
0.2
setosa
3
4.7
3.2
1.3
0.2
setosa
4
4.6
3.1
1.5
0.2
setosa
5
5
3.6
1.4
0.2
setosa
6
5.4
3.9
1.7
0.4
setosa
7
4.6
3.4
1.4
0.3
setosa
8
5
3.4
1.5
0.2
setosa
9
4.4
2.9
1.4
0.2
setosa
10
4.9
3.1
1.5
0.1
setosa
Showing 1 to 10 of 150 entries
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
Previous
1
2
3
4
5
…
15
Next
31/46
12/17/2017
Reports, Slides and Dashboards with R (3)
More ressources on DT package
http://rstudio.github.io/DT/extensions.html
https://blog.rstudio.com/2015/06/24/dt-an-r-interface-to-the-datatables-library/
googleVis
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
32/46
12/17/2017
Reports, Slides and Dashboards with R (3)
HTML table with sjPlot package
>
>
>
+
+
+
>
>
library(sjmisc)
library(sjPlot)
xCSS = list(css.table = "border: 2px solid;",
css.tdata = "border: 1px solid;",
css.firsttablecol = "color:#003399;
font-weight:bold;")
data(efc)
sjt.df(efc, altr.row.col = TRUE,CSS=xCSS)
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
33/46
12/17/2017
Reports, Slides and Dashboards with R (3)
HTML table with sjPlot package
Variable vars
n
missings
missings
mean
(percentage)
sd
median trimmed mad min max range skew kurtosis
c12hour
1
902
6
0.66
42.4 50.81
20
31.43
17.79
4
e15relat
2
901
7
0.77
2.85
2.08
2
2.44
0
1
8
e16sex
3
901
7
0.77
1.67
0.47
2
1.71
0
1
2
e17age
4
891
17
1.87
79.12 8.09
79
79.05
8.9
e42dep
5
901
7
0.77
2.94
3
3.02
1.48
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
0.94
168 164
se
1.65
1.31
1.69
7
1.55
1.21
0.07
1
-0.73
-1.47
0.02
65 103
38
0.06
-0.83
0.27
1
3
-0.42
-0.84
0.03
4
34/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Online LaTex and HTML tables
https://www.tablesgenerator.com
Tables Generator (/)
LaTeX Tables
(/latex_tables)
HTML Tables
(/html_tables)
Text Tables
(/text_tables)
Markdown Tables
(/markdown_tables)
MediaWiki Tables
(/mediawiki_tables)
LaTeX Table Generator
File
Edit
Table
Column
Contact
(/contact)
Facebook
Row
Cell
Help
4487
Twitter
Show Example
Default table style
A B C D E
1
2
3
4
Generate
Result (click "Generate" to refresh)
1
2
3
4
5
Copy to clipboard
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{lllll}
&
&
&
&
\\
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
35/46
12/17/2017
Reports, Slides and Dashboards with R (3)
LaTeX tables with xtable package
> library(xtable)
> tli$grade<-tli$grade+rnorm(nrow(tli))
> tli.table <- xtable(tli[1:3,],align=c("l","c","l","l","c","c"),
+
digits=c(0,2,0,0,0,0),
+
caption="My table",label="tab1")
> tli.table
% latex table generated in R 3.4.2 by xtable 1.8-2 package
% Wed Nov 22 15:16:29 2017
\begin{table}[ht]
\centering
\begin{tabular}{lcllcc}
\hline
& grade & sex & disadvg & ethnicty & tlimth \\
\hline
1 & 5.95 & M & YES & HISPANIC & 43 \\
2 & 6.22 & M & NO & BLACK & 88 \\
3 & 3.33 & F & YES & HISPANIC & 34 \\
\hline
\end{tabular}
\caption{My table}
\label{tab1}
\end{table}
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
36/46
12/17/2017
Reports, Slides and Dashboards with R (3)
HTML tables with xtable package
> library(xtable)
> data(tli)
> tli$grade<-tli$grade+rnorm(nrow(tli))
> tli.table <- xtable(tli[1:3,],align=c("l","c","l","l","c","c"),
+
digits=c(0,2,0,0,0,0),
+
caption="My table",label="tab1")
> print(tli.table,type="html")
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
37/46
12/17/2017
Reports, Slides and Dashboards with R (3)
HTML tables with xtable package
grade sex disadvg ethnicty tlimth
1
6.60
M
YES
HISPANIC
43
2
3.65
M
NO
BLACK
88
3
5.14
F
YES
HISPANIC
34
My table
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
38/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Model LaTeX and HTML tables with stargazer
package
1. Data Summary
>
>
>
>
library(stargazer)
data("attitude")
head(attitude,3)
stargazer(attitude,flip=T)
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, Nov 22, 2017 - 14:33:03
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}}lccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Statistic & rating & complaints & privileges & learning & raises & critical & advance \\
\hline \\[-1.8ex]
N & 30 & 30 & 30 & 30 & 30 & 30 & 30 \\
Mean & 64.633 & 66.600 & 53.133 & 56.367 & 64.633 & 74.767 & 42.933 \\
St. Dev. & 12.173 & 13.315 & 12.235 & 11.737 & 10.397 & 9.895 & 10.289 \\
Min & 40 & 37 & 30 & 34 & 43 & 49 & 25 \\
Max & 85 & 90 & 83 & 75 & 88 & 92 & 72 \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
>
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
39/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Model LaTeX and HTML tables with stargazer
package
2. Display a whole table
> stargazer(attitude, summary=FALSE)
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, Nov 22, 2017 - 14:35:15
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} cccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& rating & complaints & privileges & learning & raises & critical & advance \\
\hline \\[-1.8ex]
1 & $43$ & $51$ & $30$ & $39$ & $61$ & $92$ & $45$ \\
2 & $63$ & $64$ & $51$ & $54$ & $63$ & $73$ & $47$ \\
3 & $71$ & $70$ & $68$ & $69$ & $76$ & $86$ & $48$ \\
4 & $61$ & $63$ & $45$ & $47$ & $54$ & $84$ & $35$ \\
5 & $81$ & $78$ & $56$ & $66$ & $71$ & $83$ & $47$ \\
6 & $43$ & $55$ & $49$ & $44$ & $54$ & $49$ & $34$ \\
.... #Truncated output
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
40/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Model LaTeX and HTML tables with stargazer
package
3. Comparing models:
>
>
>
+
>
>
>
+
>
>
library(stargazer)
attitude$high.rating <- (attitude$rating > 70)
linear.1 <- lm(rating ~ complaints + privileges + learning
+ raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude,
family = binomial(link = "probit"))
stargazer(linear.1, linear.2, probit.model, title="Regression Results")
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
41/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Model LaTeX and HTML tables with stargazer
package
3. Comparing models:
LaTex file
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, Nov 22, 2017 - 14:46:00
\begin{table}[!htbp] \centering
\caption{Regression Results}
\label{}
\begin{tabular}{@{\extracolsep{5pt}}lccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{3}{c}{\textit{Dependent variable:}} \\
\cline{2-4}
\\[-1.8ex] & \multicolumn{2}{c}{rating} & high.rating \\
\\[-1.8ex] & \multicolumn{2}{c}{\textit{OLS}} & \textit{probit} \\
\\[-1.8ex] & (1) & (2) & (3)\\
\hline \\[-1.8ex]
complaints & 0.692$^{***}$ & 0.682$^{***}$ & \\
& (0.149) & (0.129) & \\
& & & \\
privileges & $-$0.104 & $-$0.103 & \\
& (0.135) & (0.129) & \\
& & & \\
learning & 0.249 & 0.238$^{*}$ & 0.164$^{***}$ \\
& (0.160) & (0.139) & (0.053) \\
& & & \\
raises & $-$0.033 & & \\
& (0.202) & & \\
& & & \\
critical & 0.015 & & $-$0.001 \\
& (0.147) & & (0.044) \\
& & & \\
advance & & & $-$0.062 \\
& & & (0.042) \\
& & & \\
Constant & 11.011 & 11.258 & $-$7.476$^{**}$ \\
& (11.704) & (7.318) & (3.570) \\
& & & \\
\hline \\[-1.8ex]
Observations & 30 & 30 & 30 \\
R$^{2}$ & 0.715 & 0.715 & \\
Adjusted R$^{2}$ & 0.656 & 0.682 & \\
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
42/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Log Likelihood & & & $-$9.087 \\
Akaike Inf. Crit. & & & 26.175 \\
Residual Std. Error & 7.139 (df = 24) & 6.863 (df = 26) & \\
F Statistic & 12.063$^{***}$ (df = 5; 24) & 21.743$^{***}$ (df = 3; 26) & \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{3}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
43/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Model LaTeX and HTML tables with stargazer
package
3. Comparing models (HTML output)
>
>
>
+
>
>
>
+
>
>
+
+
+
+
+
library(stargazer)
attitude$high.rating <- (attitude$rating > 70)
linear.1 <- lm(rating ~ complaints + privileges + learning
+ raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude,
family = binomial(link = "probit"))
stargazer(linear.1, linear.2, probit.model,
type="html",
title="Regression Results",
single.row=TRUE,
ci=TRUE, ci.level=0.9,
omit.stat=c("f", "ser"))
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
44/46
12/17/2017
Reports, Slides and Dashboards with R (3)
Model LaTeX and HTML tables with stargazer
package
3. Comparing models (HTML output)
Regression Results
Dependent variable:
rating
high.rating
OLS
probit
(1)
(2)
complaints
0.692*** (0.447, 0.937) 0.682*** (0.470, 0.894)
privileges
-0.104 (-0.325, 0.118)
-0.103 (-0.316, 0.109)
learning
0.249 (-0.013, 0.512)
0.238* (0.009, 0.467)
raises
-0.033 (-0.366, 0.299)
critical
0.015 (-0.227, 0.258)
Observations
0.164*** (0.077, 0.252)
-0.001 (-0.073, 0.072)
advance
Constant
(3)
-0.062 (-0.131, 0.007)
11.011 (-8.240, 30.262) 11.258 (-0.779, 23.296) -7.476** (-13.349, -1.604)
30
30
R2
0.715
0.715
Adjusted R2
0.656
0.682
30
Log Likelihood
-9.087
Akaike Inf. Crit.
26.175
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
45/46
12/17/2017
Note:
file:///Users/dhafermalouche/Documents/Teaching/AdvancedR/reports_slides.html#(3)
Reports, Slides and Dashboards with R (3)
p<0.1; p<0.05; p<0.01
46/46