LME.R 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Load required packages
  2. library(lme4)
  3. library(emmeans)
  4. library(dplyr)
  5. library(lmerTest)
  6. # Get the current directory of the script (or use getwd() for the working directory)
  7. code_dir <- dirname(rstudioapi::getSourceEditorContext()$path)
  8. # Construct the path to the CSV file
  9. tablePath <- file.path(code_dir, "..", "output", "Quantitative_outputs", "Quantitative_results_from_dwi_processing.csv")
  10. # Read the CSV file
  11. data <- read.csv(tablePath)
  12. # Ensure the relevant variables are treated as factors
  13. data$merged_timepoint <- as.factor(data$merged_timepoint) # Time points
  14. data$Group <- as.factor(data$Group) # Stroke vs Control
  15. data$mask_name <- as.factor(data$mask_name) # Brain regions
  16. data$subjectID <- as.factor(data$subjectID) # Mouse ID
  17. data$dialation_amount <- as.factor(data$dialation_amount) # Ensure dialation_amount is a factor
  18. # Filter the data for FA type only
  19. fa_data <- subset(data, Qtype == "fa")
  20. # Check how many rows are left after filtering
  21. cat("Number of rows after filtering for FA data:", nrow(fa_data), "\n")
  22. # Check for missing data in the relevant columns
  23. missing_data <- colSums(is.na(fa_data[c("Value", "merged_timepoint", "Group", "mask_name", "dialation_amount", "subjectID")]))
  24. cat("Missing values in each relevant column:\n")
  25. print(missing_data)
  26. # If there are missing values, remove rows with missing data
  27. fa_data_clean <- na.omit(fa_data[c("Value", "merged_timepoint", "Group", "mask_name", "dialation_amount", "subjectID")])
  28. # Check how many rows remain after removing missing data
  29. cat("Number of rows after removing missing values:", nrow(fa_data_clean), "\n")
  30. # Check the levels of merged_timepoint to ensure reference is correct
  31. print(levels(fa_data_clean$merged_timepoint))
  32. # Optionally, set the reference level for merged_timepoint if needed
  33. fa_data_clean$merged_timepoint <- relevel(fa_data_clean$merged_timepoint, ref = "0")
  34. # Optionally, set the reference level for Group if needed
  35. fa_data_clean$Group <- relevel(fa_data_clean$Group, ref = "Sham")
  36. # Fit the linear mixed-effects model using the cleaned dataset
  37. # Simplify the random effects structure to reduce boundary singular fit issues
  38. fa_model <- lmer(Value ~ merged_timepoint * Group + mask_name + dialation_amount +
  39. (1 | subjectID) + (1 | mask_name),
  40. data = fa_data_clean)
  41. # Summarize the model
  42. cat("Model Summary:\n")
  43. summary(fa_model)
  44. # Set emm_options to allow degrees of freedom calculations for large datasets and increase rg.limit to avoid issues with reference grid
  45. emm_options(pbkrtest.limit = 342163, lmerTest.limit = 342163, rg.limit = 12000)
  46. # Post-hoc comparisons for significant differences between Stroke and Control within each time point for each mask and dilation amount
  47. posthoc_group <- emmeans(fa_model, pairwise ~ Group | merged_timepoint * mask_name * dialation_amount)
  48. # Post-hoc comparisons for significant differences between time points within each group, mask, and dilation amount
  49. posthoc_time <- emmeans(fa_model, pairwise ~ merged_timepoint | Group * mask_name * dialation_amount)
  50. # Construct the path to the output text file (same location as CSV file)
  51. output_file <- file.path(dirname(tablePath), "posthoc_results_FA_model2.txt")
  52. # Open a connection to the file
  53. sink(output_file)
  54. # Display the results of the pairwise comparisons
  55. # Question 1: Are there significant differences between Stroke and Control within each time point?
  56. cat("Post-hoc comparison: Stroke vs Control within each time point, mask, and dilation amount\n")
  57. print(summary(posthoc_group))
  58. # Question 2: Are there significant differences between time points within each group?
  59. cat("\nPost-hoc comparison: Time point differences within each group, mask, and dilation amount\n")
  60. print(summary(posthoc_time))
  61. # Print pairwise results for Stroke vs Control within each time point, mask, and dilation amount
  62. cat("\nPairwise results for Stroke vs Control within each time point, mask, and dilation amount:\n")
  63. print(posthoc_group$contrasts)
  64. # Print pairwise results for time point differences within each group, mask, and dilation amount
  65. cat("\nPairwise results for time point differences within each group, mask, and dilation amount:\n")
  66. print(posthoc_time$contrasts)
  67. # Close the connection to the file
  68. sink()
  69. # Notify user that the results have been saved
  70. cat("Results have been saved to", output_file, "\n")