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 |
#packages we will need to conduct to create and graph our data
library(MASS) #create data
library(car) #graph 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
#build the correlated variables. Note: empirical=TRUE means make the correlation EXACTLY r.
# if we say empirical=FALSE, the correlation would be normally distributed around r
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")
#make the scatter plots
scatterplot(Memory~Practice,CorrDataT, smoother=FALSE)
scatterplot(Memory~Anxiety,CorrDataT, smoother=FALSE)
scatterplot(Anxiety~Practice,CorrDataT, smoother=FALSE)
# Pearson Correlations
ry1<-cor(CorrDataT$Memory,CorrDataT$Practice)
ry2<-cor(CorrDataT$Memory,CorrDataT$Anxiety)
r12<-cor(CorrDataT$Anxiety,CorrDataT$Practice)
R2y.12<-sqrt((ry1^2+ry2^2 - (2*ry1*ry2*r12))/(1-r12^2))^2
a = R2y.12 -ry2^2
b = R2y.12 -ry1^2
scatterplot(Practice~Anxiety,CorrDataT, smoother=FALSE)
CorrDataT$Practice.control.Anxiety<-residuals(lm(Practice~Anxiety, CorrDataT))
scatterplot(Practice.control.Anxiety~Anxiety,CorrDataT, smoother=FALSE)
Sr1.alt<-cor(CorrDataT$Memory,CorrDataT$Practice.control.Anxiety)
If we square the correlation value we got 0.5031767, it becomes 0.2531868 which matches our \(a\) from the analysis above.
scatterplot(Anxiety~Practice,CorrDataT, smoother=FALSE)
CorrDataT$Anxiety.control.Practice<-residuals(lm(Anxiety~Practice, CorrDataT))
scatterplot(Anxiety.control.Practice~Practice,CorrDataT, smoother=FALSE)
Sr2.alt<-cor(CorrDataT$Memory,CorrDataT$Anxiety.control.Practice)
If we square the correlation value we got 0.2306227, it becomes 0.0531868 which matches our \(b\) from the analysis above.
library(ppcor)
Sr1<-spcor.test(CorrDataT$Memory, CorrDataT$Practice, CorrDataT$Anxiety)
Sr2<-spcor.test(CorrDataT$Memory, CorrDataT$Anxiety, CorrDataT$Practice)
# Extract result call for Sr1$estimate
# Center variables (if you said scale = TRUE it would zscore the predictors)
CorrDataT$Memory.Z<-scale(CorrDataT$Memory, scale=TRUE, center=TRUE)[,]
CorrDataT$Practice.Z<-scale(CorrDataT$Practice, scale=TRUE, center=TRUE)[,]
CorrDataT$Anxiety.Z<-scale(CorrDataT$Anxiety, scale=TRUE, center=TRUE)[,]
###############Model 1
M.Model.1<-lm(Memory.Z~ Practice.Z, data = CorrDataT)
M.Model.2<-lm(Memory.Z~ Anxiety.Z, data = CorrDataT)
M.Model.3<-lm(Memory.Z~ Practice.Z+Anxiety.Z, data = CorrDataT)
library(stargazer)
stargazer(M.Model.1,M.Model.2,M.Model.3,type="latex",
intercept.bottom = FALSE, single.row=TRUE,
star.cutoffs=c(.05,.01,.001), notes.append = FALSE,
header=FALSE)
Thus, \[R_{model.3_{P+A}}^2 - sr_{Anxiety}^2 = R^2_{Practice.only}\]
R2.Practice.only = (summary(M.Model.3)$r.squared) - (Sr2$estimate^2)
Thus, \[R_{model.3.P+A}^2 - sr_{Practice}^2 = R^2_{Anxiety.only}\]
R2.Anxiety.only = (summary(M.Model.3)$r.squared) - (Sr1$estimate^2)
M.Model.4<-lm(Memory.Z~ Practice.control.Anxiety, data = CorrDataT)
M.Model.5<-lm(Memory.Z~ Anxiety.control.Practice, data = CorrDataT)
stargazer(M.Model.3,M.Model.4,M.Model.5,type="latex",
intercept.bottom = FALSE, single.row=TRUE,
star.cutoffs=c(.05,.01,.001), notes.append = FALSE,
header=FALSE)
Partial correlation asks how much of the Y variance, which is not estimated by the other IVs, is estimated by this variable.
It removes the shared variance of the control variable (Say X2) from both Y and X1.
\(pr_1^2: = \frac{a}{a+e} = \frac{R_{Y.12}^2 - r_{Y2}^2}{1-r_{Y2}^2}\)
\(pr_2^2: \frac{b}{b+e} = \frac{R_{Y.12}^2 - r_{Y1}^2}{1-r_{Y1}^2}\)
Another way to understand it:
# Control for Anxiety
CorrDataT$Memory.control.Anxiety<-residuals(lm(Memory~Anxiety, CorrDataT))
CorrDataT$Practice.control.Anxiety<-residuals(lm(Practice~Anxiety, CorrDataT))
scatterplot(Memory.control.Anxiety~Practice.control.Anxiety,CorrDataT, smoother=FALSE)
# Control for Practice Time
CorrDataT$Memory.control.Practice<-residuals(lm(Memory~Practice, CorrDataT))
CorrDataT$Anxiety.control.Practice<-residuals(lm(Anxiety~Practice, CorrDataT))
scatterplot(Memory.control.Practice~Anxiety.control.Practice,CorrDataT, smoother=FALSE)
library(apa)
Res.pr1<-cor_apa(cor.test(CorrDataT$Practice.control.Anxiety,CorrDataT$Memory.control.Anxiety,
method = c("pearson")),format ="latex",print = FALSE)
Res.pr2<-cor_apa(cor.test(CorrDataT$Anxiety.control.Practice,CorrDataT$Memory.control.Practice,
method = c("pearson")),format ="latex",print = FALSE)
pr1<-pcor.test(CorrDataT$Memory, CorrDataT$Practice, CorrDataT$Anxiety)
pr2<-pcor.test(CorrDataT$Memory, CorrDataT$Anxiety, CorrDataT$Practice)
Partial.Model.1<-lm(Memory.control.Anxiety~ Practice.control.Anxiety+Anxiety.Z,
data = CorrDataT)
stargazer(Partial.Model.1,type="latex",
intercept.bottom = FALSE, single.row=TRUE,
star.cutoffs=c(.05,.01,.001), notes.append = FALSE,
header=FALSE)
Partial.Model.2<-lm(Memory.control.Practice~ Anxiety.control.Practice+Practice.Z,
data = CorrDataT)
stargazer(Partial.Model.2,type="latex",
intercept.bottom = FALSE, single.row=TRUE,
star.cutoffs=c(.05,.01,.001), notes.append = FALSE,
header=FALSE)