glmmstan: Generalized liner mixed model in Rstan
> library(glmmstan)
Loading required package: rstan
rstan (Version 2.8.2, packaged: 2015-11-26 15:27:02 UTC, GitRev: 05c3d0058b6a)
For execution on a local, multicore CPU with excess RAM we recommend calling
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
Attaching package: 'rstan'
The following object is masked from 'package:raster':
extract
The following object is masked from 'package:magrittr':
extract
> data("baseball")
バージョン: 1.31
関数名 | 概略 |
---|---|
baseball |
Baseball player's perfomance data |
glmmstan |
Generalized liner mixed model in Rstan using glmer-style formulas |
glmmstan-package |
Generalized liner mixed model in Rstan |
iccstan |
intra-class correlations in rstan |
baseball
野球選手の成績や所属チームのデータセット
> data("baseball")
> head(baseball)
player team league AVR ATbase ATbats HIT HR RBI steal walk K salary
1 1 1 1 0.324 685 596 193 29 89 15 74 95 80
2 2 1 1 0.305 637 580 177 10 69 2 43 62 85
3 3 1 1 0.316 597 547 173 23 90 10 48 103 60
4 4 1 1 0.310 464 422 131 17 79 2 36 54 64
5 5 1 1 0.301 446 366 110 31 69 2 75 95 297
6 6 1 1 0.298 365 325 97 5 41 0 26 51 40
salary_log Cluster
1 4.382027 1
2 4.442651 1
3 4.094345 2
4 4.158883 2
5 5.693732 3
6 3.688879 2
glmmstan
RStanを利用した一般化線形混合モデルの実行
Arguments
- formula_str
- data
- family
- center
- offset
- codeonly
- dataonly
- modelonly
- cauchy
- parallel
- cores
- iter
- warmup
- thin
> fit <- glmmstan(salary_log ~ 1, data = baseball, family = "gaussian")
> output_result(fit)
>
> fit <- glmmstan(HR ~ 1 + (1 | team), data = baseball, family = "poisson")
> output_result(fit)
>
> fit <- glmmstan(salary ~ HR + K + (HR | team), data = baseball, family = "lognormal")
> output_stan(fit)
>
> # 打率がリーグごとに異なるか
> df_team <- baseball %>% dplyr::filter(team %in% c(1, 2))
> fit <- df_team %>% glmmstan(cbind(HIT, ATbats - HIT) ~ team + (1 | player),
+ data = .,
+ family = "binomial")
> baseball %>% ggplot(aes(factor(team), AVR)) + geom_boxplot()
> baseball %>% ggplot(aes(factor(team), HR)) + geom_boxplot()