Skip to contents

This function accepting an mean_lnr, which it then trains on the data and formula given. Then stats::density is fit to the error (difference between observed outcome and the mean_lnr predictions).

Usage

lnr_homoskedastic_density(
  data,
  formula,
  mean_lnr,
  mean_lnr_args = NULL,
  density_args = NULL,
  weights = NULL
)

Arguments

data

A dataframe to train a learner / learners on.

formula

A regression formula to use inside this learner.

mean_lnr

A learner (function) passed in to be trained on the data with the given formula and then used to predict conditional means for provided newdata.

mean_lnr_args

Extra arguments to be passed to the mean_lnr

density_args

Extra arguments to be passed to the kernel density smoother stats::density, especially things like bw for specifying the smoothing bandwidth. See ?stats::density.

weights

Observation weights; see ?lm

Value

A predictor function that takes in newdata and produces density estimates

Details

This returns a function that takes in newdata and produces density estimates according to the estimated stats::density fit the error from the newdata observed outcome and the prediction from the mean_lnr.

That is to say, this follows the following procedure (assuming \(Y\) as the outcome and \(X\) as a matrix of predictors):

$$\texttt{obtain } \hat{\mathbb E}(Y | X) \quad \mathtt{using \quad mean\_learner}$$ $$\texttt{fit } \hat{f} \gets \mathtt{density}(Y - \hat{\mathbb E}(Y | X))$$ $$\mathtt{return \quad function(newdata) \{ } \hat{f}(\mathtt{newdata\$Y} - \hat{\mathbb E}[Y | \mathtt{newdata\$X}]) \} $$

Examples

# fit a conditional density model with mean model as a randomForest
fit_density_lnr <- lnr_homoskedastic_density(
  data = mtcars,
  formula = mpg ~ hp,
  mean_lnr = lnr_rf
)

# and what we should get back should be predicted densities at the
# observed mpg given the covariates hp
fit_density_lnr(mtcars)
#>  [1] 0.26540683 0.26540683 0.21309267 0.27370671 0.27185953 0.10959307
#>  [7] 0.27479757 0.01842828 0.27489520 0.27065926 0.17125989 0.26420773
#> [13] 0.23557609 0.17649752 0.16044735 0.17892349 0.18548632 0.04724422
#> [19] 0.14346378 0.04590438 0.25460112 0.17772784 0.15573194 0.21281009
#> [25] 0.26138954 0.07606822 0.23447868 0.04710417 0.16198474 0.19921917
#> [31] 0.26883583 0.27276693