focusing on selected variables in the model, and eliminating impacts from other variables.

Share:

Description

focusing on selected variables in the model, and eliminating impacts from other variables.

Usage

1
2
focusing_var_coeff(model, focus_var_coeff = NULL, focus_var_raw = NULL,
  intercept_include = TRUE, data = NULL)

Arguments

model

an output of lm or glm

focus_var_coeff

NULL or a character vector, choose coeff vars you want to focus. The unselected vars will have coeff values as 0. Default is NULL, which means to choosing nothing.

focus_var_raw

NULL or a character vector, choose raw vars you want to focus. The unselected vars will have coeff values as 0. Default is NULL, which means to choosing nothing.

intercept_include

a boolean, whether to include the intercept (default is TRUE).

data

optional, a new dataset to evaluate the categorical variables. If NULL, then use the data used in model itself.

Details

In a model y ~ a + b. Sometimes you want to fix value of a and see the variations of b in y. The most straightforward way to code this, as we did in this function, is to make a's coefficients as 0, and then use the predict().

Value

a new model with only focused vars having coeff unchanged, and all other vars having coeff as 0.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
focus_var_raw  = 'carat'

model = lm(price~ cut + carat + I(carat^2) + I(carat^3) +
             I(carat  * depth) + depth,ggplot2::diamonds)
# all coeffs except carat's will be 0
focusing_var_coeff(model, focus_var_coeff = 'carat')
# all coeffs except cut.L's will be 0
focusing_var_coeff(model, focus_var_coeff = 'cut.L')
# all coeffs without raw vars cut or carat will be 0
focusing_var_coeff(model, focus_var_raw = c('cut','carat'))

# if you didn't specify anything, then all vars' coeff will become 0 except intercept
focusing_var_coeff(model)


# if cannot find the focus_var_coeff or focus_var_raw in the model
tryCatch(focusing_var_coeff(model, focus_var_coeff = 'caratdsd'),
         error = function(err) warning(err))
tryCatch(focusing_var_coeff(model, focus_var_raw = '3213'),
         error = function(err) warning(err))

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.