X1 and X2 are causing Y, but X1 is causing X2 thus making X2 indirect as well
Both occur when: \(r_{Y1} > r_{Y2}r_{12}\) & \(r_{Y2} > r_{Y1}r_{12}\)
Hard to know whether you have the Model A or B
Variable | Practice Time (X1) | Performance Anxiety (X2) | Memory Errors (Y) |
---|---|---|---|
Practice Time (X1) | 1 | .3 | .6 |
Performance Anxiety (X2) | .3 | 1 | .4 |
Memory Errors (Y) | .6 | .4 | 1 |
library(MASS) #create data
py1 =.6 #Cor between X1 (Practice Time) and Memory Errors
py2 =.4 #Cor between X2 (Performance Anxiety) and Memory Errors
p12= .3 #Cor between X1 (Practice Time) and X2 (Performance Anxiety)
Means.X1X2Y<- c(10,10,10) #set the means of X and Y variables
CovMatrix.X1X2Y <- matrix(c(1,p12,py1,
p12,1,py2,
py1,py2,1),3,3) # creates the covariate matrix
set.seed(42)
CorrDataT<-mvrnorm(n=100, mu=Means.X1X2Y,Sigma=CovMatrix.X1X2Y, empirical=TRUE)
#Convert them to a "Data.Frame" & add our labels to the vectors we created
CorrDataT<-as.data.frame(CorrDataT)
colnames(CorrDataT) <- c("Practice","Anxiety","Memory")
library(stargazer)
###############Model 1
Model.1<-lm(Memory~ Practice, data = CorrDataT)
###############Model 2
Model.2<-lm(Memory~ Practice+Anxiety, data = CorrDataT)
stargazer(Model.1,Model.2,type="latex",
intercept.bottom = FALSE, single.row=TRUE,
star.cutoffs=c(.05,.01,.001), notes.append = FALSE,
header=FALSE)
library(effects)
#plot individual effects
Model.2.Plot <- allEffects(Model.2,
xlevels=list(Practice=seq(8, 12, 1),
Anxiety=seq(8, 12, 1)))
plot(Model.2.Plot, 'Practice', ylab="Memory")
plot(Model.2.Plot, 'Anxiety', ylab="Memory")
I.py1 =.6 #Cor between X1 (Working Memory Digit Span) and Memory Errors
I.py2 =.6 #Cor between X2 (Auditory Working Memory) and Memory Errors
I.p12 =.9999 #Cor between X1 Working Memory and Auditory Working Memory
I.Means.X1X2Y<- c(10,10,10) #set the means of X and Y variables
I.CovMatrix.X1X2Y <- matrix(c(1,I.p12,I.py1,
I.p12,1,I.py2,
I.py1,I.py2,1),3,3) # creates the covariate matrix
set.seed(42)
IData<-mvrnorm(n=100, mu=I.Means.X1X2Y,Sigma=I.CovMatrix.X1X2Y, empirical=TRUE)
IData<-as.data.frame(IData)
#lets add our labels to the vectors we created
colnames(IData) <- c("WM","AWM","Errors")
###############Model 1
Spurious.1<-lm(Errors~ WM, data = IData)
###############Model 2
Spurious.2<-lm(Errors~ AWM, data = IData)
###############Model 3
Spurious.3<-lm(Errors~ WM+AWM, data = IData)
stargazer(Spurious.1,Spurious.2,Spurious.3,type="latex",
intercept.bottom = FALSE, single.row=TRUE,
star.cutoffs=c(.05,.01,.001), notes.append = FALSE,
header=FALSE)
#plot individual effects
Tax.Model.Plot <- allEffects(TaxCutsOnly,
xlevels=list(TaxCuts=seq(3, 7, 1)))
plot(Tax.Model.Plot, 'TaxCuts', ylab="Growth")
#plot individual effects
Full.Model.Plot <- allEffects(Full.Model,
xlevels=list(TaxCuts=seq(3, 7, 1),
inflation=seq(3, 7, 1)))
plot(Full.Model.Plot, 'TaxCuts', ylab="Growth")
plot(Full.Model.Plot, 'inflation', ylab="Growth")
#plot both effects
Full.Model.Plot2<-Effect(c("TaxCuts", "inflation"), Full.Model,
xlevels=list(TaxCuts=c(3, 7),
inflation=c(3,7)))
plot(Full.Model.Plot2)
Linear.Model<-lm(Memory~ Practice+Anxiety, data = CorrDataT)
library(car)
vif(Linear.Model) # variance inflation factors
## Practice Anxiety
## 1.098901 1.098901
vif(Linear.Model) > 4 # problem?
## Practice Anxiety
## FALSE FALSE
#To see them all at once
layout(matrix(c(1,2,3,4),2,2)) # optional 4 graphs/page
plot(Linear.Model)
# Evaluate homoscedasticity
# non-constant error variance test
ncvTest(Linear.Model)
## Non-constant Variance Score Test
## Variance formula: ~ fitted.values
## Chisquare = 6.705913, Df = 1, p = 0.0096094
# distribution of studentized residuals
sresid <- studres(Linear.Model)
hist(sresid, freq=FALSE,
main="Distribution of Studentized Residuals")
xfit<-seq(min(sresid),max(sresid),length=40)
yfit<-dnorm(xfit)
lines(xfit, yfit)
# Cook's D plot
# identify D values > 4/(n-k-1)
cutoff <- 4/((nrow(CorrDataT)-length(Linear.Model$coefficients)-2))
cutoff
## [1] 0.04210526
plot(Linear.Model, which=4, cook.levels=cutoff)
plot(Linear.Model, which=5, cook.levels=cutoff)
CorrDataT$CooksD<-(cooks.distance(Linear.Model))
CorrDataT_Cleaned<-subset(CorrDataT,CooksD < cutoff)
nrow(CorrDataT_Cleaned)
## [1] 96
# Assessing Outliers
outlierTest(Linear.Model) # Bonferonni p-value for most extreme obs
## rstudent unadjusted p-value Bonferroni p
## 18 3.906991 0.00017396 0.017396
# Evaluate Nonlinearity
# component + residual plot aka partial-residual plots
crPlots(Linear.Model)
# Test for Autocorrelated Errors
durbinWatsonTest(Linear.Model)
## lag Autocorrelation D-W Statistic p-value
## 1 0.08884707 1.814168 0.356
## Alternative hypothesis: rho != 0
library(texreg)
#htmlreg(list(Linear.Model),file = "ModelResults.doc",
# single.row = FALSE, stars = c(0.001, 0.01, 0.05,0.1),digits=3,
# inline.css = FALSE, doctype = TRUE, html.tag = TRUE,
# head.tag = TRUE, body.tag = TRUE)