Packages

Dataframes

base<-read.csv('/Users/pucc03/OneDrive - The Ohio State University Wexner Medical Center/FournierLab/Datafiles/Constellation/Extracted_5_prek_2025_9_24.csv')
week<-read.csv('/Users/pucc03/OneDrive - The Ohio State University Wexner Medical Center/FournierLab/Datafiles/Constellation/Extracted_1w_5_prek_2025_9_24.csv')
month<-read.csv('/Users/pucc03/OneDrive - The Ohio State University Wexner Medical Center/FournierLab/Datafiles/Constellation/Extracted_1m_5_prek_2025_9_24.csv')
keta<-read.csv('/Users/pucc03/OneDrive - The Ohio State University Wexner Medical Center/FournierLab/Datafiles/Constellation/Extracted_24hr_5_prek_2025_9_24.csv')

Formatting function

formatextracted <- function(extracted_data) {
  
clustdat<-extracted_data 
#Pull subid from the filepath string
#digits after the last slash and before "_con_"
clustdat$sub <- str_extract(clustdat$Conss, "(?<=/)[0-9]+(?=_con_)")
clustdat$sub <- substr(clustdat$sub,1,4)

# Extract con number (digits after "_con_")
clustdat$con <- as.integer(str_extract(clustdat$Conss, "(?<=_con_)\\d+")) 
clustdat$con2 <- factor(clustdat$con,
                     levels <- c(1,2,3,4,5,6),
                     labels <- c("Self_Pos", "Self_Neg", "Other_Pos", "Other_Neg","SelfvOther","PosvNeg"))

#assign addition within-sub/repeated measures
clustdat$con2 <- as.character(clustdat$con2)
clustdat$val <- NA
clustdat$person <- NA
for (r in 1:length(clustdat$Conss)){
  parts <- strsplit(clustdat$con2[r], "_")[[1]]
  clustdat$val[r] <- parts[2]
  clustdat$person[r] <- parts[1]
}
return(clustdat)

#roidat <- clustdat[,c(12,13,15,16,2:11)]
#roidat$sub <- as.numeric(roidat$sub)

}

Formatting fxn applied to our dfs

clustdat_base <- formatextracted(base)
clustdat_week <- formatextracted(week)
clustdat_month <- formatextracted(month)
clustdat_keta <- formatextracted(keta)

Function to Compute ICC (loop); McGraw Wong ICC C,1

compute_iccs <- function(datalist, rois, timepoints, con_levels) {
  
  tp_pairs <- combn(timepoints, 2, simplify = FALSE)
  results <- list()
  
  for (roi in rois) {
    for (con in con_levels) {
      for (pair in tp_pairs) {
        tp1 <- pair[[1]]
        tp2 <- pair[[2]]

        # Subset each timepoint df for this condition and ROI
        df1 <- datalist[[tp1]] %>%
          select(sub, con2, all_of(roi)) %>%
          filter(con2 == con) 
      
        df2 <- datalist[[tp2]] %>%
          select(sub, con2, all_of(roi)) %>%
          filter(con2 == con)
      
        
        if (nrow(df1) < 1 || nrow(df2) < 1) next
        
        
        # Merge subjects across timepoints
        merged <- inner_join(df1, df2, by = c("sub","con2"), suffix = c(".t1",".t2"))
        
        roi_cols <- merged %>% select(ends_with(".t1"), ends_with(".t2"))
        if (nrow(roi_cols) < 2) next
        
        #roi_cols_t <- t(roi_cols)

        icc_out <- tryCatch(psych::ICC(roi_cols), error = function(e) NULL)
        if (is.null(icc_out)) next
        
        # Convert ICC results to tidy df
        icc_df <- as.data.frame(icc_out$results) %>%
          mutate(type = paste0(rownames(.), "_", seq_len(nrow(.)))) %>%
          mutate(
            ROI = roi,
            Condition = con,
            Timepoint1 = tp1,
            Timepoint2 = tp2
          )
        
        results[[length(results) + 1]] <- icc_df
      }
    }
  }
  
  if (length(results) == 0) {
    message("No ICC results produced (check data).")
    return(tibble())
  }
  
  bind_rows(results) %>%
    relocate(ROI, Condition, Timepoint1, Timepoint2, type)
}

Applying function to these data

datalist <- list(
  base = clustdat_base,
  week = clustdat_week,
  month = clustdat_month,
  keta = clustdat_keta
)

rois <- colnames(clustdat_base)[2:6]   # your 5 ROIs
timepoints <- names(datalist)          # "base","week","month","keta"
con_levels <- c("Self_Pos", "Self_Neg", "Other_Pos", "Other_Neg","SelfvOther","PosvNeg")  # your 6 conditions

icc_summary <- compute_iccs(datalist, rois, timepoints, con_levels)
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
## boundary (singular) fit: see help('isSingular')
icc_summary_C1 <- subset(icc_summary,icc_summary$type=="Single_random_raters_2")

Summary Tables

icc_base_week <- icc_summary_C1 %>%
  filter(
    (Timepoint1 == "base" & Timepoint2 == "week") |
    (Timepoint1 == "week" & Timepoint2 == "base")
  )

Across all conditions/contrasts

icc_by_roi <- icc_base_week %>%
  group_by(ROI) %>%
  summarize(mean_ICC = mean(ICC, na.rm = TRUE),
            sd_ICC = sd(ICC, na.rm = TRUE),
            n = n())
icc_by_roi

Across all ROIs

icc_by_condition <- icc_base_week %>%
  group_by(Condition) %>%
  summarize(mean_ICC = mean(ICC, na.rm = TRUE),
            sd_ICC = sd(ICC, na.rm = TRUE),
            n = n())
icc_by_condition

Cross tab

icc_base_week %>%
  select(ROI, Condition, ICC) %>%
  pivot_wider(names_from = Condition, values_from = ICC)