
Conditional Normal Density Estimation Given Mean Predictors — with GLMs
Source:R/density_learners.R
lnr_glm_density.RdThis is a step up from the lnr_lm_density in that it uses
a glm for the conditional mean model.
Note that this allows for specification of glm features
like family = ... in the ,.. arguments, and
that's the main advantage over the lnr_lm_density.
Also note that this still differs from using lnr_homoskedastic_density
with mean_lnr = lnr_glm because lnr_homoscedastic_density
uses stats::density to do kernel bandwidth smoothing
on the error distribution of the mean predictions..
Arguments
- data
A dataframe to train a learner / learners on.
- formula
A regression formula to use inside this learner.
- weights
Observation weights; see
?lm- ...
Any extra arguments that should be passed to the internal model for model fitting purposes.
Value
a closure (function) that produces density estimates
at the newdata given according to the fit model.
Examples
# for example, we could use a Poisson assumption with identity link:
lnr_glm_density(mtcars, hp ~ mpg, family = poisson(link = "identity"))(mtcars)
#> [1] 4.548971e-18 4.548971e-18 5.393408e-22 6.643636e-15 1.668889e-07
#> [6] 3.666970e-61 1.742952e-56 9.756233e-52 1.286568e-19 9.165389e-19
#> [11] 9.427301e-33 3.403811e-02 2.097218e-04 6.484083e-02 7.697425e-06
#> [16] 3.946421e-02 4.893057e-35 1.528293e-04 8.819586e-07 2.666786e-11
#> [21] 8.767254e-30 6.466000e-20 1.196739e-22 2.618111e-42 2.957429e-10
#> [26] 2.937978e-14 7.057990e-04 5.397539e-38 1.343741e-134 1.549503e-13
#> [31] 0.000000e+00 6.527346e-16
hp_seq <- seq(min(mtcars$hp), max(mtcars$hp), length.out = 1000)
plot(
x = hp_seq,
y = lnr_glm_density(mtcars, hp ~ mpg, family = poisson(link = "identity"))(
data.frame(hp = hp_seq, mpg = rep(mean(mtcars$mpg), 1000))),
xlab = "hp",
ylab = "density",
main = "normal density model of horsepower given mean(mpg)"
)