library("gamlss2")
## fixed intervals and their observed values
breaks <- c(-Inf, -1, 0, 1, Inf)
values <- 0:3
## discretize a normal family
fam <- discretize_family(NO, breaks = breaks, values = values)
## simulate data whose location depends on x
set.seed(111)
n <- 500
x <- sort(runif(n, -3, 3))
par <- data.frame(mu = sin(x), sigma = exp(-0.5))
y <- fam$random(par, n)
d <- data.frame(y = y, x = x)
## fit a regression model using the discretized family
m <- gamlss2(y ~ s(x), data = d, family = fam)
## inspect the estimated effect and fitted probabilities
plot(m)
fitted_par <- predict(m)
head(fam$pdf(fitted_par, y))Discretize a Distribution Family into Fixed Bins
Description
Constructs a discrete distribution family by assigning the probability of each fixed interval of a base distribution to a corresponding response value.
Usage
discretize_family(family = NO, breaks,
values = seq_len(length(breaks) - 1L), right = TRUE)
Arguments
family
|
A distribution family accepted by the gamlss2 family infrastructure, such as NO, or another modified family.
|
breaks
|
A strictly increasing numeric vector of interval boundaries. The first and last elements must be -Inf and Inf.
|
values
|
A strictly increasing vector of finite numeric response values, one for each interval defined by breaks. By default, the intervals are labeled consecutively starting at one.
|
right
|
Logical. Should the intervals be right-closed, \((a, b]\), rather than left-closed, \([a, b)\)? |
Details
For each interval defined by adjacent elements of breaks, discretize_family() assigns the interval probability under the base distribution to the corresponding element of values. Thus, with right = TRUE, response value \(v_j\) has probability
\(P(X \in (b_j, b_{j+1}])\)
under the base distribution. With right = FALSE, the intervals are left-closed instead.
The returned family provides probability mass, cumulative distribution, quantile, random-number, mean, and variance functions. Its distributional parameters and link functions are inherited from the base family.
Value
An object of class “gamlss2.family” representing the discretized distribution.
See Also
family_extensions, round_family, gamlss2, gamlss2.family