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

Description Usage Arguments Value Author(s) Examples

View source: R/utils.R

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

1
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.

Author(s)

Alireza S. Mahani, Mansour T.A. Sharabiani

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 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)
}

RegressionFactory documentation built on Oct. 26, 2020, 9:07 a.m.