R/densityRegBernoulliLogit.R

#' Bernoulli regression with logistic link density (univariate, discrete, binary space)
#'
#' @inherit Density
#' @param xBeta Either a fixed value or a prior density for the parameter of the regression.
#' @param M     An integer with the number of covariates in the observation regression model.
#' @family Density
#' @examples
#' RegBernoulliLogit(
#'   xBeta = Gaussian(0, 10),
#'   M     = 3
#' )
RegBernoulliLogit <- function(xBeta = NULL, M = NULL, ordered = NULL, equal = NULL, bounds = list(NULL, NULL),
                        trunc  = list(NULL, NULL), k = NULL, r = NULL, param = NULL) {
  DiscreteDensity("RegBernoulliLogit", ordered, equal, bounds, trunc, k, r, param, xBeta = xBeta, M = M)
}

#' @keywords internal
#' @inherit block_data
block_data.RegBernoulliLogit <- function(x, noLogLike) {
  c(
    "int<lower = 1> M; // number of predictors",
    "matrix[T, M] x;   // predictors",
    if (!noLogLike) { NextMethod()}
  )
}

#' @keywords internal
#' @inherit freeParameters
freeParameters.RegBernoulliLogit <- function(x) {
  xBetaStr <-
    if (is.Density(x$xBeta)) {
      xBetaBoundsStr <- make_bounds(x, "xBeta")
      sprintf(
        "vector%s[M] xBeta%s%s;",
        xBetaBoundsStr, get_k(x, "xBeta"), get_r(x, "xBeta")
      )
    } else {
      ""
    }

  xBetaStr
}

#' @keywords internal
#' @inherit fixedParameters
fixedParameters.RegBernoulliLogit <- function(x) {
  xBetaStr <-
    if (is.Density(x$xBeta)) {
      ""
    } else {
      if (!check_vector(x$xBeta)) {
        stop("If fixed, xBeta must be a vector.")
      }

      sprintf(
        "vector[M] xBeta%s%s = %s;",
        get_k(x, "xBeta"), get_r(x, "xBeta"), x$xBeta
      )
    }

  xBetaStr
}

#' @keywords internal
#' @inherit generated
generated.RegBernoulliLogit <- function(x) {
  sprintf(
    "if(zpred[t] == %s) ypred[t][%s] = bernoulli_logit_rng(x[t] * xBeta%s%s);",
    x$k, x$r,
    get_k(x, "xBeta"), get_r(x, "xBeta")
  )
}

#' @keywords internal
#' @inherit getParameterNames
getParameterNames.RegBernoulliLogit <- function(x) {
  return(c("xBeta"))
}

#' @keywords internal
#' @inherit logLike
logLike.RegBernoulliLogit <- function(x) {
  sprintf(
    "loglike[%s][t] = bernoulli_logit_lpmf(y[t] | x[t] * xBeta%s%s);",
    x$k,
    get_k(x, "xBeta"), get_r(x, "xBeta")
  )
}

#' @keywords internal
#' @inherit prior
prior.RegBernoulliLogit <- function(x) {
  stop("Not to be used as a prior :)")
}
luisdamiano/BayesHMM documentation built on May 20, 2019, 2:59 p.m.