-
Notifications
You must be signed in to change notification settings - Fork 0
/
Figure 6-1-1.r
51 lines (43 loc) · 2.23 KB
/
Figure 6-1-1.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
# R code for Figure 6-1-1 #
# Required packages #
# - ggplot2: making pretty graphs #
# - gridExtra: combine graphs #
library(ggplot2)
library(gridExtra)
# Generate series
nobs = 100
x <- runif(nobs)
y.linear <- x + (x > 0.5) * 0.25 + rnorm(n = nobs, mean = 0, sd = 0.1)
y.nonlin <- 0.5 * sin(6 * (x - 0.5)) + 0.5 + (x > 0.5) * 0.25 + rnorm(n = nobs, mean = 0, sd = 0.1)
y.mistake <- 1 / (1 + exp(-25 * (x - 0.5))) + rnorm(n = nobs, mean = 0, sd = 0.1)
rd.series <- data.frame(x, y.linear, y.nonlin, y.mistake)
# Make graph with ggplot2
g.data <- ggplot(rd.series, aes(x = x, group = x > 0.5))
p.linear <- g.data + geom_point(aes(y = y.linear)) +
stat_smooth(aes(y = y.linear),
method = "lm",
se = FALSE) +
geom_vline(xintercept = 0.5) +
ylab("Outcome") +
ggtitle(bquote('A. Linear E[' * Y["0i"] * '|' * X[i] * ']'))
p.nonlin <- g.data + geom_point(aes(y = y.nonlin)) +
stat_smooth(aes(y = y.nonlin),
method = "lm",
formula = y ~ poly(x, 2),
se = FALSE) +
geom_vline(xintercept = 0.5) +
ylab("Outcome") +
ggtitle(bquote('B. Nonlinear E[' * Y["0i"] * '|' * X[i] * ']'))
f.mistake <- function(x) {1 / (1 + exp(-25 * (x - 0.5)))}
p.mistake <- g.data + geom_point(aes(y = y.mistake)) +
stat_smooth(aes(y = y.mistake),
method = "lm",
se = FALSE) +
stat_function(fun = f.mistake,
linetype = "dashed") +
geom_vline(xintercept = 0.5) +
ylab("Outcome") +
ggtitle('C. Nonlinearity mistaken for discontinuity')
p.rd.examples <- arrangeGrob(p.linear, p.nonlin, p.mistake, ncol = 1)
ggsave(p.rd.examples, file = "Figure 6-1-1-R.pdf", width = 5, height = 9)
# End of script