https://github.com/poissonconsulting/tmbr
An R package to facilitate analyses using TMB
https://github.com/poissonconsulting/tmbr
mbr rstats tmb
Last synced: 7 months ago
JSON representation
An R package to facilitate analyses using TMB
- Host: GitHub
- URL: https://github.com/poissonconsulting/tmbr
- Owner: poissonconsulting
- License: other
- Created: 2016-09-20T19:57:03.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2025-01-14T21:06:11.000Z (9 months ago)
- Last Synced: 2025-02-01T18:42:59.358Z (8 months ago)
- Topics: mbr, rstats, tmb
- Language: R
- Homepage: https://poissonconsulting.github.io/tmbr/
- Size: 2.14 MB
- Stars: 0
- Watchers: 5
- Forks: 1
- Open Issues: 7
-
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-"
)
```[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://github.com/poissonconsulting/tmbr/actions/workflows/R-CMD-check.yaml)
[](https://codecov.io/gh/poissonconsulting/tmbr?branch=master)
[](https://opensource.org/license/mit/)
# tmbr## Introduction
`tmbr` (pronounced timber) is an R package to facilitate analyses using Template Model Builder ([`TMB`](https://github.com/kaskr/adcomp)).
It is part of the [mbr](https://github.com/poissonconsulting/mbr) family of packages.## Demonstration
```{r, message = FALSE}
library(magrittr)
library(ggplot2)
library(mbr)
library(tmbr)
``````{r}
model <- model("#includetemplate
Type objective_function::operator() () {DATA_VECTOR(Pairs);
DATA_VECTOR(Year);
DATA_FACTOR(Annual);
DATA_INTEGER(nAnnual);PARAMETER(alpha);
PARAMETER(beta1);
PARAMETER(beta2);
PARAMETER(beta3);
PARAMETER_VECTOR(bAnnual);
PARAMETER(log_sAnnual);Type sAnnual = exp(log_sAnnual);
vector ePairs = Pairs;
Type nll = 0.0;
for(int i = 0; i < nAnnual; i++){
nll -= dnorm(bAnnual(i), Type(0), sAnnual, true);
}for(int i = 0; i < Pairs.size(); i++){
ePairs(i) = exp(alpha + beta1 * Year(i) + beta2 * pow(Year(i), 2) + beta3 * pow(Year(i), 3) + bAnnual(Annual(i)));
nll -= dpois(Pairs(i), ePairs(i), true);
}
ADREPORT(sAnnual)
return nll;
}")# add R code to calculate derived parameters
model %<>% update_model(new_expr = "
for (i in 1:length(Pairs)) {
log(prediction[i]) <- alpha + beta1 * Year[i] + beta2 * Year[i]^2 + beta3 * Year[i]^3 + bAnnual[Annual[i]]
}")# define data types and center year
model %<>% update_model(
gen_inits = function(data) list(alpha = 4, beta1 = 1, beta2 = 0, beta3 = 0, log_sAnnual = 0, bAnnual = rep(0, data$nAnnual)),
select_data = list("Pairs" = integer(), "Year*" = integer(), Annual = factor()),
random_effects = list(bAnnual = "Annual")
)data <- bauw::peregrine
data$Annual <- factor(data$Year)analysis <- analyse(model, data = data)
coef(analysis)
``````{r, message = FALSE}
year <- predict(analysis, new_data = "Year")ggplot(data = year, aes(x = Year, y = estimate)) +
geom_point(data = bauw::peregrine, aes(y = Pairs)) +
geom_line() +
expand_limits(y = 0)
```## Installation
To install from GitHub
```
install.packages("devtools")
devtools::install_github("poissonconsulting/tmbr")
```## Citation
```{r, comment="", echo=FALSE}
citation(package = "tmbr")
```## Contribution
Please report any [issues](https://github.com/poissonconsulting/tmbr/issues).
[Pull requests](https://github.com/poissonconsulting/tmbr/pulls) are always welcome.
## Code of Conduct
Please note that the tmbr 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.