modglm: Computing Interactions in nonlinear probability and count...

Description Usage Arguments Examples

View source: R/modglm.R

Description

Computing Interactions in nonlinear probability and count models

Usage

1
modglm(model, vars, data, hyps = "means", plotby = NULL, type = "cpd")

Arguments

model

The estmiated model object. #Currently, this may ntake logit or Poisson model objects #estimated using the 'stats' package or negative binomial #model objects ested 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 evaluating the interaction function. By default, #this is specified at the mean values of all included covariates.

plotby

An option to view the interaction effect #estimates plot across categories of another variable. Default is NULL.

type

The type of interaction being estimated. #Options are ""cpd", "fd", and "dd". "cpd" indicates #cross-partial derivative, and should be used when both #variables are continuous. "fd" indicates finite #difference, and should be used when one variable #is continuous and the other is discrete. #"dd" indicates double (finite) difference, and should be #used if when both variables are categorical.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#Simulate a dataset
set.seed(1678)
require(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")

#Evaluated the interaction function (i.e., computes
#finite difference values) for sensation seeking and sex
pois.ints<-modglm(model=pois, vars=c("senseek","male"), data=df, type="fd", hyps="means")

names(pois.ints)

#`obints` provides the interaction effect conditioned on each observation in the data. E.g:
head(pois.ints$obints)

#`inthyp` provides the results of the hypothetical condition
# specified by 'hyps' above. In this case, this is
#represented at the mean of all predictors:
pois.ints$inthyp

#`aie` refers to the average interaction effect.
#This is computed as the mean of all interaction
#effects in the observed data:
pois.ints$aie

#`desc` provides several other helpful descriptors
#of the interaction effect that researchers may
#wish to report:
pois.ints$desc

#`intsplot` provides a graphical depiction of the interaction
#point estimates computed observation-wise, plotted against
#the model-predicted outcome (see also Ai & Norton, 2003)

pois.ints$intsplot

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