You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages.Rmd
+59-43
Original file line number
Diff line number
Diff line change
@@ -1,35 +1,39 @@
1
1
# R Packages
2
2
3
-
Here I will go into a bit of detail regarding <spanclass="pack">rstanarm</span> and <spanclass="pack">brms</span>. For standard models, these should be your first choice, rather than using Stan directly. Why? For one, the underlying code that is used will be more optimized and efficient than what you come up with, and has had multiple individuals developing that code and hundreds actually using it. Furthermore, you can still, and probably should, set your priors as you wish.
3
+
Here I will go into a bit of detail regarding <spanclass="pack">rstanarm</span> and <spanclass="pack">brms</span>. For standard models, these should be your first choice, rather than using Stan directly. Why? For one, the underlying code that is used will be more optimized and efficient than what you come up with, and also, that code has had multiple individuals developing it and hundreds actually using it. Furthermore, you can still, and probably should, set your priors as you wish.
4
4
5
5
The nice thing about both is that you use the same syntax that you do for [R modeling](https://m-clark.github.io/R-models/) in general. Here is a a basic GLM in both.
6
6
7
7
```{r pack_demo, eval=FALSE}
8
-
stan_glm(y ~ x + z, data=d)
9
-
brm(y ~ x + z, data=d)
8
+
stan_glm(y ~ x + z, data = d)
9
+
10
+
brm(y ~ x + z, data = d)
10
11
```
11
12
12
13
And here are a couple complexities thrown in to show some minor differences. For example, the priors are specified a bit differently, and you may have options for one that you won't have in the other, but both will allow passing standard arguments, like cores, chains, etc. to <spanclass="pack">rstan</span>.
prior = prior(normal(0, 1), class = b, cauchy(0, 5), class = sd),
33
+
chains = 2,
34
+
cores = 2,
35
+
iter = 2000
36
+
)
33
37
```
34
38
35
39
So the syntax is easy to use for both of them, and to a point identical to standard R modeling syntax, and both have the same <spanclass="pack">rstan</span> arguments. However, you'll need to know what's available to tweak and how to do so specifically for each package.
@@ -61,20 +65,22 @@ It's not a good thing that for the most common linear models R has multiple func
61
65
62
66
Contrast this with <spanclass="pack">brms</span>, which only requires the <spanclass="func">brm</span> function and appropriate family, e.g. 'poisson' or 'categorical', and which can do multinomial models also.
63
67
64
-
However, if you want to do a standard linear regression, I would not recommend using stan_lm, as it requires a prior for the $R^2$, which is unfamiliar and only explained in technical ways that are likely going to be lost on those less comfortable with or new to statistical or Bayesian analysis[^stan_lm_vignette]. The good news is that you can simply run stan_glm instead, and work with the prior on the regression coefficients as we have discussed, and you can use <spanclass="func">bayes_R2</span> to get the $R^2$.
68
+
However, if you want to do a standard linear regression, I would probably not recommend using <spanclass="func">stan_lm</span>, as it requires a prior for the $R^2$, which is unfamiliar and only explained in technical ways that are likely going to be lost on those less comfortable with, or new to, statistical or Bayesian analysis[^stan_lm_vignette]. The good news is that you can simply run <spanclass="func">stan_glm</span> instead, and work with the prior on the regression coefficients as we have discussed, and you can use <spanclass="func">bayes_R2</span> to get the $R^2$.
69
+
65
70
71
+
You can certainly use <spanclass="pack">brms</span> for GLM, but it would have to compile the code first, and so will always be relatively, but not prohibitively, slower. For linear models with interactions or GLM generally, you may prefer it for the marginal effects plots and other additional functionality.
66
72
67
-
You can certainly use <spanclass="pack">brms</span> for GLM, but it would have to compile the code and so will always be notably slower. For LM with interactions or GLM generally, you may prefer it for the marginal effects plots.
68
73
69
74
70
75
## Categorical Models
71
76
72
77
73
-
If you're just doing a standard logistic regression, I'd suggest <spanclass="func">stan_glm</span>, again, for the speed. In addition, it has a specific model function for conditional logistic regression (<spanclass="func">stan_clogit</span>). Beyond that, I'd probably recommend <spanclass="pack">brms</span>. For ordinal regression, <spanclass="func">stan_polr</span> goes back to requiring a prior for $R^2$, which is now the $R^2$ for the underlying latent variable of the ordinal outcome[^r2_polr]. Furthermore, <spanclass="pack">brms</span> has some ordinal-specific plots, as well as other types of ordinal regression (e.g. adjacent category) that allow the proportional odds assumption to be relaxed. It also can do multi-category models[^nomulti].
78
+
If you're just doing a standard logistic regression, I'd suggest <spanclass="func">stan_glm</span>, again, for the speed. In addition, it has a specific model function for conditional logistic regression (<spanclass="func">stan_clogit</span>). Beyond that, I'd recommend <spanclass="pack">brms</span>. For ordinal regression, <spanclass="func">stan_polr</span> goes back to requiring a prior for $R^2$, which is now the $R^2$ for the underlying latent variable of the ordinal outcome[^r2_polr]. Furthermore, <spanclass="pack">brms</span> has some ordinal-specific plots, as well as other types of ordinal regression (e.g. adjacent category) that allow the proportional odds assumption to be relaxed. It also can do multi-category models.
- <spanclass="func">stan_gamm4</span>: generalized additive mixed model in <spanclass="pack">lme4</span> style
112
118
113
-
I would probably just recommend <spanclass="pack">rstanarm</span> for stan_lmer and stan_glmer, as <spanclass="pack">brms</span> has more flexibility, and even would be recommended for the standard models if you want to estimate residual (co-)variance structure, e.g. autocorrelation. It also will do multivariate models, and one can use <spanclass="pack">mgcv</span>::<spanclass="func">s</span> for smooth terms in *any* <spanclass="pack">brms</span> model.
119
+
I would probably just recommend <spanclass="pack">rstanarm</span> for stan_lmer and stan_glmer, as <spanclass="pack">brms</span> has more flexibility, and even would be recommended for the standard models if you want to estimate residual (co-)variance structure, e.g. autocorrelation. It also will do multivariate models, and one can use <spanclass="pack">mgcv</span>::<spanclass="func">s</span> for smooth terms in any <spanclass="pack">brms</span> model.
114
120
115
121
116
122
## Other Models and Related
117
123
118
-
Along with all those <spanclass="pack">rstanarm</span> has specific functions for [beta regression](http://mc-stan.org/<spanclass="pack">rstanarm</span>/articles/betareg.html), [joint mixed/survival models](http://mc-stan.org/<spanclass="pack">rstanarm</span>/articles/jm.html), and [regularized linear regression](http://mc-stan.org/<spanclass="pack">rstanarm</span>/articles/lm.html). <spanclass="pack">brms</span> has many more distributional families, can do hypothesis testing[^], has marginal effects plots, and more. Both have plenty of tools for diagnostics, posterior predictive checks, and more of what has been discussed previously.
124
+
Along with all those <spanclass="pack">rstanarm</span> has specific functions for [beta regression](http://mc-stan.org/rstanarm/articles/betareg.html), [joint mixed/survival models](http://mc-stan.org/rstanarm/articles/jm.html), and [regularized linear regression](http://mc-stan.org/rstanarm/articles/lm.html). <spanclass="pack">brms</span> has many more distributional families, can do hypothesis testing[^], has marginal effects plots, and more. Both have plenty of tools for diagnostics, posterior predictive checks, and more of what has been discussed previously.
119
125
120
-
In general, <spanclass="pack">rstanarm</span> is a great tool for translating your standard models into Bayesian ones in an efficient fashion. On the other hand, <spanclass="pack">brms</span> uses a simplified syntax and is notably more flexible. Here is a brief summary of my recommend use.
126
+
In general, <spanclass="pack">rstanarm</span> is a great tool for translating your standard models into Bayesian ones in an efficient fashion. On the other hand, <spanclass="pack">brms</span> uses a simplified syntax and is notably more flexible. Here is a brief summary of my recommend use for common regression models.
Besides that, if you still need to model complexity not found within those, you can *still* use them to generate some highly optimized starter code, as they have functions for solely generating the underlying Stan code.
147
+
Besides that, if you still need to model complexity not found within those, you can still use them to generate some highly optimized starter code, as they have functions for solely generating the underlying Stan code.
131
148
132
149
133
150
## Even More Packages
134
151
135
-
I've focused on the two widely-used general-purpose packages, but nothing can stop Stan at this point. Here is a visualization of the current <spanclass="pack">rstan</span> ecosystem.
152
+
I've focused on the two widely-used general-purpose packages, but nothing can stop Stan at this point. As of the latest update to this document, there are now `r length(devtools::revdep('rstan'))` other packages depending on <spanclass="pack">rstan</span> in some way. There are also interfaces for Python, Julia, and more. Odds are good you'll find one to suit your needs.
153
+
154
+
```{r stan_getgraph, eval=FALSE, echo=FALSE}
155
+
# no longer works, and wasn't easily duplicated with e.g. devtools::revdep
At this point there are already a couple dozen packages working with Stan under the hood. Odds are good you'll find one to suit your needs.
179
-
180
197
181
-
[^stan_lm_vignette]: The developers note in their vignette for <spanclass="func">stan_aov</span>:<br><br>'but it is reasonable to expect a researcher to have a plausible guess for R2 before conducting an ANOVA.'<br><br> Actually, I'm not sure how reasonable this is. I see many, many researchers of varying levels of expertise, and I don't think any of them would be able to hazard much of a guess about $R^2$ before running a model, unless they're essentially duplicating previous work. I also haven't come across an explanation in the documentation (which is otherwise great) of how to specify it that would be very helpful to people just starting out with Bayesian analysis or even statistics in general. If the result is that one then has to try a bunch of different priors, then that becomes the focus of the analytical effort, which likely won't appeal to people just wanting to run a standard regression model.
182
198
183
-
[^r2_polr]: If someone tells me they know what the prior should be for that, I probably would not believe them.
199
+
[^stan_lm_vignette]: The developers note in their [vignette for ANOVA](https://cran.r-project.org/web/packages/rstanarm/vignettes/aov.html):<br><br>'but it is reasonable to expect a researcher to have a plausible guess for R2 before conducting an ANOVA.'<br><br> Actually, I'm not sure how reasonable this is. In consulting I've seen many, many researchers of varying levels of expertise, and I don't think even a large minority of them would be able to hazard much of a guess about $R^2$ before running a model, unless they're essentially duplicating previous work. I also haven't come across an explanation in the documentation (which is otherwise great) of how to specify it that would be very helpful to people just starting out with Bayesian analysis or even statistics in general. If the result is that one then has to try a bunch of different priors, then that becomes the focus of the analytical effort, which likely won't appeal to people just wanting to run a standard regression model.
184
200
185
-
[^nomulti]: The corresponding distribution is the categorical distribution, which is a multinomial distribution with size = 1. Multinomial count models, i.e. with size > 1, on the other hand, are not currently supported [except indirectly](https://github.com/paul-buerkner/brms/issues/338). However, the multinomial-poisson transformation can be used instead.
201
+
[^r2_polr]: If someone tells me they know what the prior should be for that, I probably would not believe them.
0 commit comments