Skip to content

Commit c56a1b3

Browse files
20251125 - partial regression plots
1 parent feac57f commit c56a1b3

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

regression.Rmd

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,46 @@ summary(imputedData_pooledEstimates)
596596
rockchalk::getDeltaRsquare(multipleRegressionModel)
597597
```
598598

599+
# Partial Regression Plot {#partialRegressionPlot}
600+
601+
A partial regression plot (aka added-variable) examines the association between the predictor and the outcome after controlling for the covariates.
602+
603+
```{r}
604+
car::avPlots(
605+
multipleRegressionModel,
606+
id = FALSE)
607+
```
608+
609+
Manually:
610+
611+
```{r}
612+
# DV residuals (outcome residualized on covariates)
613+
ry <- resid(lm(
614+
bpi_antisocialT2Sum ~ bpi_antisocialT1Sum,
615+
data = mydata, na.action = na.exclude))
616+
617+
# Predictor residuals (focal predictor residualized on covariates)
618+
rx <- resid(lm(
619+
bpi_anxiousDepressedSum ~ bpi_antisocialT1Sum,
620+
data = mydata, na.action = na.exclude))
621+
622+
# Create the partial regression plot
623+
df_plot <- data.frame(rx, ry)
624+
625+
ggplot(
626+
data = df_plot,
627+
mapping = aes(x = rx, y = ry)) +
628+
geom_point(alpha = 0.6) +
629+
geom_smooth(
630+
method = "lm",
631+
color = "blue") +
632+
labs(
633+
x = "Residualized bpi_antisocialT1Sum",
634+
y = "Residualized bpi_antisocialT2Sum",
635+
title = "Partial Regression Plot\nbpi_antisocialT2Sum ~ bpi_antisocialT1Sum controlling for bpi_anxiousDepressedSum"
636+
)
637+
```
638+
599639
# Dominance Analysis {#dominanceAnalysis}
600640

601641
```{r}

sem.Rmd

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,62 @@ https://isaactpetersen.github.io/Principles-Psychological-Assessment/sem.html#mo
27732773

27742774
For a list of tools to create path diagrams, see [here](figures.html#pathDiagrams).
27752775

2776+
# Partial Regression Plot {#partialRegressionPlot}
2777+
2778+
```{r}
2779+
# simulate extra observed covariates
2780+
N <- nrow(PoliticalDemocracy)
2781+
PoliticalDemocracy$z1 <- rnorm(N)
2782+
PoliticalDemocracy$z2 <- 0.3*PoliticalDemocracy$x1 + 0.3*PoliticalDemocracy$x2 + 0.3*PoliticalDemocracy$x3 + rnorm(N)
2783+
2784+
# model syntax
2785+
myModel <- '
2786+
# latent variables
2787+
ind60 =~ x1 + x2 + x3
2788+
dem60 =~ y1 + y2 + y3 + y4
2789+
dem65 =~ y5 + y6 + y7 + y8
2790+
# regressions
2791+
dem60 ~ ind60
2792+
dem65 ~ ind60 + dem60 + z1 + z2
2793+
# residual covariances
2794+
y1 ~~ y5
2795+
y2 ~~ y4 + y6
2796+
y3 ~~ y7
2797+
y4 ~~ y8
2798+
y6 ~~ y8
2799+
'
2800+
2801+
# fit the model
2802+
fit <- sem(
2803+
model = myModel,
2804+
data = PoliticalDemocracy)
2805+
2806+
# extract the factor scores
2807+
factorScores <- as.data.frame(lavaan::lavPredict(fit))
2808+
2809+
# merge the factor scores with the original data
2810+
mydata <- cbind(PoliticalDemocracy, factorScores)
2811+
2812+
# residualize latent Y (dem65) on covariates
2813+
ry <- resid(lm(dem65 ~ dem60 + z1 + z2, data = mydata))
2814+
2815+
# residualize latent X (ind60) on same covariates
2816+
rx <- resid(lm(ind60 ~ dem60 + z1 + z2, data = mydata))
2817+
2818+
# create the partial regression plot
2819+
ggplot(
2820+
data = data.frame(rx, ry),
2821+
mapping = aes(rx, ry)) +
2822+
geom_point() +
2823+
geom_smooth(
2824+
method = "lm") +
2825+
labs(
2826+
x = "Residualized ind60",
2827+
y = "Residualized dem65",
2828+
title = "Partial Regression Plot from SEM"
2829+
)
2830+
```
2831+
27762832
# Session Info
27772833

27782834
```{r, class.source = "fold-hide"}

0 commit comments

Comments
 (0)