Title: | Plot Marginal Effects from Linear Models |
---|---|
Description: | Plot marginal effects for interactions estimated from linear models. |
Authors: | Christopher Gandrud [aut, cre] |
Maintainer: | Christopher Gandrud <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.6 |
Built: | 2024-11-01 02:52:42 UTC |
Source: | https://github.com/christophergandrud/plotmelm |
Plot marginal effects from two-way interactions in linear regressions
plot_me(obj, term1, term2, fitted2, ci = 95, ci_type = "standard", t_statistic, plot = TRUE)
plot_me(obj, term1, term2, fitted2, ci = 95, ci_type = "standard", t_statistic, plot = TRUE)
obj |
fitted model object from |
term1 |
character string of the first constitutive term of the interaction's variable name. |
term2 |
character string of the second constitutive term of the interaction's variable name. |
fitted2 |
numeric vector of fitted values of |
ci |
numeric. confidence interval level, expressed on the ]0, 100[
interval. The default is |
ci_type |
character string specifying the type of confidence interval
to find and plot. If |
t_statistic |
numeric. Custom t-statistic for finding the confidence interval.
May be useful if the user want to use a funciton like |
plot |
boolean. return plot if |
a gg
class ggplot2 object
Inspired by: http://www.statsblogs.com/2013/08/27/creating-marginal-effect-plots-for-linear-regression-models-in-r/
Benjamini, Yoav, and Yosef Hochberg. 1995. "Controlling the False Discovery Rate: A Practical and Powerful Approach to Multiple Testing". Journal of the Royal Statistical Society, Series B 57(1): 289–300.
Brambor, Thomas, William Roberts Clark, and Matt Golder. "Understanding interaction models: Improving empirical analyses". Political Analysis 14.1 (2006): 63-82.
Esarey, Justin, and Jane Lawrence Sumner. 2015. "Marginal Effects in Interaction Models: Determining and Controlling the False Positive Rate". URL: http://jee3.web.rice.edu/interaction-overconfidence.pdf.
## Continuous Term1 and Term2 # Estimate model states <- as.data.frame(state.x77) m1 <- lm(Murder ~ Income * Population, data = states) # Plot marginal effect of Income across the observed range of Population # on the Murder rate plot_me(m1, 'Income', 'Population', ci = 95) # CI created using false discovery rate limiting t-statistic plot_me(m1, 'Income', 'Population', ci_type = 'fdr') # Return marginal effects as a data frame plot_me(m1, 'Income', 'Population', plot = FALSE) ## Term 2 with <= 5 unique values # Estimate model m2 <- lm(mpg ~ wt * cyl, data = mtcars) # Plot marginal effect of Weight across the Number of Cylinders (continuous) plot_me(m2, 'wt', 'cyl') ## Categorical (factor) Term2 # Set Term 2 as a factor variable mtcars$cyl <- factor(mtcars$cyl, labels = c('4 Cyl', '6 Cyl', '8 Cyl')) # Estimate model m3 <- lm(mpg ~ wt * cyl, data = mtcars) # Plot marginal effect of Weight across the Number of Cylinders (factor) plot_me(m3, 'wt', 'cyl')
## Continuous Term1 and Term2 # Estimate model states <- as.data.frame(state.x77) m1 <- lm(Murder ~ Income * Population, data = states) # Plot marginal effect of Income across the observed range of Population # on the Murder rate plot_me(m1, 'Income', 'Population', ci = 95) # CI created using false discovery rate limiting t-statistic plot_me(m1, 'Income', 'Population', ci_type = 'fdr') # Return marginal effects as a data frame plot_me(m1, 'Income', 'Population', plot = FALSE) ## Term 2 with <= 5 unique values # Estimate model m2 <- lm(mpg ~ wt * cyl, data = mtcars) # Plot marginal effect of Weight across the Number of Cylinders (continuous) plot_me(m2, 'wt', 'cyl') ## Categorical (factor) Term2 # Set Term 2 as a factor variable mtcars$cyl <- factor(mtcars$cyl, labels = c('4 Cyl', '6 Cyl', '8 Cyl')) # Estimate model m3 <- lm(mpg ~ wt * cyl, data = mtcars) # Plot marginal effect of Weight across the Number of Cylinders (factor) plot_me(m3, 'wt', 'cyl')