-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy path18.5_CensoredData.R
54 lines (37 loc) · 1.49 KB
/
18.5_CensoredData.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
library(rstan)
library(ggplot2)
### Regression: weight ~ height
source("weight.data.R", echo = TRUE)
weight.data <- c("N", "weight", "height")
censored.0.sf <- stan(file='weight.stan', data=weight.data, iter=1000, chains=4)
print(censored.0.sf)
### Censoring
source("weight_censored.data.R", echo = TRUE)
## Figure 18.8 (a)
weight_censored.ggdf <- data.frame(weight, height)
p1 <- ggplot(weight_censored.ggdf, aes(weight)) +
geom_histogram(color = "black", fill = "gray", binwidth = 5) +
xlab("weight measurement")
print(p1)
## Figure 18.8 (b)
dev.new()
p2 <- ggplot(weight_censored.ggdf, aes(height, weight)) +
geom_jitter(position = position_jitter(width = 0.15, height = 1.5)) +
ylab("weight measurement")
print(p2)
### Naive regression excluding the censored data
source("weight_lt200.data.R", echo = TRUE)
weight.data <- c("N", "weight", "height")
censored.1.sf <- stan(file='weight.stan', data=weight.data, iter=1000, chains=4)
print(censored.1.sf)
## Naive regression imputing the censoring points
source("weight_censored.data.R", echo = TRUE)
weight.data <- c("N", "weight", "height")
censored.2.sf <- stan(file='weight.stan', data=weight.data, iter=1000, chains=4)
print(censored.2.sf)
## Fitting the censored-data model
# source("weight_censored.data.R")
censoring.data <- c("N", "N_obs", "N_cens", "C", "weight", "height")
censoring.sf <- stan(file='weight_censored.stan', data=censoring.data,
iter=1000, chains=4)
print(censoring.sf, pars = c("a", "b", "sigma"))