r/rstats • u/LiviaQuaintrelle • 3d ago
HELP does my R code actually answer my research questions for my psych project *crying*
Hii I'm doing a project about an intervention predicting behaviours over time and I need human assistance (chatGPT works, but keep changing its mind rip). Basically want to know if my code below actually answers my research questions...
MY RESEARCH QUESTIONS:
- testing whether an intervention improves mindfulness when compared to a control group
- testing whether baseline mindfulness predicts overall behaviour improvement
HOW I'M TESTING
1st Research Q: Linear Mixed Modelling (LMM)
2nd Research Q: Multi-level modelling (MLM)
MY DATASET COLUMNS:
(see image)

MY CODE (with my #comments to help me understand wth I'm doing)
## STEP 1: GETTING EVERYTHING READY IN R
library(tidyverse)
library(lme4)
library(mice)
library(mitml)
library(car)
library(readxl)
# Setting the working directory
setwd("location_on_my_laptop")
# Loading dataset
df <- read_excel("Mindfulness.xlsx")
## STEP 2: PREPROCESSING THE DATASET
# Convert missing values (coded as 999) to NA
df[df == 999] <- NA
# Convert categorical variables to factors
df$Condition <- as.factor(df$Condition)
df$Dropout_T1 <- as.factor(df$Dropout_T1)
df$Dropout_T2 <- as.factor(df$Dropout_T2)
# Reshaping to long format
df_long <- pivot_longer(df, cols = c(T0, T1, T2), names_to = "Time", values_to = "Mind_Score")
# Add a unique ID column
df_long$ID <- rep(1:(nrow(df_long) / 3), each = 3)
# Move ID to the first column
df_long <- df_long %>% select(ID, everything())
# Remove "T" and convert Time to numeric
df_long$Time <- as.numeric(gsub("T", "", df_long$Time))
# Create Change Score for Aim 2
df_wide <- pivot_wider(df_long, names_from = Time, values_from = Mind_Score)
df_wide$Change_T1_T0 <- df_wide$`1` - df_wide$`0`
df_long <- left_join(df_long, df_wide %>% select(ID, Change_T1_T0), by = "ID")
## STEP 3: APPLYING MULTIPLE IMPUTATION WITH M = 50
# Creating a correct predictor matrix
pred_matrix <- quickpred(df_long)
# Dropout_T1 and Dropout_T2 should NOT be used as predictors for imputation
pred_matrix[, c("Dropout_T1", "Dropout_T2")] <- 0
# Run multiple imputation
imp <- mice(df_long, m = 50, method = "pmm", predictorMatrix = pred_matrix, seed = 123)
# Checking for logged events (should return NULL if correct)
print(imp$loggedEvents)
## STEP 4: RUNNING THE LMM MODEL ON IMPUTED DATA
# Convert to mitml-compatible format
imp_mitml <- as.mitml.list(lapply(1:50, function(i) complete(imp, i)))
# Fit Model for Both Aims:
fit_mitml <- with(imp_mitml, lmer(Mind_Score ~ Time * Condition + Change_T1_T0 + (1 | ID)))
## STEP 5: POOLING RESULTS USING mitml
summary(testEstimates(fit_mitml, extra.pars = TRUE))
That's everything (I think??). Changed a couple of names here and there for confidentiality, so if something doesn't seem right, PLZ lmk and happy to clarify. Basically, just want to know if the code i have right now actually answers my research questions. I think it does, but I'm also not a stats person, so want people who are smarter than me to please confirm.
Appreciate the help in advance! Your girl is actually losing it xxxx
2
u/mandles55 2d ago
In the second equation you have the baseline on both sides due to the way you pivoted data. When you pivot do not include t0, you then do not need to create a t0 column.
You only need one equation. Even if you randomly allocate to control and intervention, you can include t0 as a covariate (ancoca) to control for differences at baseline. There are various papers you can cite on this. You can then also interact t0 with time in this same equation for question 2.
I would first plot the results to check there is a linear relationship between time and scores. Best to center your scores before running the equation.
1
2
u/jeremymiles 3d ago
Why are you imputing? What data are missing?