R/densityRegGaussian.R

#' Regression with Gaussian link density (univariate, continuous, unbounded space)
#'
#' @inherit Density
#' @param sigma Either a fixed value or a prior density for the shape parameter.
#' @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
#' RegGaussian(
#'   sigma = Cauchy(mu = 0, sigma = 10, bounds = list(0, NULL)),
#'   xBeta = Gaussian(0, 10),
#'   M     = 3
#' )
RegGaussian <- function(sigma = NULL, xBeta = NULL, M = NULL, ordered = NULL, equal = NULL, bounds = list(NULL, NULL),
                     trunc  = list(NULL, NULL), k = NULL, r = NULL, param = NULL) {
  Density("RegGaussian", ordered, equal, bounds, trunc, k, r, param, sigma = sigma, xBeta = xBeta, M = M)
}

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

#' @keywords internal
#' @inherit freeParameters
freeParameters.RegGaussian <- 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 {
      ""
    }

  sigmaStr <-
    if (is.Density(x$sigma)) {
      sigmaBoundsStr <- make_bounds(x, "sigma")
      sprintf(
        "real%s sigma%s%s;",
        sigmaBoundsStr, get_k(x, "sigma"), get_r(x, "sigma")
      )
    } else {
      ""
    }

  collapse(xBetaStr, sigmaStr)
}

#' @keywords internal
#' @inherit fixedParameters
fixedParameters.RegGaussian <- 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
      )
    }

  sigmaStr <-
    if (is.Density(x$sigma)) {
      ""
    } else {
      if (!check_scalar(x$sigma)) {
        stop("If fixed, sigma must be a scalar.")
      }

      sprintf(
        "real sigma%s%s = %s;",
        get_k(x, "sigma"), get_r(x, "sigma"), x$sigma
      )
    }

  collapse(xBetaStr, sigmaStr)
}

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

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

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

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