Description Usage Arguments Details Value Examples
evaluate the marginal effects of the selected raw variable on the dependent.
1 2 3 4 |
model |
an output of lm or glm |
data |
NULL (default) or a data.frame, a new dataset to evaluate the categorical variables. If NULL, then use the data used in model itself. |
focus_var_raw |
NULL or a character vector with maximum length of 2, in which you can choose
See the example code for details. |
focus_var_coeff |
NULL or a character vector. Must be See the example code for details. |
focus_var_model |
NULL or a character vector. Must be model vars containing See the example code for details. |
focus_value |
NULL or a list; each element of the list must have names in focus_var_raw.
By default, we will check marginal effects of |
nonfocus_value |
NULL or a list; each element of the list must have names in non-focused raw vars (not show up in |
transform_y |
NULL or a function, used only for plot. Used as a function to recalculate y (a function on y (ex. log(y) )). |
PRINT |
a boolean, whether to print messages AND to plot. |
PLOT |
a bookean, whether to plot |
Reverse |
a boolean, whether to use reverse order in x-axis when plot. Default is FALSE. |
bar_plot |
NULL or a boolean, choose bar plot or line plot. If NULL, we will choose automatically. |
intolerance_on_wrong_names |
a boolean. If a name is wrong, either in focus_var_raw, focus_var_model, focus_var_coeff, focus_value or nonfocus_value, whether we delete the wrong names and go on (default), or report an error. |
This function will evaluate marginal impacts and show the monotonicity of marginal impacts of a selected variable on the dependent.
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.
a list:
Focus_values: show the values of focus_var_raw we used to evaluate the marginal effects.
data_and_predict: full dataset used to evaluate the marginal effects.
summmary_glm: a summary of lm or glm model.
Monoton_Increase: whether the marginal impact is Monotonic Increase.
Monoton_Decrease: whether the marginal impact is Monotonic Decrease.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | ##___ 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)))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.