class: center, middle, inverse, title-slide # Unit 1 ### Jung-Jin Lee ### Last modified: Nov 03, 2019 --- ## List of packages used ```r library(tidyverse) library(broom) library(gt) library(rstan) # the following are recommended in startup messages *options(mc.cores = parallel::detectCores()) *rstan_options(auto_write = TRUE) ``` Stan user's guide https://mc-stan.org/docs/2_18/stan-users-guide/index.html --- ## Installation of RStan Follow instruction in https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started: - Remove existing `RStan`, if any: ```r remove.packages("rstan") if (file.exists(".RData")) file.remove(".RData") ``` - Restart `R` and install `rstan` package: ```r install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE) ``` - Make sure you have the necessary C++ toolchain: ```r pkgbuild::has_build_tools(debug = TRUE) ``` ``` ## [1] TRUE ``` --- ## Example: simple linear regression `mtcars` is one of built-in data sets in `R`. ```r head(mtcars) %>% rownames_to_column("name") %>% gt(rowname_col = vars(name)) ```
name
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
Mazda RX4
21.0
6
160
110
3.90
2.620
16.46
0
1
4
4
Mazda RX4 Wag
21.0
6
160
110
3.90
2.875
17.02
0
1
4
4
Datsun 710
22.8
4
108
93
3.85
2.320
18.61
1
1
4
1
Hornet 4 Drive
21.4
6
258
110
3.08
3.215
19.44
1
0
3
1
Hornet Sportabout
18.7
8
360
175
3.15
3.440
17.02
0
0
3
2
Valiant
18.1
6
225
105
2.76
3.460
20.22
1
0
3
1
--- ## Example: simple linear regression Scatter plot showing the relation between `wt` and `mpg`: ```r mtcars %>% ggplot(aes(wt, mpg)) + geom_point() + geom_smooth(method = "lm", se = FALSE) ``` <img src="Stan_Unit_1_files/figure-html/unnamed-chunk-7-1.png" width="50%" style="display: block; margin: auto;" /> --- ## Example: simple linear regression Simple linear regression using `lm()`: ```r fit <- lm(mpg ~ wt, data = mtcars) tidy(fit) %>% gt(rowname_col = "term") %>% fmt_number(columns = vars(estimate, std.error, statistic), decimals = 2) %>% fmt_scientific(columns = vars(p.value), decimals = 2) ```
estimate
std.error
statistic
p.value
(Intercept)
37.29
1.88
19.86
8.24 × 10
−19
wt
−5.34
0.56
−9.56
1.29 × 10
−10
--- ## Example: simple linear regression In particular, the 95% confidence intervals of the coefficients are given below: ```r confint(fit) %>% as.data.frame() %>% rownames_to_column("term") %>% gt(rowname_col = "term") %>% fmt_number(columns = everything(), decimals = 2) ```
2.5 %
97.5 %
(Intercept)
33.45
41.12
wt
−6.49
−4.20
--- ## Example: simple linear regression The following model is saved as `simple_lm.stan`: ```bash cat simple_lm.stan ``` ``` data { int<lower=0> N; vector[N] x; vector[N] y; } parameters { real alpha; real beta; real<lower=0> sigma; } model { y ~ normal(alpha + beta * x, sigma); } ``` --- ## Example: simple linear regression Data need to be provided: ```r mtcarsData <- list(N = nrow(mtcars), y = mtcars$mpg, x = mtcars$wt) fit_stan <- stan(file = 'simple_lm.stan', data = mtcarsData) ``` Recall that previously the following was obtained using `lm()`:
estimate
std.error
statistic
p.value
(Intercept)
37.29
1.88
19.86
8.24 × 10
−19
wt
−5.34
0.56
−9.56
1.29 × 10
−10
2.5 %
97.5 %
(Intercept)
33.45
41.12
wt
−6.49
−4.20
--- ## Example: simple linear regression ```r print(fit_stan) ``` ``` Inference for Stan model: simple_lm. 4 chains, each with iter=2000; warmup=1000; thin=1; post-warmup draws per chain=1000, total post-warmup draws=4000. mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat alpha 37.23 0.05 1.94 33.41 35.98 37.18 38.49 41.09 1334 1.00 beta -5.33 0.02 0.58 -6.48 -5.70 -5.32 -4.95 -4.19 1439 1.00 sigma 3.19 0.01 0.43 2.46 2.88 3.15 3.44 4.14 1315 1.00 lp__ -51.05 0.04 1.23 -54.25 -51.61 -50.72 -50.16 -49.64 1164 1.01 Samples were drawn using NUTS(diag_e) at Mon Oct 21 22:57:08 2019. For each parameter, n_eff is a crude measure of effective sample size, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat=1). ``` --- ## Example: simple linear regression Run `stan()` with options: ```r fit_stan2 <- stan(file = 'simple_lm.stan', data = mtcarsData, seed = 2019, chains = 5, iter = 10000, warmup = 3000) ``` --- ## Example: simple linear regression ```r print(fit_stan2) ``` ``` Inference for Stan model: simple_lm. 5 chains, each with iter=10000; warmup=3000; thin=1; post-warmup draws per chain=7000, total post-warmup draws=35000. mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat alpha 37.28 0.02 1.99 33.30 35.98 37.29 38.60 41.23 11621 1 beta -5.34 0.01 0.60 -6.52 -5.74 -5.35 -4.95 -4.16 11620 1 sigma 3.18 0.00 0.44 2.47 2.87 3.13 3.44 4.17 15359 1 lp__ -51.11 0.01 1.30 -54.47 -51.69 -50.78 -50.17 -49.63 10790 1 Samples were drawn using NUTS(diag_e) at Mon Oct 21 23:46:03 2019. For each parameter, n_eff is a crude measure of effective sample size, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat=1). ``` --- ## Example: simple linear regression ```r traceplot(fit_stan2) ``` <img src="Stan_Unit_1_files/figure-html/unnamed-chunk-15-1.png" width="60%" style="display: block; margin: auto;" /> --- ## Example: simple linear regression Output from `stan()` can be extracted: ```r df <- as.data.frame(fit_stan2) dim(df) # iter = 10000, warmup = 3000, chains = 5 ``` ``` ## [1] 35000 4 ``` ```r head(df) ``` ``` ## alpha beta sigma lp__ ## 1 38.48587 -5.295838 3.269406 -52.50245 ## 2 38.42898 -5.334596 3.259406 -51.80863 ## 3 38.45064 -5.414004 3.401005 -51.21041 ## 4 38.55132 -5.426852 3.555980 -51.60899 ## 5 37.79401 -5.771271 2.736554 -51.74718 ## 6 38.02972 -5.688517 2.731914 -50.31771 ``` --- ## Example: logistic regression ```r # virginica or not dIris <- iris %>% mutate(IsVirginica = ifelse(Species == "virginica", 1, 0)) fit <- glm(IsVirginica ~ Sepal.Length + Sepal.Width, data = dIris, family = binomial(link = "logit")) tidy(fit) %>% gt(rowname_col = "term") %>% fmt_number(columns = vars(estimate, std.error, statistic), decimals = 2) %>% fmt_scientific(columns = vars(p.value), decimals = 2) ```
estimate
std.error
statistic
p.value
(Intercept)
−14.18
3.12
−4.55
5.36 × 10
−6
Sepal.Length
2.60
0.44
5.91
3.48 × 10
−9
Sepal.Width
−0.75
0.64
−1.16
2.45 × 10
−1
--- ## Example: logistic regression ```r confint(fit) %>% as.data.frame() %>% rownames_to_column("term") %>% gt(rowname_col = "term") %>% fmt_number(columns = everything(), decimals = 2) ```
2.5 %
97.5 %
(Intercept)
−20.85
−8.56
Sepal.Length
1.82
3.57
Sepal.Width
−2.09
0.45
--- ## Example: logistic regression The following model is saved as `logistic_2.stan`: ```bash cat logistic_2.stan ``` ``` data { int<lower=0> N; vector[N] x1; vector[N] x2; int<lower=0, upper=1> y[N]; } parameters { real alpha; real beta1; real beta2; } model { y ~ bernoulli_logit(alpha + beta1 * x1 + beta2 * x2); } ``` --- ## Example: logistic regression ```r irisData <- list(N = nrow(dIris), y = dIris$IsVirginica, x1 = dIris$Sepal.Length, x2 = dIris$Sepal.Width) fit_logit <- stan(file = 'logistic_2.stan', data = irisData, seed = 2019, chains = 5, iter = 10000, warmup = 3000) ``` --- ## Example: logistic regression ```r print(fit_logit) ``` ``` Inference for Stan model: logistic_2. 5 chains, each with iter=10000; warmup=3000; thin=1; post-warmup draws per chain=7000, total post-warmup draws=35000. mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat alpha -14.87 0.03 3.21 -21.57 -16.91 -14.71 -12.66 -9.06 10388 1 beta1 2.74 0.00 0.46 1.92 2.42 2.71 3.04 3.74 10766 1 beta2 -0.82 0.01 0.65 -2.14 -1.24 -0.79 -0.37 0.40 12745 1 lp__ -59.47 0.01 1.26 -62.74 -60.02 -59.14 -58.56 -58.06 9382 1 Samples were drawn using NUTS(diag_e) at Sun Nov 3 11:49:04 2019. For each parameter, n_eff is a crude measure of effective sample size, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat=1). ``` --- ## Example: logistic regression ```r traceplot(fit_logit) ``` <img src="Stan_Unit_1_files/figure-html/unnamed-chunk-21-1.png" width="60%" style="display: block; margin: auto;" />