2
2
demo(" SETUP" , package = " rstanarm" , verbose = FALSE , echo = FALSE , ask = FALSE )
3
3
4
4
source(paste0(ROOT , " ARM/Ch.4/earnings.data.R" ), local = DATA_ENV , verbose = FALSE )
5
+ earnings_dat <- with(DATA_ENV , data.frame (earn , height , male ))
5
6
6
7
# The stuff in sections 4.0 -- 4.3 is not very relevant
7
8
# Moreover, centering predictors is NOT recommended in the rstanarm package
@@ -10,16 +11,16 @@ source(paste0(ROOT, "ARM/Ch.4/earnings.data.R"), local = DATA_ENV, verbose = FAL
10
11
11
12
# These two models are essentially equivalent in the likelihood
12
13
# But the "same" priors affect the posterior differently
13
- post1 <- stan_glm(log(earn ) ~ height , data = DATA_ENV ,
14
+ post1 <- stan_glm(log(earn ) ~ height , data = earnings_dat ,
14
15
family = gaussian(link = " identity" ),
15
16
seed = SEED , refresh = REFRESH )
16
- # post2 <- stan_glm(earn ~ height, data = DATA_ENV ,
17
+ # post2 <- stan_glm(earn ~ height, data = earnings_dat ,
17
18
# family = gaussian(link = "log"),
18
19
# seed = SEED, refresh = REFRESH)
19
20
# and this does not even converge
20
21
21
22
# These models add terms to the right-hand side
22
- post3 <- stan_lm(log(earn ) ~ height + male , data = DATA_ENV ,
23
+ post3 <- stan_lm(log(earn ) ~ height + male , data = earnings_dat ,
23
24
prior = R2(location = 0.3 , what = " mean" ),
24
25
seed = SEED , refresh = REFRESH )
25
26
post4 <- update(post3 , formula = log(earn ) ~ height * male )
@@ -49,15 +50,18 @@ boxplot(y_men, outline = FALSE, col = "red", axes = FALSE, log = "y", ylim = YLI
49
50
axis(1 , at = 1 : ncol(y_men ), labels = MEN_SEQ , las = 3 )
50
51
51
52
# Prediction of the weight of mesquite trees
53
+ DATA_ENV <- new.env()
52
54
source(paste0(ROOT , " ARM/Ch.4/mesquite.data.R" ), local = DATA_ENV , verbose = FALSE )
55
+ tree_dat <- as.data.frame(do.call(cbind , as.list(DATA_ENV )))
56
+
53
57
CONTINUE1 <- tolower(readline(
54
58
paste(" A heads up: the next part of the demo (Predicting weight of mesquite trees )" ,
55
59
" prints many lines \n to the console as it runs many models and compares the results" ,
56
60
" Proceed? (y/n)" )
57
61
))
58
62
if (CONTINUE1 != " n" ) {
59
63
post5 <- stan_lm(weight ~ diam1 + diam2 + canopy_height + total_height +
60
- density + group , data = DATA_ENV ,
64
+ density + group , data = tree_dat ,
61
65
prior = R2(0.9 ), seed = SEED , refresh = REFRESH )
62
66
post6 <- update(post5 , formula = log(weight ) ~ log(diam1 ) + log(diam2 ) +
63
67
log(canopy_height ) + log(total_height ) + log(density ) + group )
@@ -80,11 +84,13 @@ CONTINUE2 <- tolower(readline(
80
84
" Proceed? (y/n)" )
81
85
))
82
86
if (CONTINUE2 != " n" ) {
83
- YEARS <- as.character(seq(from = 1972 , to = 2000 , by = 4 ))
87
+ YEARS <- as.character(seq(from = 1972 , to = 1980 , by = 4 ))
84
88
round(digits = 2 , x = sapply(YEARS , FUN = function (YEAR ) {
89
+ DATA_ENV <- new.env()
85
90
source(paste0(ROOT , " ARM/Ch.4/nes" , YEAR , " .data.R" ), local = DATA_ENV , verbose = FALSE )
91
+ pid_dat <- as.data.frame(do.call(cbind , as.list(DATA_ENV )))
86
92
coef(stan_lm(partyid7 ~ real_ideo + I(race_adj == 1 ) + as.factor(age_discrete ) +
87
- educ1 + gender + income , data = DATA_ENV , prior = R2(0.5 ),
93
+ educ1 + gender + income , data = pid_dat , prior = R2(0.5 ),
88
94
seed = SEED , refresh = 0 ))
89
95
}))
90
96
}
0 commit comments