Skip to contents

Gets a copy of the BUGS code and writes it to the screen and (optionally) to a text file

Usage

get_bmaBUGScode(
  option = "smooth",
  print.screen = FALSE,
  save.local = FALSE,
  incl.2deriv = FALSE,
  Y1perfect = FALSE,
  seFromData = FALSE
)

Arguments

option

text string defining which particular variant is desired

print.screen

Logical, should the code be printed to the console?

save.local

Logical, should text file of the BUGS code be produced?

incl.2deriv

Logical, Should there be a calculation of the indicator's second derivative?

Y1perfect

Logical, should the first year for each species be assumed to be known without error (`TRUE`)

seFromData

Logical, Should the standard errors be read in from data (`TRUE`) or estimated (`FALSE`)

Details

There are a number of model to choose from:

  • "smooth" The default option. Indicator defined by Growth rates, with Ruppert smoother, allowing for species to join late. Error on the first year of each species' time-series is assumed to be zero. The indicator is the expected value of the geometric mean across species (with missing species imputed). Includes three options: `seFromData` `Y1perfect` and `incl.2deriv`. See bayesian_meta_analysis for mode details. Using the default values `seFromData = FALSE` and `Y1perfect = TRUE` are the options used in Freeman et al. (2020).

  • "smooth_det2" Equivalent to smooth with `seFromData = TRUE` and `Y1perfect = FALSE`. Retained for backwards compatability. Choosing this option will overwrite user-entered options for `seFromData` and `Y1perfect`.

  • "smooth_det_sigtheta" Equivalent to smooth with `seFromData = FALSE` and `Y1perfect = FALSE`. Retained for backwards compatability. Choosing this option will overwrite user-entered options for `seFromData` and `Y1perfect`.

  • "smooth_det" Specific variant of smooth_det2 - deprecated but retained here for backward compatibility

Examples

