factorPull: factorPull

Description Usage Arguments Details Examples

View source: R/factorPull.R

Description

This function takes any estimated regression compatible with broom::tidy() that contains a factor variable, or a factor variable interacted with something else. It will pull out those coefficients and line them up with the original data so you can store them as a variable.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
factorPull(
  model,
  data,
  factor,
  interaction = NULL,
  specify = NULL,
  basevalue = 0,
  addterm = NULL,
  value = "estimate",
  na.predict = TRUE
)

Arguments

model

A model object compatible with broom::tidy().

data

A data set containing the factor variable you want coefficients for.

factor

A string with the name of the factor variable you'd like to get the coefficients of. Note if the variable is included in regression via the factor() function, include that. So if your model is mpg~factor(cyl)+hp, do factor = 'factor(cyl)'.

interaction

A string with the name of a variable being interacted with factor for which you'd like to get the different interaction terms. Specify the name as it shows up in the regression table. So for example if factor = 'X' and your model has coefficients Z1:X1, Z1:X2, Z2:X1, Z2:X2, then interaction = 'Z1' will get the Z1:X1, Z1:X2 coefficients.

specify

For more complex sets of interactions, like three-way interactions, or if your factor variable is included via some function more complex than factor(), you can specify the exact naming structure of the coefficients, with {value} standing in for the factor value. So for example if you had coefficients Z:X1:W, Z:X2:W in your model, you could do specify = 'Z:X{value}:W'. This will override anything in interaction.

basevalue

If specify is used, what value should the factor reference group (or any excluded terms) be given? By default 0. Set to NA to leave omitted terms as NA.

addterm

A string indicating a coefficient in the model to be added to all terms. Commonly this is the coefficient for the variable being interacted with, to give an overall effect of that variable, or '(Intercept)' to give a mean prediction.

value

A string with the column name of the broom::tidy() result that you would like to extract. By default this is value = 'estimate' to get the coefficients.

na.predict

Should observations in the data but not in the model, which do have nonmissing values of factor, be included in predictions? Setting this to FALSE requires that sending your model through fitted() produces a named vector.

Details

If multiple coefficients related to the factor are dropped from the model, be sure to check the result.

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
df <- data.frame(w1 = rnorm(1000),
                 w2 = rnorm(1000),
                 e1 = rnorm(1000),
                 e2 = rnorm(1000),
                 z = rnorm(1000),
                 groups = factor(floor(0:999/100)))
df$x <- df$w1+df$w2+df$z+df$e1

# Create a model with an interaction between z and groups
lm_inx <- lm(x ~ z*groups + w1 + w2, data = df)

# Get the effect of z for each group
indivfx <- factorPull(lm_inx,
                      data = df,
                      factor = 'groups',
                      interaction = 'z',
                      addterm = 'z')

# Create a model with a fixed effect for groups
lm_inx2 <- lm(x ~ groups + z + w1 + w2, data = df)

# Get the fixed effect for each group, with 0 for the reference group
indivfe <- factorPull(lm_inx,
                      data = df,
                      factor = 'groups')
# Or add in the intercept to get the full fixed effect
indivfe2 <- factorPull(lm_inx,
                       data = df,
                       factor = 'groups',
                       addterm = '(Intercept)')

NickCH-K/MagnifiedIV documentation built on Jan. 21, 2020, 11:38 a.m.