3.1 one predictor

reference:
- ARM chapter 03, github

library(tidyverse)
library(knitr)
options(show.signif.stars = FALSE)
# 3.1 one_predictor.R
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
library(shinystan)

one predictor

data

### Data
source("kidiq.data.R", echo = TRUE)
## 
## > N <- 434
## 
## > kid_score <- c(65, 98, 85, 83, 115, 98, 69, 106, 102, 
## +     95, 91, 58, 84, 78, 102, 110, 102, 99, 105, 101, 102, 115, 
## +     100, 87, 99, 96, 72,  .... [TRUNCATED] 
## 
## > mom_hs <- c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 
## +     1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 
## +     1, 0, 1, 1, 1, 1, 1, 1, 1, .... [TRUNCATED] 
## 
## > mom_iq <- c(121.117528602603, 89.3618817100663, 115.443164881725, 
## +     99.4496394360723, 92.7457099982118, 107.901837758501, 138.893106071162, 
## +  .... [TRUNCATED] 
## 
## > mom_hs_new <- 1
## 
## > mom_iq_new <- 100

categorical predictor

model

kidscore_momhs.stan


data {
  int N;
  vector[N] kid_score;
  vector[N] mom_hs;
}
parameters {
  vector[2] beta;
  real sigma;
}
model {
  sigma ~ cauchy(0, 2.5);
  kid_score ~ normal(beta[1] + beta[2] * mom_hs, sigma);
}

fit

### First model: kid_score ~ mom_hs
data.list.1 <- c("N", "kid_score", "mom_hs")
kidscore_momhs <- stan(file = "kidscore_momhs.stan", data = data.list.1, iter = 500)
kidscore_momhs
## Inference for Stan model: kidscore_momhs.
## 4 chains, each with iter=500; warmup=250; thin=1; 
## post-warmup draws per chain=250, total post-warmup draws=1000.
## 
##             mean se_mean   sd     2.5%      25%      50%      75%    97.5%
## beta[1]    77.47    0.12 2.28    72.62    75.99    77.61    78.91    81.81
## beta[2]    11.89    0.14 2.53     7.11    10.21    11.91    13.48    17.33
## sigma      19.90    0.03 0.67    18.64    19.42    19.90    20.37    21.20
## lp__    -1515.71    0.07 1.33 -1519.07 -1516.31 -1515.35 -1514.75 -1514.23
##         n_eff Rhat
## beta[1]   353 1.00
## beta[2]   325 1.01
## sigma     480 1.00
## lp__      372 1.01
## 
## Samples were drawn using NUTS(diag_e) at Wed Aug 23 06:23:04 2017.
## 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).
plot(kidscore_momhs)
## ci_level: 0.8 (80% intervals)
## outer_level: 0.95 (95% intervals)