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(6020)
n <- 500
x <- sort(runif(n, -3, 3))
par <- data.frame(mu = sin(x), sigma = exp(-0.5 + cos(x)))
y <- fam$random(par, n)
head(y)
df <- data.frame(y = y, x = x)
## plot count data
plot(y ~ x, data = df)
## fitted pdf
fit_family(df$y, family = fam)
## fit a regression model using the discretized family
m <- gamlss2(y ~ s(x) | ., data = df, family = fam)
## inspect the estimated effect and fitted probabilities
plot(m)
d <- predict(m)
head(pdf(d, y))
## predict median
p <- quantile(d, probs = 0.5)
plot(y ~ x, data = df)
lines(p ~ x, lwd = 2, col = 4)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. |
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 \(y_j\) has probability
\(P(Y \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