Skip to contents

Add a Screener to a Learner

Usage

add_screener(learner, screener, screener_extra_args = NULL)

Arguments

learner

A learner to be modified by wrapping a screening stage on top of it.

screener

A screener to be added on top of the learner

screener_extra_args

Extra arguments to be passed to the screener

Value

A modified learner that when called on data and a formula now runs a screening stage before fitting the learner and returning a prediction function.

Examples


# construct a learner where variables with less than .6 correlation are screened out
lnr_glm_with_cor_60_thresholding <-
  add_screener(
    learner = lnr_glm,
    screener = screener_cor,
    screener_extra_args = list(threshold = 0.6)
  )

# train that on the mtcars dataset — also checking that extra arguments are
# properly passed to glm
lnr_glm_with_cor_60_thresholding(mtcars, formula = mpg ~ ., family = "gaussian")(mtcars)
#>           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
#>            22.57168            21.62228            25.84130            21.24504 
#>   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
#>            17.76571            19.69987            15.67814            23.66263 
#>            Merc 230            Merc 280           Merc 280C          Merc 450SE 
#>            23.19173            19.83174            19.83174            14.15198 
#>          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
#>            15.41784            15.23168            11.50016            10.53559 
#>   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#>            10.45993            26.80036            30.16198            28.23324 
#>       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
#>            25.20260            17.12387            17.67075            15.08978 
#>    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
#>            16.69088            27.79078            26.81546            28.12294 
#>      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
#>            17.65673            19.98352            13.13403            24.18403 

# if we've screened out variables with low correlation to mpg, one such variable is qsec,
# so changing qsec shouldn't modify the predictions from our learned algorithm
mtcars_but_qsec_is_changed <- mtcars
mtcars_but_qsec_is_changed$qsec <- rnorm(n = nrow(mtcars))

identical(
  lnr_glm_with_cor_60_thresholding(mtcars, formula = mpg ~ .)(mtcars),
  lnr_glm_with_cor_60_thresholding(mtcars, formula = mpg ~ .)(mtcars_but_qsec_is_changed)
)
#> [1] TRUE

# earth version
lnr_earth_with_cor_60_thresholding <-
  add_screener(
    learner = lnr_earth,
    screener = screener_cor,
    screener_extra_args = list(threshold = 0.6)
  )
lnr_earth_with_cor_60_thresholding(mtcars, formula = mpg ~ .)(mtcars)
#>  [1] 20.26772 20.26772 25.89116 17.76432 15.15874 18.60730 15.15874 20.60747
#>  [9] 20.75819 20.07358 20.07358 17.30962 17.30962 17.30962 12.29771 12.60425
#> [17] 13.11515 30.47640 30.94588 31.66575 23.99759 16.23163 16.58926 15.41419
#> [25] 14.13694 30.42945 23.96629 27.90992 15.38865 20.65090 16.66589 23.85675

identical(
  lnr_earth_with_cor_60_thresholding(mtcars, formula = mpg ~ .)(mtcars),
  lnr_earth_with_cor_60_thresholding(mtcars, formula = mpg ~ .)(mtcars)
)
#> [1] TRUE

# note that this 'test' does not pass for a learner like randomForest that has
# some randomness in its predictions.