-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
4.1_LinearTransformations.R
46 lines (38 loc) · 1.61 KB
/
4.1_LinearTransformations.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
library(rstan)
library(ggplot2)
### Data
source("earnings.data.R", echo = TRUE)
### First model: earn ~ height
data.list <- c("N", "earn", "height")
earn_height <- stan(file="earn_height.stan", data=data.list,
iter=1000, chains=4)
print(earn_height, pars = c("beta", "sigma", "lp__"))
### Figures
beta.post <- extract(earn_height, "beta")$beta
beta.mean <- colMeans(beta.post)
n <- 100
ndx <- sample(nrow(beta.post), n)
earnings.post <- data.frame(sampled.int = beta.post[ndx, 1],
sampled.slope = beta.post[ndx, 2],
id = ndx)
p <- ggplot(data.frame(height, earn), aes(x = height, y = earn)) +
geom_jitter(position = position_jitter(width = 0.2, height = 0.2)) +
geom_abline(aes(intercept = sampled.int, slope = sampled.slope, group = id),
data = earnings.post, alpha = 0.05) +
geom_abline(aes(intercept = beta.mean[1], slope = beta.mean[2])) +
theme(axis.text.y = element_text(angle = 90, hjust = 0.5)) +
theme_bw()
# Figure 4.1 (left)
print(p +
scale_x_continuous("height", breaks = seq(60, 75, 5)) +
scale_y_continuous("earnings", breaks = c(0, 100000, 200000),
labels = c("0", "100000", "200000")) +
ggtitle("Fitted linear model"))
## Figure 4.1 (right)
dev.new()
print(p +
scale_x_continuous("height", limits = c(0, 80), breaks = seq(0, 80, 20)) +
scale_y_continuous("earnings", limits = c(-200000, 200000),
breaks = c(-100000, 0, 100000),
labels = c("-100000", "0", "100000")) +
ggtitle("x-axis extended to 0"))