Description Usage Arguments Value Author(s) Examples
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.
1 | regfac.merge(fgh1, fgh2, fgh = 2)
|
fgh1 |
First log-density list, containing elements |
fgh2 |
Second log-density list, containing elements |
fgh |
Integer flag with possible values |
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.
Alireza S. Mahani, Mansour T.A. Sharabiani
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)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.