get_bmaBUGScode(option="smooth", print.screen=TRUE, save.local=FALSE)
#> model {
#>     ###################  Define priors  ###########################
#> 
#>   # process errors
#>   tau.spi <- pow(sigma.spi,-2)
#>   sigma.spi ~ dunif(0.0001,30)
#> 
#>   # observation error is constant
#>   theta ~ dunif(0.0001,10)
#> 
#>     ######################### Smoothing #######################
#> 
#>     beta[1] ~ dnorm(0, 0.000001)
#>     beta[2] ~ dnorm(0, 0.000001)
#>     taub ~ dgamma(0.000001, 0.000001)
#>     for(k in 1:num.knots){b[k]~dnorm(0,taub)}
#>   
#>     for (t in 1:(nyears - 1)){
#>       logLambda[t] <- m[t]
#>       m[t] <- mfe[t] + mre[t]
#>       mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]
#>       for (k in 1:num.knots){
#>         temp[t,k] <- b[k] * Z[t,k]
#>       }
#>       mre[t]<-sum(temp[t,1:num.knots])
#>     }  
#>   
#>     ###################  Define likelihood  ###########################
#> 
#>     M[1] <- 0 
#>   
#>     for (t in 2:nyears){
#>       M[t] <- M[t-1] + logLambda[t-1]
#>     }
#> 
#>     for (s in 1:nsp){
#>       for (t in 1:(nyears-1)){
#>         spgrowth[s,t] ~ dnorm(logLambda[t], tau.spi)
#>     }}
#> 
#>     for (s in 1:nsp){
#>       for (t in 1:FY[s]){
#>         spindex[s,t] <- spindex[s,t+1] - spgrowth[s,t]
#>       }
#> 
#>       for (t in (FY[s]+1):(nyears)){
#>         spindex[s,t] <- estimate[s,FY[s]] + sum(spgrowth[s,FY[s]:(t-1)])
#>         estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])
#>         tau.obs[s,t] <- pow(theta, -2)
#>       }}
#> 
#>   ####################  geomean of expected values ######################
#>   
#>   for (t in 1:nyears){
#>     Mprime[t] <- sum(spindex[,t])/nsp
#>   }
#> 
#> }
#> [1] "model {\n    ###################  Define priors  ###########################\n\n  # process errors\n  tau.spi <- pow(sigma.spi,-2)\n  sigma.spi ~ dunif(0.0001,30)\n\n  # observation error is constant\n  theta ~ dunif(0.0001,10)\n\n    ######################### Smoothing #######################\n\n    beta[1] ~ dnorm(0, 0.000001)\n    beta[2] ~ dnorm(0, 0.000001)\n    taub ~ dgamma(0.000001, 0.000001)\n    for(k in 1:num.knots){b[k]~dnorm(0,taub)}\n  \n    for (t in 1:(nyears - 1)){\n      logLambda[t] <- m[t]\n      m[t] <- mfe[t] + mre[t]\n      mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]\n      for (k in 1:num.knots){\n        temp[t,k] <- b[k] * Z[t,k]\n      }\n      mre[t]<-sum(temp[t,1:num.knots])\n    }  \n  \n    ###################  Define likelihood  ###########################\n\n    M[1] <- 0 \n  \n    for (t in 2:nyears){\n      M[t] <- M[t-1] + logLambda[t-1]\n    }\n\n    for (s in 1:nsp){\n      for (t in 1:(nyears-1)){\n        spgrowth[s,t] ~ dnorm(logLambda[t], tau.spi)\n    }}\n\n    for (s in 1:nsp){\n      for (t in 1:FY[s]){\n        spindex[s,t] <- spindex[s,t+1] - spgrowth[s,t]\n      }\n\n      for (t in (FY[s]+1):(nyears)){\n        spindex[s,t] <- estimate[s,FY[s]] + sum(spgrowth[s,FY[s]:(t-1)])\n        estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])\n        tau.obs[s,t] <- pow(theta, -2)\n      }}\n\n  ####################  geomean of expected values ######################\n  \n  for (t in 1:nyears){\n    Mprime[t] <- sum(spindex[,t])/nsp\n  }\n\n}"
get_bmaBUGScode(option="smooth_JABES", print.screen=TRUE, save.local=FALSE)
#> model {
#>     ###################  Define priors  ###########################
#> 
#>   # process errors
#>   tau.spi <- pow(sigma.spi,-2)
#>   sigma.spi ~ dunif(0.0001,30)
#> 
#>   # observation error is constant
#>   theta ~ dunif(0.0001,10)
#> 
#>     ######################### Smoothing #######################
#> 
#>     beta[1] ~ dnorm(0, 0.000001)
#>     beta[2] ~ dnorm(0, 0.000001)
#>     taub ~ dgamma(0.000001, 0.000001)
#>     for(k in 1:num.knots){b[k]~dnorm(0,taub)}
#>   
#>     for (t in 1:(nyears - 1)){
#>       logLambda[t] <- m[t]
#>       m[t] <- mfe[t] + mre[t]
#>       mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]
#>       for (k in 1:num.knots){
#>         temp[t,k] <- b[k] * Z[t,k]
#>       }
#>       mre[t]<-sum(temp[t,1:num.knots])
#>     }  
#>   
#>     ###################  Define likelihood  ###########################
#> 
#>     M[1] <- 0 
#>   
#>     for (t in 2:nyears){
#>       M[t] <- M[t-1] + logLambda[t-1]
#>     }
#> 
#>     for (s in 1:nsp){
#>       for (t in 1:(nyears-1)){
#>         spgrowth[s,t] ~ dnorm(logLambda[t], tau.spi)
#>     }}
#> 
#>     for (s in 1:nsp){
#>       for (t in 1:(FY[s]-1)){
#>         spindex[s,t] <- spindex[s,t+1] - spgrowth[s,t]
#>     }
#> 
#>     spindex[s,FY[s]] <- estimate[s,FY[s]] # we assume the first year is known without error
#> 
#>     for (t in (FY[s]+1):(nyears)){
#>       spindex[s,t] <- estimate[s,FY[s]] + sum(spgrowth[s,FY[s]:(t-1)])
#>       estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])
#>       tau.obs[s,t] <- pow(theta, -2)
#>     }}
#> 
#>   ####################  geomean of expected values ######################
#>   
#>   for (t in 1:nyears){
#>     Mprime[t] <- sum(spindex[,t])/nsp
#>   }
#> 
#> }
#> [1] "model {\n    ###################  Define priors  ###########################\n\n  # process errors\n  tau.spi <- pow(sigma.spi,-2)\n  sigma.spi ~ dunif(0.0001,30)\n\n  # observation error is constant\n  theta ~ dunif(0.0001,10)\n\n    ######################### Smoothing #######################\n\n    beta[1] ~ dnorm(0, 0.000001)\n    beta[2] ~ dnorm(0, 0.000001)\n    taub ~ dgamma(0.000001, 0.000001)\n    for(k in 1:num.knots){b[k]~dnorm(0,taub)}\n  \n    for (t in 1:(nyears - 1)){\n      logLambda[t] <- m[t]\n      m[t] <- mfe[t] + mre[t]\n      mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]\n      for (k in 1:num.knots){\n        temp[t,k] <- b[k] * Z[t,k]\n      }\n      mre[t]<-sum(temp[t,1:num.knots])\n    }  \n  \n    ###################  Define likelihood  ###########################\n\n    M[1] <- 0 \n  \n    for (t in 2:nyears){\n      M[t] <- M[t-1] + logLambda[t-1]\n    }\n\n    for (s in 1:nsp){\n      for (t in 1:(nyears-1)){\n        spgrowth[s,t] ~ dnorm(logLambda[t], tau.spi)\n    }}\n\n    for (s in 1:nsp){\n      for (t in 1:(FY[s]-1)){\n        spindex[s,t] <- spindex[s,t+1] - spgrowth[s,t]\n    }\n\n    spindex[s,FY[s]] <- estimate[s,FY[s]] # we assume the first year is known without error\n\n    for (t in (FY[s]+1):(nyears)){\n      spindex[s,t] <- estimate[s,FY[s]] + sum(spgrowth[s,FY[s]:(t-1)])\n      estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])\n      tau.obs[s,t] <- pow(theta, -2)\n    }}\n\n  ####################  geomean of expected values ######################\n  \n  for (t in 1:nyears){\n    Mprime[t] <- sum(spindex[,t])/nsp\n  }\n\n}"
get_bmaBUGScode(option="smooth_det", print.screen=TRUE, save.local=FALSE)
#> model {
#> 
#>   ###################  Define priors
#>   # process errors
#>   tau.spi <- pow(sigma.spi,-2)
#>   sigma.spi ~ dunif(0,30)
#>   tau.sg <- pow(sigma.sg,-2)
#>   sigma.sg ~ dunif(0,1000)
#> 
#>   # observation errors
#>   # one value per site-species
#>   for (s in 1:nsp){   
#>     for (t in 1:nyears){
#>       sigma.obs[s,t] ~ dunif(0, max_se) # for the missing values
#>     }}
#>   
#>   for (s in 1:nsp){
#>     spindex[s,1]~dnorm(Mprime.raw[1],tau.spi)
#>   }
#>   
#>   ###################  Define likelihood  #######################
#>   
#>   for (t in 1:(nyears-1)){
#>     logLambda[t] <- Mprime.raw[t+1] - Mprime.raw[t]
#>   }
#>   
#>   for (s in 1:nsp){
#>     for (t in 1:(nyears-1)){
#>       spgrowth[s,t]~dnorm(logLambda[t],tau.sg)
#>     }
#>   }
#>   
#>   for (s in 1:nsp){
#>     for (t in 2:(nyears)){
#>       spindex[s,t]<-spindex[s,1] + sum(spgrowth[s,1:(t-1)])
#>     }
#>   }
#>   
#>   for (s in 1:nsp){
#>     for (t in 1:(nyears)){
#>       estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])
#>       tau.obs[s,t] <- pow(sigma.obs[s,t], -2)
#>     }
#>   }
#>   
#>   M <- m
#>   
#>   #########################  end likelihood ###########################
#>   
#> 
#>     ######################### Smoothing #######################
#> 
#>     beta[1] ~ dnorm(0, 0.000001)
#>     beta[2] ~ dnorm(0, 0.000001)
#>     taub ~ dgamma(0.000001, 0.000001)
#>     for(k in 1:num.knots){b[k]~dnorm(0,taub)}
#>   
#>     for (t in 1:(nyears - 1)){
#>       logLambda[t] <- m[t]
#>       m[t] <- mfe[t] + mre[t]
#>       mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]
#>       for (k in 1:num.knots){
#>         temp[t,k] <- b[k] * Z[t,k]
#>       }
#>       mre[t]<-sum(temp[t,1:num.knots])
#>     }  
#>   
#> }
#> [1] "model {\n\n  ###################  Define priors\n  # process errors\n  tau.spi <- pow(sigma.spi,-2)\n  sigma.spi ~ dunif(0,30)\n  tau.sg <- pow(sigma.sg,-2)\n  sigma.sg ~ dunif(0,1000)\n\n  # observation errors\n  # one value per site-species\n  for (s in 1:nsp){   \n    for (t in 1:nyears){\n      sigma.obs[s,t] ~ dunif(0, max_se) # for the missing values\n    }}\n  \n  for (s in 1:nsp){\n    spindex[s,1]~dnorm(Mprime.raw[1],tau.spi)\n  }\n  \n  ###################  Define likelihood  #######################\n  \n  for (t in 1:(nyears-1)){\n    logLambda[t] <- Mprime.raw[t+1] - Mprime.raw[t]\n  }\n  \n  for (s in 1:nsp){\n    for (t in 1:(nyears-1)){\n      spgrowth[s,t]~dnorm(logLambda[t],tau.sg)\n    }\n  }\n  \n  for (s in 1:nsp){\n    for (t in 2:(nyears)){\n      spindex[s,t]<-spindex[s,1] + sum(spgrowth[s,1:(t-1)])\n    }\n  }\n  \n  for (s in 1:nsp){\n    for (t in 1:(nyears)){\n      estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])\n      tau.obs[s,t] <- pow(sigma.obs[s,t], -2)\n    }\n  }\n  \n  M <- m\n  \n  #########################  end likelihood ###########################\n  \n\n    ######################### Smoothing #######################\n\n    beta[1] ~ dnorm(0, 0.000001)\n    beta[2] ~ dnorm(0, 0.000001)\n    taub ~ dgamma(0.000001, 0.000001)\n    for(k in 1:num.knots){b[k]~dnorm(0,taub)}\n  \n    for (t in 1:(nyears - 1)){\n      logLambda[t] <- m[t]\n      m[t] <- mfe[t] + mre[t]\n      mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]\n      for (k in 1:num.knots){\n        temp[t,k] <- b[k] * Z[t,k]\n      }\n      mre[t]<-sum(temp[t,1:num.knots])\n    }  \n  \n}"
get_bmaBUGScode(option="smooth_det2", print.screen=TRUE, save.local=FALSE)
#> model {
#>     ###################  Define priors  ###########################
#> 
#>   # process errors
#>   tau.spi <- pow(sigma.spi,-2)
#>   sigma.spi ~ dunif(0.0001,30)
#> 
#>   # observation errors: one value per site-species
#>   for (s in 1:nsp){
#>    for (t in 1:nyears){
#>     sigma.obs[s,t] ~ dunif(0.0001, max_se) # for the missing values
#>   }}
#>     
#> 
#>     ######################### Smoothing #######################
#> 
#>     beta[1] ~ dnorm(0, 0.000001)
#>     beta[2] ~ dnorm(0, 0.000001)
#>     taub ~ dgamma(0.000001, 0.000001)
#>     for(k in 1:num.knots){b[k]~dnorm(0,taub)}
#>   
#>     for (t in 1:(nyears - 1)){
#>       logLambda[t] <- m[t]
#>       m[t] <- mfe[t] + mre[t]
#>       mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]
#>       for (k in 1:num.knots){
#>         temp[t,k] <- b[k] * Z[t,k]
#>       }
#>       mre[t]<-sum(temp[t,1:num.knots])
#>     }  
#>   
#>     ###################  Define likelihood  ###########################
#> 
#>     M[1] <- 0 
#>   
#>     for (t in 2:nyears){
#>       M[t] <- M[t-1] + logLambda[t-1]
#>     }
#> 
#>     for (s in 1:nsp){
#>       for (t in 1:(nyears-1)){
#>         spgrowth[s,t] ~ dnorm(logLambda[t], tau.spi)
#>     }}
#> 
#>     for (s in 1:nsp){
#>       for (t in 1:FY[s]){
#>         spindex[s,t] <- spindex[s,t+1] - spgrowth[s,t]
#>       }
#> 
#>       for (t in (FY[s]+1):(nyears)){
#>         spindex[s,t] <- estimate[s,FY[s]] + sum(spgrowth[s,FY[s]:(t-1)])
#>         estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])
#>         tau.obs[s,t] <- pow(sigma.obs[s,t], -2)
#>       }}
#> 
#>   ####################  geomean of expected values ######################
#>   
#>   for (t in 1:nyears){
#>     Mprime[t] <- sum(spindex[,t])/nsp
#>   }
#> 
#> }
#> [1] "model {\n    ###################  Define priors  ###########################\n\n  # process errors\n  tau.spi <- pow(sigma.spi,-2)\n  sigma.spi ~ dunif(0.0001,30)\n\n  # observation errors: one value per site-species\n  for (s in 1:nsp){\n   for (t in 1:nyears){\n    sigma.obs[s,t] ~ dunif(0.0001, max_se) # for the missing values\n  }}\n    \n\n    ######################### Smoothing #######################\n\n    beta[1] ~ dnorm(0, 0.000001)\n    beta[2] ~ dnorm(0, 0.000001)\n    taub ~ dgamma(0.000001, 0.000001)\n    for(k in 1:num.knots){b[k]~dnorm(0,taub)}\n  \n    for (t in 1:(nyears - 1)){\n      logLambda[t] <- m[t]\n      m[t] <- mfe[t] + mre[t]\n      mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]\n      for (k in 1:num.knots){\n        temp[t,k] <- b[k] * Z[t,k]\n      }\n      mre[t]<-sum(temp[t,1:num.knots])\n    }  \n  \n    ###################  Define likelihood  ###########################\n\n    M[1] <- 0 \n  \n    for (t in 2:nyears){\n      M[t] <- M[t-1] + logLambda[t-1]\n    }\n\n    for (s in 1:nsp){\n      for (t in 1:(nyears-1)){\n        spgrowth[s,t] ~ dnorm(logLambda[t], tau.spi)\n    }}\n\n    for (s in 1:nsp){\n      for (t in 1:FY[s]){\n        spindex[s,t] <- spindex[s,t+1] - spgrowth[s,t]\n      }\n\n      for (t in (FY[s]+1):(nyears)){\n        spindex[s,t] <- estimate[s,FY[s]] + sum(spgrowth[s,FY[s]:(t-1)])\n        estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])\n        tau.obs[s,t] <- pow(sigma.obs[s,t], -2)\n      }}\n\n  ####################  geomean of expected values ######################\n  \n  for (t in 1:nyears){\n    Mprime[t] <- sum(spindex[,t])/nsp\n  }\n\n}"
get_bmaBUGScode(option="smooth_det_sigtheta", print.screen=TRUE, save.local=FALSE)
#> model {
#>     ###################  Define priors  ###########################
#> 
#>   # process errors
#>   tau.spi <- pow(sigma.spi,-2)
#>   sigma.spi ~ dunif(0.0001,30)
#> 
#>   # observation error is constant
#>   theta ~ dunif(0.0001,10)
#> 
#>     ######################### Smoothing #######################
#> 
#>     beta[1] ~ dnorm(0, 0.000001)
#>     beta[2] ~ dnorm(0, 0.000001)
#>     taub ~ dgamma(0.000001, 0.000001)
#>     for(k in 1:num.knots){b[k]~dnorm(0,taub)}
#>   
#>     for (t in 1:(nyears - 1)){
#>       logLambda[t] <- m[t]
#>       m[t] <- mfe[t] + mre[t]
#>       mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]
#>       for (k in 1:num.knots){
#>         temp[t,k] <- b[k] * Z[t,k]
#>       }
#>       mre[t]<-sum(temp[t,1:num.knots])
#>     }  
#>   
#>     ###################  Define likelihood  ###########################
#> 
#>     M[1] <- 0 
#>   
#>     for (t in 2:nyears){
#>       M[t] <- M[t-1] + logLambda[t-1]
#>     }
#> 
#>     for (s in 1:nsp){
#>       for (t in 1:(nyears-1)){
#>         spgrowth[s,t] ~ dnorm(logLambda[t], tau.spi)
#>     }}
#> 
#>     for (s in 1:nsp){
#>       for (t in 1:FY[s]){
#>         spindex[s,t] <- spindex[s,t+1] - spgrowth[s,t]
#>       }
#> 
#>       for (t in (FY[s]+1):(nyears)){
#>         spindex[s,t] <- estimate[s,FY[s]] + sum(spgrowth[s,FY[s]:(t-1)])
#>         estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])
#>         tau.obs[s,t] <- pow(theta, -2)
#>       }}
#> 
#>   ####################  geomean of expected values ######################
#>   
#>   for (t in 1:nyears){
#>     Mprime[t] <- sum(spindex[,t])/nsp
#>   }
#> 
#> }
#> [1] "model {\n    ###################  Define priors  ###########################\n\n  # process errors\n  tau.spi <- pow(sigma.spi,-2)\n  sigma.spi ~ dunif(0.0001,30)\n\n  # observation error is constant\n  theta ~ dunif(0.0001,10)\n\n    ######################### Smoothing #######################\n\n    beta[1] ~ dnorm(0, 0.000001)\n    beta[2] ~ dnorm(0, 0.000001)\n    taub ~ dgamma(0.000001, 0.000001)\n    for(k in 1:num.knots){b[k]~dnorm(0,taub)}\n  \n    for (t in 1:(nyears - 1)){\n      logLambda[t] <- m[t]\n      m[t] <- mfe[t] + mre[t]\n      mfe[t] <- beta[1] * X[t,1] + beta[2] * X[t,2]\n      for (k in 1:num.knots){\n        temp[t,k] <- b[k] * Z[t,k]\n      }\n      mre[t]<-sum(temp[t,1:num.knots])\n    }  \n  \n    ###################  Define likelihood  ###########################\n\n    M[1] <- 0 \n  \n    for (t in 2:nyears){\n      M[t] <- M[t-1] + logLambda[t-1]\n    }\n\n    for (s in 1:nsp){\n      for (t in 1:(nyears-1)){\n        spgrowth[s,t] ~ dnorm(logLambda[t], tau.spi)\n    }}\n\n    for (s in 1:nsp){\n      for (t in 1:FY[s]){\n        spindex[s,t] <- spindex[s,t+1] - spgrowth[s,t]\n      }\n\n      for (t in (FY[s]+1):(nyears)){\n        spindex[s,t] <- estimate[s,FY[s]] + sum(spgrowth[s,FY[s]:(t-1)])\n        estimate[s,t] ~ dnorm(spindex[s,t], tau.obs[s,t])\n        tau.obs[s,t] <- pow(theta, -2)\n      }}\n\n  ####################  geomean of expected values ######################\n  \n  for (t in 1:nyears){\n    Mprime[t] <- sum(spindex[,t])/nsp\n  }\n\n}"