Learn R Programming

plotMElm (version 0.1.5)

plot_me: Plot marginal effects from two-way interactions in linear regressions

Description

Plot marginal effects from two-way interactions in linear regressions

Usage

plot_me(obj, term1, term2, fitted2, ci = 95, ci_type = "standard",
  t_statistic, plot = TRUE)

Arguments

obj

fitted model object from lm.

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 term2 to plot for. If unspecified, then all unique observed values are used.

ci

numeric. confidence interval level, expressed on the ]0, 100[ interval. The default is 95.

ci_type

character string specifying the type of confidence interval to find and plot. If 'standard' then standard confidence intervals (e.g. those suggested by Brambor, Clark, and Golder 2006) are found. If fdr then confidence intervals are found using critical t-statistics to limit the false discovery rate (limit over confidence).

t_statistic

numeric. Custom t-statistic for finding the confidence interval. May be useful if the user want to use a funciton like findMultiLims to find the t-statistic.

plot

boolean. return plot if TRUE; return data.frame of marginal effects estimates if FALSE.

Value

a gg class ggplot2 object

Examples

Run this code
# NOT RUN {
## 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')

# }

Run the code above in your browser using DataLab