Skip to content

An R package to facilitate analyses using TMB

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

poissonconsulting/tmbr

Repository files navigation

Lifecycle: stable R-CMD-check Codecov test coverage License: MIT

tmbr

Introduction

tmbr (pronounced timber) is an R package to facilitate analyses using Template Model Builder (TMB). It is part of the mbr family of packages.

Demonstration

library(magrittr)
library(ggplot2)
library(mbr)
library(tmbr)
model <- model("#include <TMB.hpp>

template<class Type>
Type objective_function<Type>::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<Type> 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)
#> Note: Using Makevars in /Users/joe/.R/Makevars
#> using C++ compiler: 'Apple clang version 14.0.3 (clang-1403.0.22.14.1)'
#> using SDK: ''
#> # A tibble: 1 × 5
#>       n     K logLik    IC converged
#>   <int> <int>  <dbl> <dbl> <lgl>    
#> 1    40     5  -154.  321. TRUE     
#> Warning: 4 external pointers will be removed

coef(analysis)
#> # A tibble: 5 × 8
#>   term        estimate   lower   upper svalue     sd  zscore   pvalue
#>   <term>         <dbl>   <dbl>   <dbl>  <dbl>  <dbl>   <dbl>    <dbl>
#> 1 alpha         4.26    4.19    4.34        1 0.0379 112.    0       
#> 2 beta1         1.19    1.05    1.33        1 0.0697  17.1   2.10e-65
#> 3 beta2        -0.0177 -0.0743  0.0390      1 0.0289  -0.611 5.41e- 1
#> 4 beta3        -0.272  -0.342  -0.202       1 0.0357  -7.62  2.63e-14
#> 5 log_sAnnual  -2.31   -2.84   -1.78        1 0.271   -8.53  1.45e-17
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

To cite tmbr in publications use:

  Joe Thorley (2018) tmbr: Analyses Using TMB. doi:
  https://doi.org/10.5281/zenodo.1162374.

A BibTeX entry for LaTeX users is

  @Misc{,
    author = {Joe Thorley},
    year = {2018},
    title = {tmbr: Analyses Using TMB},
    doi = {https://doi.org/10.5281/zenodo.1162374},
  }

Please also cite TMB.

Contribution

Please report any issues.

Pull requests are always welcome.

Code of Conduct

Please note that the tmbr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

About

An R package to facilitate analyses using TMB

Topics

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages