Skip to contents

This is the simplest possible density estimator that is entertainable. It fits a lm model to the data, and uses the variance of the residuals to parameterize a model of the data as \(\mathcal N(y | \beta x, \sigma^2)\).

Usage

lnr_lm_density(data, formula, weights = NULL, ...)

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

lnr_lm_density(mtcars, hp ~ mpg)(mtcars)
#>  [1] 7.407872e-03 7.407872e-03 7.280857e-03 7.794082e-03 8.614861e-03
#>  [6] 3.606026e-03 5.086892e-03 5.157082e-03 7.508464e-03 7.070516e-03
#> [11] 5.508851e-03 9.226929e-03 9.044411e-03 8.990802e-03 7.565190e-03
#> [16] 8.521810e-03 6.560129e-03 7.482015e-03 9.195241e-03 5.983066e-03
#> [21] 6.367316e-03 6.370058e-03 6.031372e-03 6.225963e-03 8.251524e-03
#> [26] 8.539216e-03 9.197872e-03 3.828936e-03 1.706495e-03 7.821512e-03
#> [31] 3.776055e-05 7.687939e-03
hp_seq <- seq(min(mtcars$hp), max(mtcars$hp), length.out = 1000)
plot(
  x = hp_seq,
  y = lnr_lm_density(mtcars, hp ~ mpg)(
    data.frame(hp = hp_seq, mpg = rep(mean(mtcars$mpg), 1000))),
  xlab = "hp",
  ylab = "density",
  main = "normal density model of horsepower given mean(mpg)"
)