-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.R
72 lines (64 loc) · 3.05 KB
/
index.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
## ----setup, include=FALSE------------------------------------------------------
## First specify the packages of interest
packages = c("tidyverse", "knitr", "survey", "ggthemes", "extrafont", "dplyr",
"srvyr", "tidyr", "readxl", "showtext", "ggplot2", "ggExtra",
"broom", "kableExtra", "lmtest", "scales", "summarytools", "apsrtable",
"NetworkChange", "MCMCpack", "stargazer")
## Now load or install&load all
package.check <- lapply(
packages,
FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
}
)
## transparent dots in graph
addTrans <- NetworkChange:::addTrans
## default R base plot format
par(mar=c(3,3,2,1), mgp=c(2,.7,0), tck=.02)
## theme_set(theme_classic())
knitr::opts_chunk$set(echo=TRUE, out.width="100%", fig.align = "center", message=F, warning=F, fig.height = 6, cache=T, dpi = 300, dev = "png")
Sys.setlocale(category = "LC_CTYPE", locale = "ko_KR.UTF-8")
theme_set(theme_gray(base_family='NanumGothic'))
## 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")
}
## regression summary plot
## The first variable after ~ will be considered as the explanatory var.
ggReg <- function (fit, title="") {
gtext <- paste("Call = ", summary(fit)$call[2], "\n",
"Adj R2 = ",signif(summary(fit)$adj.r.squared, 5))
ggplot(fit$model, aes_string(x = names(fit$model)[2],
y = names(fit$model)[1])) +
labs(title=title, subtitle = gtext) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
theme(plot.title = element_text(size = rel(.8)))
}
## my style ggplot2 theme
theme_jhp <- function (base_size = 10, base_family = "sans")
{
colors <- tibble::deframe(ggthemes::ggthemes_data[["fivethirtyeight"]])
(theme_foundation(base_size = base_size, base_family = base_family) +
theme(line = element_line(colour = "black"),
rect = element_rect(fill = colors["Light Gray"],
linetype = 0, colour = NA),
text = element_text(colour = colors["Dark Gray"]),
## axis.title = element_blank(), axis.text = element_text(),
axis.ticks = element_blank(), axis.line = element_blank(),
legend.background = element_rect(), legend.position = "bottom",
legend.direction = "horizontal", legend.box = "vertical",
panel.grid = element_line(colour = NULL),
panel.grid.major = element_line(colour = colors["Medium Gray"]),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = 0, size = rel(1.5), face = "bold"),
plot.margin = unit(c(1, 1, 1, 1), "lines"),
strip.background = element_rect()))
}