Description Usage Arguments Details Value Examples
check monotonicity of marginal impacts and re-estimate the model (optional) until we get correct marginal impacts.
1 2 3 |
model, |
an output of lm or glm |
focus_var_raw |
see |
focus_var_model |
see |
Monoton_to_Match |
1 or -1. 1 means you want monotonic increasing as the correct marginal effect, -1 means negative |
family |
family of glm, for example, can be gaussian |
re_estimate |
a boolean with default as TRUE. This is to decide if the marginal impacts are found to be incorrect, then whether to delete a model var that potentially cause the wrong marginal impacts and re-estimate the model |
data |
optional, a new dataset to show the marginal impacts and re-estimate the model. If NULL, then use the data used in model itself. |
STOP |
a boolean. When find a model with incorrect marginal impacts, whether to stop there and wait to continue (call the |
PRINT |
a boolean, whether to print messages and to plot. |
PLOT |
a boolean, whether to plot. |
... |
additional arguments going to |
This function first calls function effects
and then checks the monotonicity of marginal impacts. If the direction of marginal impacts are incorrect,
it can delete a model var that potentially causes the wrong marginal impacts and then re-estimate the model.
We will keep doing this until the correct marginal impacts are found
Details of evaluating the marginal impacts effects
a model (lm
or glm
).
If re_estimate == TRUE, then return will be an re-estimated model with correct marginal impacts given we can find one.
If re_estimate == FALSE, original model will be returned.
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 | ##
set.seed(413)
traing_data = ggplot2::diamonds[runif(nrow(ggplot2::diamonds))<0.05,]
nrow(traing_data)
diamond_lm3 = lm(formula = price ~ carat + I(carat^2) + I(carat^3) + cut +
I(carat * depth) , data = traing_data)
test = deleting_wrongeffect(model = diamond_lm3,
focus_var_raw = 'carat',
focus_var_model = c("I(carat^3)","I(carat*depth)",
"I(carat^2)","I(carat)"),
focus_value = list(carat=seq(0.5,6,0.1)),
data = traing_data,
PRINT = TRUE,STOP = FALSE,
Reverse = FALSE)
## two focus on vars
test =
deleting_wrongeffect(model = diamond_lm3 ,
focus_var_raw = c('carat',"cut"),
focus_var_model = c("I(carat*depth)","I(carat^3)"),
focus_value = list(carat=seq(0.5,6,0.1)),
data = traing_data,PRINT = TRUE,STOP =FALSE)
diamond_lm3 = lm(formula = price ~ cut + depth +
I(carat * depth) , data = ggplot2::diamonds)
## negative signs
deleting_wrongeffect(model = diamond_lm3 ,
focus_var_raw = c('depth',"cut"),
focus_var_model = c("depth"),Monoton_to_Match = -1,
data = ggplot2::diamonds,PRINT = TRUE,STOP =FALSE)
## wrong variables names
deleting_wrongeffect(diamond_lm3, focus_var_raw = 'carat',
focus_var_model = c("I(cara79t^3)"),
data = ggplot2::diamonds,PRINT = TRUE)
deleting_wrongeffect(diamond_lm3, focus_var_raw = 'carat890',
focus_var_model = c("I(carat^3)"),
data = ggplot2::diamonds, PRINT = TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.