deleting_wrongeffect: check monotonicity of marginal impacts and re-estimate the...

Description Usage Arguments Details Value Examples

Description

check monotonicity of marginal impacts and re-estimate the model (optional) until we get correct marginal impacts.

Usage

1
2
3
deleting_wrongeffect(model, focus_var_raw = NULL, focus_var_model = NULL,
  Monoton_to_Match = 1, family = NULL, re_estimate = TRUE, data,
  STOP = FALSE, PRINT = TRUE, PLOT = TRUE, ...)

Arguments

model,

an output of lm or glm

focus_var_raw

see effects.

focus_var_model

see effects.

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 "(link = 'identity')" or "(link = 'logit')". If NULL, we will use the default family of the model

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 Enter_to_Continue)

PRINT

a boolean, whether to print messages and to plot.

PLOT

a boolean, whether to plot.

...

additional arguments going to effect

Details

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

Value

a model (lm or glm).

Examples

 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)

Fan-Yang-Econ/linear.tools_github documentation built on May 6, 2019, 4:34 p.m.