
Last chance! 50% off unlimited learning
Sale ends in
effect(model, data = NULL, focus_var_raw, focus_var_coeff = NULL, focus_var_model = NULL, focus_value = NULL, nonfocus_value = NULL, transform_y = NULL, PRINT = TRUE, PLOT = TRUE, Reverse = FALSE, bar_plot = NULL, intolerance_on_wrong_names = FALSE)
raw vars
you want to focus.
See get_x
for the meaning of raw var
.focus_var_raw
, then we will check the marginal impact of that raw var.
focus_var_raw
, then we
will check the marginal impact of the FIRST raw var (focus_var_raw[1]
) under different values of SECOND raw var (focus_var_raw[2]
).
See the example code for details.
coeff vars
containing focus_var_raw[1]
.
See get_x
for the meaning of coeff var
.
After you set up the focus_var_raw
, you can also choose to focus on effects of focus_var_raw[1]
through only certain coeff vars,
then all other unspecified coeff vars related focus_var_raw[1]
will have coeff 0
by default, focus_var_coeff is null, which means we will check effect of focus_var_raw[1]
on all coeff vars.See the example code for details.
focus_var_raw[1]
.
See get_x
for the meaning of model var
.
Similar use as argument focus_var_coeff
, except here you can specify which model vars you want to focus.See the example code for details.
focus_var_raw[1]
through seq(0.05,0.95,by = 0.05)
quantiles of its values in the modelling data.
But you can also specify the values you want to check here. See the sample code.focus_var_raw
)
The meaning of non-focus var is: When we check the marginal effect of focus var on dependent, we let the focus var vary and fix the non-focus vars.
By default, for non-focused raw vars, we assume their values are fixed at mean (if numeric) or mode (if factor or character) in the modelling data.
But you can also specify the fixed values you want. See the sample code.Note that the marginal impacts is not simply the sign of coeff: In a model like y~ x + x^2 + p + q
,
marginal impacts of x
on y
requires an evaluation of both x
and x^2
at the same time.
Here the focus_var_raw
is x
, focus_var_coeff
are x
and x^2
nonfocus_value
is p
and q
Also the monotonicity of marginal impacts of x
will be different for different range of x
's values.
Another interesting case is when x
is interacting with other variables, then its marginal impacts will also
be dependent on the values of those interacted variables.
Level of marginal impacts: To make the level of marginal impacts of x
realistic, by default we fixed all other right-hand-side variables
fixed at their mean (numeric) or mode (character or factor). You can also provide fixed values for them.
Also by default we let the interested variable (focused raw var) x
to vary between its seq(0.05,0.95,by = 0.05)
quantiles.
This function will take care those cases above and make evaluating marginal impacts easier.
##___ unit test ____
# __________________ One Dimension: the most basic case ____________________
set.seed(413)
traing_data = ggplot2::diamonds[runif(nrow(ggplot2::diamonds))<0.05,]
nrow(traing_data)
diamond_lm3 = lm(price~ cut + carat + I(carat^2) +
I(carat^3) + I(carat * depth) + cut:depth, traing_data) # a GLM
# more carats, higher price.
effect(model = diamond_lm3,
data = traing_data,
focus_var_raw = c('carat'),
Reverse = TRUE) # value in x-axis is reverse
# focus on only 'I(carat^3)', which means we will make all other coeff,
# including 'carat' and 'I(carat^2)' into 0
effect(model = diamond_lm3,
data =traing_data,
focus_var_raw =c('carat'),
focus_var_coeff = 'I(carat^3)')
# __________________ One Dimension: Categorical ____________________
# selected model-var to focus: here not focus on cut:depth, only focus on cut
suppressWarnings(
effect(model = diamond_lm3,
data = traing_data,
focus_var_raw = c('cut'),
focus_var_model = 'cut'
)
)
# __________________ Double Dimensions ____________________
# here focus_var_raw has two values: "carat" and "cut"
# that means we will evaluate impact of "carat" on "price" through different value of "cut"
effect(model = diamond_lm3,data = traing_data, focus_var_raw=c('carat',"cut"))
# __________________ Provide Values to Focused vars ____________________
# when evaluating impacts,
# we can provide the range of values for key variables
effect(model = diamond_lm3,data = traing_data,
focus_var_raw = c('carat',"cut"),
focus_value = list(carat=seq(0.5,6,0.1)))
Run the code above in your browser using DataLab