str(Rohwer)
# Plot responses against each predictor
library(tidyr)
library(dplyr)
library(ggplot2)
yvars <- c("SAT", "PPVT", "Raven" )
xvars <- c("n", "s", "ns", "na", "ss")
Rohwer_long <- Rohwer %>%
pivot_longer(cols = all_of(xvars), names_to = "xvar", values_to = "x") |>
pivot_longer(cols = all_of(yvars), names_to = "yvar", values_to = "y") |>
mutate(xvar = factor(xvar, xvars), yvar = factor(yvar, yvars))
ggplot(Rohwer_long, aes(x, y, color = SES, shape = SES, fill = SES)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, formula = y ~ x) +
stat_ellipse(geom = "polygon", level = 0.68, alpha = 0.1) +
facet_grid(yvar ~ xvar, scales = "free") +
labs(x = "predictor", y = "response") +
theme_bw(base_size = 14)
## ANCOVA, assuming equal slopes
rohwer.mod <- lm(cbind(SAT, PPVT, Raven) ~ SES + n + s + ns + na + ss, data=Rohwer)
car::Anova(rohwer.mod)
# Visualize the ANCOVA model
heplot(rohwer.mod)
# Add ellipse to test all 5 regressors
heplot(rohwer.mod, hypotheses=list("Regr" = c("n", "s", "ns", "na", "ss")))
# View all pairs
pairs(rohwer.mod, hypotheses=list("Regr" = c("n", "s", "ns", "na", "ss")))
# or 3D plot
if (FALSE) {
col <- c("red", "green3", "blue", "cyan", "magenta", "brown", "gray")
heplot3d(rohwer.mod, hypotheses=list("Regr" = c("n", "s", "ns", "na", "ss")),
col=col, wire=FALSE)
}
## fit separate, independent models for Lo/Hi SES
rohwer.ses1 <- lm(cbind(SAT, PPVT, Raven) ~ n + s + ns + na + ss, data=Rohwer, subset=SES=="Hi")
rohwer.ses2 <- lm(cbind(SAT, PPVT, Raven) ~ n + s + ns + na + ss, data=Rohwer, subset=SES=="Lo")
# overlay the separate HE plots
heplot(rohwer.ses1, ylim=c(40,110),col=c("red", "black"))
heplot(rohwer.ses2, add=TRUE, col=c("blue", "black"), grand.mean=TRUE, error.ellipse=TRUE)
Run the code above in your browser using DataLab