Description Usage Arguments Details Examples
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.
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
)
|
model |
A model object compatible with |
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 |
interaction |
A string with the name of a variable being interacted with |
specify |
For more complex sets of interactions, like three-way interactions, or if your |
basevalue |
If |
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 |
value |
A string with the column name of the |
na.predict |
Should observations in the data but not in the model, which do have nonmissing values of |
If multiple coefficients related to the factor are dropped from the model, be sure to check the result.
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)')
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.