Family objects are important building blocks in the design of BAMLSS models. They specify the distribution by collecting functions of the density, respective log-likelihood, first-order derivatives of the log-likelihood w.r.t. predictors (the score function), and (optionally) second-order derivatives of the log-likelihood w.r.t. predictors or their expectation (the Hessian).
The bamlss package can be easily extended by constructing families for specific tasks, i.e. problems for which a likelihood can be formulated. However, the most important distributions are shipped with bamlss. Finally, it is also possible to employ gamlss families.
We illustrate how to build a bamlss family by hand along the Gaussian distribution, with density and log-likelihood function for an individual observation. The sum of the log-likelihood function over all observations is the target function of the optimization problem.
In the distributional regression framework the parameters are linked to predictors by link functions, For the Gaussian and are linked to and by the identity function and the logarithm, respectively.
The score functions in bamlss are the first derivatives of the log-likelihood w.r.t. the predictors: and
For the second derivative of the log-likelihood we are able to obtain the negative expectation, and
Now we have to write a function that returns a
family.bamlss object (S3) which encapsulates functions for
density, score and Hessian, and the names of the family, parameter and
link functions:
| Name of element | Value |
|---|---|
family |
character string with the name of the family |
names |
vector of character strings with the names of the parameters |
links |
vector of character strings with the names of the link functions |
d |
a function returning the density with arguments
y, par, log = FALSE (see below) |
score |
a list with functions (one for each parameter) returning the first derivatives of the log-likelihood w.r.t. predictors |
hess |
a list with functions (one for each parameter) returning the negative second derivatives of the log-likelihood w.r.t. predictors |
Nearly all functions take as first argument the response
y and as second argument a named list holding the evaluated
parameters par of the distribution on the scale of the
predictors
.
The implementation looks like this:
mygauss <- function(...) {
f <- list(
"family" = "mygauss",
"names" = c("mu", "sigma"),
"links" = c(mu = "identity", sigma = "log"),
"d" = function(y, par, log = FALSE) {
dnorm(y, mean = par$mu, sd = par$sigma, log = log)
},
"score" = list(
mu = function(y, par, ...) {
drop((y - par$mu) / (par$sigma^2))
},
sigma = function(y, par, ...) {
drop(-1 + (y - par$mu)^2 / (par$sigma^2))
}
),
"hess" = list(
mu = function(y, par, ...) {
drop(1 / (par$sigma^2))
},
sigma = function(y, par, ...) {
rep(2, length(y))
}
)
)
class(f) <- "family.bamlss"
return(f)
}
library("bamlss")
## Simulate some data.
d <- GAMart()
## Specify formula.
f <- ~ s(x1) + s(x2) + s(x3) + s(lon, lat)
f <- list(mu = update(f, num ~ .), sigma = f)
## Model.
b <- bamlss(f, data = d, family = mygauss)Optionally, the family.bamlss object can be extended by
functions for
p(y, par, ...),q(p, par),r(n, par),loglik(y, par),mu(par, ...),which can help to speed up optimization, or be convenient for predictions and simulations.
There are some families implemented:
Often called binomial, though the Bernoulli is a special case of the binomial distribution.
Density: where is a probability.
Links:
Function:
binomial_bamlss(link = "logit", ...)
Say, is a categorial response with nomial categories, and are associated dummy variables. are probabilities for an observation falling in one of the categories. Category serves as reference, i.e. .
Density:
Links:
where specifies the log-odds between category and the reference category .
Function: multinomial_bamlss(...)
The bamlss package supports gamlss families via a transfer
function that builds the bamlss.family object from a GAMLSS
distribution funciton (Stasinopoulos and Rigby
2007), e.g. NO(), internally:
## Load the gamlss families.
library("gamlss.dist")
## Estimate model.
b <- bamlss(f, data = d, family = NO)