margplot: Plotting marginal effects for interactions in nonlinear...

Description Usage Arguments Examples

View source: R/margplot.R

Description

Plotting marginal effects for interactions in nonlinear probability and count models

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
margplot(
  model,
  vars,
  data,
  hyps = "means",
  foc,
  mod,
  modlevels,
  modnames = NULL,
  sm = T
)

Arguments

model

The estmiated model object. Currently, this may take logit or Poisson model objects estimated using the 'stats' package or negative binomial model objects tested using the 'MASS" package.

vars

The interacting variable names.

data

The data frame on which the estimated model is based.

hyps

User-specified levels of the predictor variables for displaying marginal effects. By default, this is specified at the mean values of all included covariates.

foc

Name of the variable that will be the focal (i.e., x-axis) variable

mod

Name of hte variable that will be the moderator (i.e., legend) variable

modlevels

A vector of hypothetical levels of the moderator at which to display marginal effects.

modnames

If the user wants to call the moderator levels something other Than what is provided in the data itself, the user can supply a vector of names here.

sm

Stands for "small multiple". If TRUE, marginal effects will be displayed as small multiple (i.e., trellis) plots. if FALSE, all marginal effects will appear on a single plot.

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
#Building from the modglm example, this outputs a ggplot object plotting the effect of x1 on
#the count of y across males and females:
set.seed(1678)
library(ggplot2)
b0 <- -3.8 ##Intercept
b1 <- .35 ###Sensation Seeking Effect
b2 <- .9 #Premeditation  Effect
b3 <- 1.1 #Sex covariate effect
b13<- .2 #product term coefficient
n<-1000 #Sample Size
mu<-rep(0,2) #Specify means
S<-matrix(c(1,.5,.5,1),nrow=2,ncol=2) #Specify covariance matrix
sigma <- 1 #Level 1 error

rawvars<-MASS::mvrnorm(n=n, mu=mu, Sigma=S) #simulates our
#continuous predictors from a multivariate normal distribution
cat<-rbinom(n=n,1,.5)
id<-seq(1:n)
eij <- rep(rnorm(id, 0, sigma))
xb<-  (b0) + (b1) * (rawvars[,1]) + (b2) * (rawvars[,2]) + (b3)*cat + b13*cat*(rawvars[,1]) + eij

ct <- exp(xb)
y <- rpois(n,ct)

df <- data.frame(y=y,senseek=rawvars[,1],premed=rawvars[,2],male=cat)

#premeditation, sex, and the interaction between sensation seeking and sex:

pois<-glm(y ~ senseek + premed + male + senseek:male, data=df,family="poisson")

p<-margplot(model=pois, vars=c("senseek","male"),foc="senseek", mod="male",
modlevels=c(0,1),data=df, hyps="means",sm=FALSE,modnames=c("Female","Male"))

#Because this is a ggplot object, we can modify anything further by adding elements to this plot. E.g.:
p + ylab("Count")

connorjmccabe/modglm documentation built on March 28, 2021, 10:45 p.m.