Doug Fir
Doug Fir

Reputation: 21212

Apply model$finalModel$formula to a new model?

I experimented with a sample of data 3 different models using a small sample of data using caret and stepwise regression. Based on prAUC I can see which model performs best.

I would like to choose features to use on a model with a larger sample based on the features chosen by one of these stepwise models.

I can see the final chosen features using:

> formula(step_both_model$finalModel)
.outcome ~ tenure_months + auto_renewal_flag + v_count_ventures + 
    v_count_hosting_top_ten_competitor + v_count_hosting_long_tail_competitor + 
    v_count_domains + v_count_email + v_count_ssl + v_count_no_hosting_detected + 
    v_change_external_mail_petal_count + product_pnl_line_nameCnP.Hosting + 
    product_pnl_line_nameGrid + product_pnl_line_namePaid.Support + 
    product_pnl_line_nameShared.Hosting + product_pnl_line_nameWordpress + 
    shopper_region_1_nameAPAC + shopper_region_1_nameCanada + 
    shopper_region_1_nameEMEA + shopper_region_1_nameLatAm + 
    shopper_region_1_nameOthers + usa_tenure
<environment: 0xb77b818>

My question is, rather than manually cutting and pasting this list of features, is there a way to extract the predictor feature names of an r model to use in another model?

Tried:

model$finalModel$terms
.outcome ~ tenure_months + auto_renewal_flag + v_count_ventures + 
    v_count_hosting_top_ten_competitor + v_count_hosting_long_tail_competitor + 
    v_count_domains + v_count_email + v_count_ssl + v_count_no_hosting_detected + 
    v_change_external_mail_petal_count + product_pnl_line_nameCnP.Hosting + 
    product_pnl_line_nameGrid + product_pnl_line_namePaid.Support + 
    product_pnl_line_nameShared.Hosting + product_pnl_line_nameWordpress + 
    shopper_region_1_nameAPAC + shopper_region_1_nameCanada + 
    shopper_region_1_nameEMEA + shopper_region_1_nameLatAm + 
    shopper_region_1_nameOthers + usa_tenure
attr(,"variables")
list(.outcome, tenure_months, auto_renewal_flag, v_count_ventures, 
    v_count_hosting_top_ten_competitor, v_count_hosting_long_tail_competitor, 
    v_count_domains, v_count_email, v_count_ssl, v_count_no_hosting_detected, 
    v_change_external_mail_petal_count, product_pnl_line_nameCnP.Hosting, 
    product_pnl_line_nameGrid, product_pnl_line_namePaid.Support, 
    product_pnl_line_nameShared.Hosting, product_pnl_line_nameWordpress, 
    shopper_region_1_nameAPAC, shopper_region_1_nameCanada, shopper_region_1_nameEMEA, 
    shopper_region_1_nameLatAm, shopper_region_1_nameOthers, 
    usa_tenure)
attr(,"factors")
                                     tenure_months auto_renewal_flag v_count_ventures
.outcome                                         0                 0                0
tenure_months                                    1                 0                0
auto_renewal_flag                                0                 1                0
v_count_ventures                                 0                 0                1
v_count_hosting_top_ten_competitor               0                 0                0
v_count_hosting_long_tail_competitor             0                 0                0
v_count_domains                                  0                 0                0
v_count_email                                    0                 0                0
v_count_ssl                                      0                 0                0
v_count_no_hosting_detected                      0                 0                0
v_change_external_mail_petal_count               0                 0                0
product_pnl_line_nameCnP.Hosting                 0                 0                0
product_pnl_line_nameGrid                        0                 0                0
product_pnl_line_namePaid.Support                0                 0                0
product_pnl_line_nameShared.Hosting              0                 0                0
product_pnl_line_nameWordpress                   0                 0                0
shopper_region_1_nameAPAC                        0                 0                0
shopper_region_1_nameCanada                      0                 0                0
shopper_region_1_nameEMEA                        0                 0                0
shopper_region_1_nameLatAm                       0                 0                0
shopper_region_1_nameOthers                      0                 0                0
usa_tenure                                       0                 0                0
                                     v_count_hosting_top_ten_competitor v_count_hosting_long_tail_competitor
.outcome                                                              0                                    0
tenure_months                                                         0                                    0
auto_renewal_flag                                                     0                                    0
v_count_ventures                                                      0                                    0
v_count_hosting_top_ten_competitor                                    1                                    0
v_count_hosting_long_tail_competitor                                  0                                    1
v_count_domains                                                       0                                    0
v_count_email                                                         0                                    0
v_count_ssl                                                           0                                    0
v_count_no_hosting_detected                                           0                                    0
v_change_external_mail_petal_count                                    0                                    0
product_pnl_line_nameCnP.Hosting                                      0                                    0
product_pnl_line_nameGrid                                             0                                    0
product_pnl_line_namePaid.Support                                     0                                    0
product_pnl_line_nameShared.Hosting                                   0                                    0
product_pnl_line_nameWordpress                                        0                                    0
shopper_region_1_nameAPAC                                             0                                    0
shopper_region_1_nameCanada                                           0                                    0
shopper_region_1_nameEMEA                                             0                                    0
shopper_region_1_nameLatAm                                            0                                    0
shopper_region_1_nameOthers                                           0                                    0
usa_tenure                                                            0                                    0

Which gives a lot of information but I cannot see how I can extract just the feature names that were used as predictors so that I can use those on a new model (with a larger sample of data)

How can I extract model feature names in order to e.g. filter a data frame based on feature names to then pass into train()

Upvotes: 1

Views: 45

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

Perhaps using

update(formula(model$finalModel), newVariable ~ .)
# newVariable ~ crim + zn + chas1 + nox + rm + dis + rad + tax + 
#     ptratio + b + lstat + `rm:lstat`
# <environment: 0x119e6c6a8>

gives what you want even faster. To get the right hand side you may use

formula(model$finalModel)[[3]]
# crim + zn + chas1 + nox + rm + dis + rad + tax + ptratio + b + 
#     lstat + `rm:lstat`

while to extract predictors as a character vector you may do

attr(terms(formula(model$finalModel)), "term.labels")
#  [1] "crim"       "zn"         "chas1"      "nox"        "rm"         "dis"       
#  [7] "rad"        "tax"        "ptratio"    "b"          "lstat"      "`rm:lstat`"

Upvotes: 1

Related Questions