Learn R Programming

RegressionFactory (version 0.7.4)

regfac.merge: Utility Function for Adding Two Functions and Their Derivatives

Description

Combining two log-density functions by adding the corresponding elements of their lists (function, gradient, Hessian). This can be useful, e.g. in combining the likelihood and the prior (in log domain) to form the posterior according to Bayes rule.

Usage

regfac.merge(fgh1, fgh2, fgh = 2)

Arguments

fgh1

First log-density list, containing elements f, g and h, corresponding to log-density function, its gradient vector, and its Hessian matrix.

fgh2

Second log-density list, containing elements f, g and h, corresponding to log-density function, its gradient vector, and its Hessian matrix.

fgh

Integer flag with possible values 0,1,2, indicating the maximum order of derivative to be returned.

Value

If fgh==0, fgh1+fgh2 is returned. Otherwise, a list is returned with elements f, g, and h, each of which is the sum of corresponding elements of fgh1 and fgh2 lists.

Examples

Run this code
# NOT RUN {
# constructing the log-posterior for Bayesian logistic regression
# log-likelihood
loglike.logistic <- function(beta, X, y, fgh) {
  regfac.expand.1par(beta, X, y, fbase1.binomial.logit, fgh, n=1)
}
# log-prior
logprior.logistic <- function(beta, mu.beta, sd.beta, fgh) {
  f <- sum(dnorm(beta, mu.beta, sd.beta, log=TRUE))
  if (fgh==0) return (f)
  g <- -(beta-mu.beta)/sd.beta^2
  if (fgh==1) return (list(f=f, g=g))
  #h <- diag(rep(-1/sd.beta^2,length(beta)))
  h <- diag(-1/sd.beta^2)
  return (list(f=f, g=g, h=h))
}
# adding log-likelihood and log-prior according to Bayes rule
logpost.logistic <- function(beta, X, y, mu.beta, sd.beta, fgh) {
  ret.loglike <- loglike.logistic(beta, X, y, fgh)
  ret.logprior <- logprior.logistic(beta, mu.beta, sd.beta, fgh)
  regfac.merge(ret.loglike,ret.logprior, fgh=fgh)
}

# }

Run the code above in your browser using DataLab