# LOAD LIBRARIES install.packages(c("tidyverse","lme4","lmerTest","emmeans","broom.mixed","performance")) library(tidyverse); library(lme4); library(lmerTest) library(emmeans); library(broom.mixed); library(performance) # RENAME COLUMNS CNMforRdata <- Data %>% rename( MACS_Time1 = MACS1, MACS_Time2 = MACS2, MACSdys_Time1= MACSdys1, MACSdys_Time2= MACSdys2, MACSimm_Time1= MACSimm1, MACSimm_Time2= MACSimm2, MACSunh_Time1= MACSunhealthy1, MACSunh_Time2= MACSunhealthy2, CNMa_Time1 = CNMacc1, CNMa_Time2 = CNMacc2, relknow_Time1= RelKnow1, relknow_Time2= RelKnow2, ClassBel_Time1 = ClassBel1, ClassBel_Time2 = ClassBel2, LGBTat_Time1 = LGBTAcc1, LGBTat_Time2 = LGBTAcc2 ) # LONG FORMAT long_data <- CNMforRdata %>% pivot_longer( cols = c( MACS_Time1, MACS_Time2, MACSdys_Time1, MACSdys_Time2, MACSimm_Time1, MACSimm_Time2, MACSunh_Time1, MACSunh_Time2, CNMa_Time1, CNMa_Time2, relknow_Time1, relknow_Time2, ClassBel_Time1, ClassBel_Time2, LGBTat_Time1, LGBTat_Time2 ), names_to = c("variable","time"), names_sep = "_", values_to = "value" ) # FILTERING long_data <- long_data %>% filter(!(race %in% c(1,5,7))) %>% # exclude low‑n race groups mutate( time = factor(time, levels = c("Time1","Time2")), variable = factor(variable), gender = factor(gender), hetero = factor(hetero), race = factor(race), income = factor(income, levels=1:10, ordered=TRUE), neuro = factor(neuro), disability = factor(disability), ID = factor(ID) ) %>% droplevels() # LINEAR MIXED-EFFECTS MODEL model <- lmer( value ~ time * variable + gender + race + income + hetero + neuro + disability + (1 | ID), data = long_data ) summary(model) r2(model) # LABEL VARIABLES long_data$variable <- factor( long_data$variable, levels = c("relknow","LGBTat","ClassBel","CNMa","MACS","MACSunh","MACSdys","MACSimm"), labels = c( "Relationship Knowledge*", "LGBTQ+ Acceptance", "Classroom Belonging*", "CNM Acceptance*", "CNM Stigma (TOTAL)*", "CNM Unhealthy*", "CNM Dysfunctional*", "CNM Immoral" ) ) # LOAD LIBRARY FOR EMMEANS library(emmeans) # LIMITS FOR EMMEANS emm_options( pbkrtest.limit = 5000, lmerTest.limit = 5000, rg.limit = 65000 # Raise to just above your need ) # RUN EMMEANS emm <- emmeans(model, ~ time * variable) emm_summary <- summary(emm) summary(emm_summary) # EMMEANS: TIME X VARIABLE emm <- emmeans(model, ~ time * variable, rg.limit = 100000) # PAIRWISE COMPARISONS emm_results <- pairs(emm, by = "variable") summary(emm_results) library(dplyr) library(emmeans) # LIMITS EMMEANS emm_options(rg.limit = 100000, pbkrtest.limit = 10000, lmerTest.limit = 10000) # MODEL FIT model <- lmer(value ~ time * variable + gender + race + income + hetero + neuro + disability + (1|ID), data = long_data) # PAIRWISE COMPARISONS do_pairwise <- function(varname, model_obj, adjust_method = "tukey") { formula_str <- paste0("~ ", varname) emm_obj <- emmeans(model_obj, as.formula(formula_str)) contrast_obj <- contrast(emm_obj, method = "pairwise") summary(contrast_obj, infer = TRUE, adjust = adjust_method) %>% mutate(Covariate = varname) } # COVARIATES covariates <- c("gender", "hetero", "race", "income", "neuro", "disability") # RUN pairwise_all <- bind_rows(lapply(covariates, function(cv) { do_pairwise(cv, model) })) # PRINT SIGNIFICANT COMPARISONS pairwise_sig <- pairwise_all %>% filter(p.value < 0.05) # FULL RESULTS print(as.data.frame(pairwise_sig), row.names = FALSE) # CSV EXPORT write.csv(pairwise_sig, "pairwise_covariate_comparisons_significant.csv", row.names = FALSE) # FOR PLOT library(ggplot2) long_data$variable <- factor( as.character(long_data$variable), levels = c( "Classroom Belonging*", "Relationship Knowledge*", "LGBTQ+ Acceptance", "CNM Acceptance*", "CNM Unhealthy*", "CNM Dysfunctional*", "CNM Immoral", "CNM Stigma (TOTAL)*" ) ) ggplot(long_data, aes(x = time, y = value, color = variable, group = variable)) + stat_summary(fun = mean, geom = "line", size = 1.1) + stat_summary(fun = mean, geom = "point", size = 2) + stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.05, size = 0.4) + theme_minimal(base_size = 14) + theme( panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.text = element_text(size = 11) ) + labs(x = "", y = "Mean Score", color = "") + scale_color_manual( # legend order breaks = c( "Classroom Belonging*", "Relationship Knowledge*", "LGBTQ+ Acceptance", "CNM Acceptance*", "CNM Unhealthy*", "CNM Dysfunctional*", "CNM Immoral", "CNM Stigma (TOTAL)*" ), # legend labels labels = c( "Classroom Belonging*", "Relationship Knowledge*", "LGBTQ+ Acceptance", "CNM Acceptance*", "CNM Unhealthy Stigma*", "CNM Dysfunctional Stigma*", "CNM Immoral Stigma", "CNM TOTAL Stigma*" ), # legend colors values = c( "Classroom Belonging*" = "#009E73", "Relationship Knowledge*" = "#0072B2", "LGBTQ+ Acceptance" = "#56B4E9", "CNM Acceptance*" = "#E69F00", "CNM Unhealthy*" = "#CC79A7", "CNM Dysfunctional*" = "#000000", "CNM Immoral" = "#D55E00", "CNM Stigma (TOTAL)*" = "#F0E442" ) ) + scale_y_continuous(limits = c(1, 7), breaks = c(1, 3, 5, 7))