set.seed(12345)
x1 <- rnorm(100)
x2 <- rnorm(100)
x3 <- rnorm(100)
x4 <- rnorm(100)
y <- rnorm(100)
y2 <- 0.03 + 0.1*x1 + 0.1*x2 + 0.25*x1*x2 + 0.4*x3 -0.1*x4 + 1*rnorm(100)
dat <- data.frame(x1,x2,x3,x4,y, y2)
rm(x1, x2, x3, x4, y, y2)
##ordinary regression
m1 <- lm(y ~ x1 + x2 +x3 + x4, data=dat)
## must specify depvar parameter
plotSlopes(m1, plotx="x1", modx="x2", modxVals=c(-0.5,0,0.5))
plotSlopes(m1, plotx="x1", modx="x2")
plotSlopes(m1, plotx="x4", modx="x1")
m2 <- lm(y2 ~ x1*x2 + x3 +x4, data=dat)
summary(m2)
plotSlopes(m2, plotx="x1", modx="x2")
plotSlopes(m2, plotx="x1", modx="x2", modxVals=c( -2, -1, 0, 1, 2))
plotSlopes(m2, plotx="x3", modx="x2")
m3 <- lm(y2 ~ x1 * x2 + x3, data=dat)
plotSlopes(m3, plotx="x3", modx="x2")
plotSlopes(m3, plotx="x1", modx="x2")
plotSlopes(m3, plotx="x2", modx="x3")
### Examples with categorical Moderator variable
xcontinuous <- rnorm(100)
xcategorical <- gl(2,50, labels=c("Gigantic","Humongous"))
stde <- 8
y <- 3 + 0.5*xcontinuous + 1.2 * (as.numeric(xcategorical)-1) +
-0.8* (as.numeric(xcategorical)-1) * xcontinuous + stde * rnorm(100)
m1 <- lm (y ~ xcontinuous + xcategorical)
summary(m1)
plotSlopes(m1, modx = "xcategorical", plotx = "xcontinuous")
m2 <- lm (y ~ xcontinuous * xcategorical)
summary(m2)
plotSlopes(m2, modx = "xcategorical", plotx = "xcontinuous")
library(car)
m3 <- lm(statusquo ~ income * sex, data = Chile)
summary(m3)
plotSlopes(m3, modx = "sex", plotx = "income")
m4 <- lm(statusquo ~ region * income, data= Chile)
summary(m4)
plotSlopes(m4, modx = "region", plotx = "income")
plotSlopes(m4, modx = "region", plotx = "income", plotPoints=FALSE)
m5 <- lm(statusquo ~ region * income + sex + age, data= Chile)
summary(m5)
plotSlopes(m5, modx = "region", plotx = "income")
m6 <- lm(statusquo ~ income * age + education + sex + age, data=Chile)
summary(m6)
plotSlopes(m6, modx = "income", plotx = "age")
plotSlopes(m6, modx = "income", plotx = "age", plotPoints=FALSE)
## Should cause error because education is not numeric
## m7 <- lm(statusquo ~ income * age + education + sex + age, data=Chile)
## summary(m7)
## plotSlopes(m7, modx = "income", plotx = "education")
## Should cause error because "as.numeric(education") not same as
## plotx="education"
## m8 <- lm(statusquo ~ income * age + as.numeric(education) + sex + age, data=Chile)
## summary(m8)
## plotSlopes(m8, modx = "income", plotx = "education")
## Still fails.
## plotSlopes(m8, modx = "income", plotx = "as.numeric(education)")
## Must recode variable first so that variable name is coherent
Chile$educationn <- as.numeric(Chile$education)
m9 <- lm(statusquo ~ income * age + educationn + sex + age, data=Chile)
summary(m9)
plotSlopes(m9, modx = "income", plotx = "educationn")
Run the code above in your browser using DataLab