get_x: get x (left hand of var) from model or formula

Description Usage Arguments Details Value Examples

Description

get x (left hand of var) from model or formula

Usage

1
get_x(model, method = c("raw", "model", "coeff"), data = NULL)

Arguments

model

a formula or a model.

method

either 'raw','model', or 'coeff', to decide what kind variables to show. Default is 'raw'. See section Detials below.

data

a dataframe, to provide new data to evaluate the model. If NULL (default), then we use the default data in the model.

Details

What do 'raw' variable, 'model' variable, and 'coeff' variable mean?

Example:

In the model, log(price) ~ cut + I(carat^2) in diamonds data, we have:

See the sample code below for more examples.

Value

x variables in the formula or model

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
# use the sample code from get_x_hidden
#
data = ggplot2::diamonds
diamond_lm  =  lm(price~  I(carat^   2) + cut  + carat*table ,ggplot2::diamonds)

#_________ input as model
get_x(model = diamond_lm,method = 'raw')
get_x(diamond_lm,method = 'model')
get_x(diamond_lm,method = 'coeff')

#_______ input as formula
get_x(formula(diamond_lm),method = 'model')
# data is required when input is formula
get_x(formula(diamond_lm), data = ggplot2::diamonds, method = 'coeff')

tryCatch(
  get_x(formula(diamond_lm),method = 'coeff'),
  error =function(err){
    print(err)
  }
)



#________ irregular formulas __________

model_dirty = model = lm(price~  I(carat^   2) + cut  - carat:table - cut ,ggplot2::diamonds)

# CORRECT for raw vars
get_x(model_dirty)

# correct for model vars
get_x(price~  I(carat^2) + cut  - carat:table - cut,data = ggplot2::diamonds, method ='model')
get_x(model_dirty,method = 'model')
get_x(model_dirty,data = ggplot2::diamonds, method = 'model')
get_x(model_dirty, method = 'model')

# clean method for model vars
# terms((price~  I(carat^2) + cut  - carat:table - cut)) %>% attr(.,"factors") %>% colnames()
# model_dirty %>% terms %>% attr(.,"factors") %>% colnames()
# formula(model_dirty) %>% terms %>% attr(.,"factors") %>% colnames()

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