https://github.com/poissonconsulting/smbr
R package to facilitate analyses using STAN
https://github.com/poissonconsulting/smbr
mbr rstats stan
Last synced: 2 months ago
JSON representation
R package to facilitate analyses using STAN
- Host: GitHub
- URL: https://github.com/poissonconsulting/smbr
- Owner: poissonconsulting
- License: other
- Created: 2017-05-18T16:11:35.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2024-11-12T17:12:33.000Z (11 months ago)
- Last Synced: 2024-11-12T18:24:22.087Z (11 months ago)
- Topics: mbr, rstats, stan
- Language: R
- Homepage: https://poissonconsulting.github.io/smbr/
- Size: 8.11 MB
- Stars: 1
- Watchers: 5
- Forks: 1
- Open Issues: 10
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Support: .github/SUPPORT.md
Awesome Lists containing this project
README
---
output: github_document
---```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "tools/README-"
)
```# smbr
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://github.com/poissonconsulting/smbr/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/poissonconsulting/smbr)
[](https://opensource.org/license/mit/)## Introduction
`smbr` (pronounced simber) is an R package to facilitate analyses using [`STAN`](http://mc-stan.org).
It is part of the [embr](https://github.com/poissonconsulting/embr) family of packages.## Demonstration
```{r, message = FALSE}
library(bauw)
library(ggplot2)
library(magrittr)
library(embr)
library(smbr)
``````{r}
# define model in Stan language
model <- model("
data {
int nAnnual;
int nObs;
int Annual[nObs];
int Pairs[nObs];
real Year[nObs];
}
parameters {
vector[nAnnual] bAnnual;
real log_sAnnual;
real alpha;
real beta1;
real beta2;
real beta3;
}
transformed parameters {
real sAnnual;
sAnnual = exp(log_sAnnual);
}
model {
vector[nObs] ePairs;log_sAnnual ~ normal(0, 10);
bAnnual ~ normal(0, sAnnual);alpha ~ normal(0, 10);
beta1 ~ normal(0, 10);
beta2 ~ normal(0, 10);
beta3 ~ normal(0, 10);for (i in 1:nObs) {
ePairs[i] = exp(alpha + beta1 * Year[i] + beta2 * Year[i]^2 +
beta3 * Year[i]^3 + bAnnual[Annual[i]]);
}
target += poisson_lpmf(Pairs | ePairs);
}")# add R code to calculate derived parameters
model %<>% update_model(new_expr = "
for (i in 1:length(Pairs)) {
prediction[i] <- exp(alpha + beta1 * Year[i] + beta2 * Year[i]^2 +
beta3 * Year[i]^3 + bAnnual[Annual[i]])
}
")# define data types and center year
model %<>% update_model(
select_data = list(
"Pairs" = integer(), "Year*" = integer(),
Annual = factor()
),
derived = "sAnnual",
random_effects = list(bAnnual = "Annual")
)data <- bauw::peregrine
data$Annual <- factor(data$Year)set.seed(42)
# analyse
analysis <- analyse(model, data = data, seed = 3L, glance = FALSE)# coefficient table
coef(analysis, simplify = TRUE)# trace plots
plot(analysis)
```
```{r, message = FALSE}
# make predictions by varying year with other predictors including the random effect of Annual held constant
year <- predict(analysis, new_data = "Year")# plot those predictions
ggplot(data = year, aes(x = Year, y = estimate)) +
geom_point(data = bauw::peregrine, aes(y = Pairs)) +
geom_line() +
geom_line(aes(y = lower), linetype = "dotted") +
geom_line(aes(y = upper), linetype = "dotted") +
expand_limits(y = 0)
```## Installation
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("poissonconsulting/smbr")
```## Citation
```{r, comment="", echo=FALSE}
citation(package = "smbr")
```## Contribution
Please report any [issues](https://github.com/poissonconsulting/smbr/issues).
[Pull requests](https://github.com/poissonconsulting/smbr/pulls) are always welcome.
## Code of Conduct
Please note that the smbr project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.