df <- read.csv("C:/Users/gibbsj1/OneDrive - College of Charleston/Jesi CofC/Thesis/Neurobiological Method/NC10Points.csv")
library(dplyr)  
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#make section unique
df$section <- interaction(df$round, df$subject, df$section, sep = "_")

#Filter out sections with only 1 hemisphere:
df <- df %>%
  group_by(subject, section) %>%
  filter(n_distinct(hemi) == 2) %>%
  ungroup()
library(ggplot2)

#Mean and Max values of 4 points method across all observations by treatment

ggplot(df, aes(x = hot, y = mean)) +
  geom_boxplot(position = position_dodge(0.8)) +
  labs(title = "Mean Intensity  Treatment",
       y = "Mean Intensity",
       x = "Treatment") +
  theme_minimal()

ggplot(df, aes(x = hot, y = max)) +
  geom_boxplot(position = position_dodge(0.8)) +
  labs(title = "Mean Intensity  Treatment",
       y = "Mean Intensity",
       x = "Treatment") +
  theme_minimal()

library(tidyr)
## Warning: package 'tidyr' was built under R version 4.3.3
#Averaging 4 points and taking the absolute value difference between each section's hemispheres

#Step 1: Average points within each hemisphere of each section
hemi_means <- df %>%
  group_by(subject, section, hot, hemi) %>%
  summarise(mean = mean(mean), .groups = "drop")

# Step 2: Pivot to wide format to get a and b side-by-side
hemi_pairs <- hemi_means %>%
  pivot_wider(names_from = hemi, values_from = mean)

# Step 3: Calculate absolute asymmetry
asymmetry_df <- hemi_pairs %>%
  mutate(asymmetry = abs(a - b)) %>%
  filter(!is.na(a) & !is.na(b))  # Ensures both hemispheres are present

# View the result
print(asymmetry_df)
## # A tibble: 25 × 6
##    subject section hot       a     b asymmetry
##    <chr>   <fct>   <chr> <dbl> <dbl>     <dbl>
##  1 CC      1_CC_4  n     125.  119.       6.18
##  2 CC      1_CC_5  n      61.0  49.0     12.1 
##  3 EE      1_EE_2  y     111.  103.       7.44
##  4 EE      2_EE_3  y     139.  105.      34.7 
##  5 HHH     1_HHH_4 y      82.4 111.      29.0 
##  6 JJJ     2_JJJ_2 y      67.6  92.2     24.7 
##  7 LLL     2_LLL_2 n      52.9  56.5      3.55
##  8 NNN     1_NNN_3 n      71.2  75.5      4.39
##  9 NNN     1_NNN_4 n     110.   73.6     36.4 
## 10 NNN     2_NNN_4 n      61.1  86.0     24.8 
## # ℹ 15 more rows
ggplot(asymmetry_df, aes(x = hot, y = asymmetry)) +
  geom_boxplot() +
  labs(title = "Hemisphere Asymmetry via Average of Points",
       y = "Absolute Difference (|a - b|) Mean Intensity",
       x = "Treatment Group") +
  theme_minimal()

#Selecting Max value of 4 points and taking the absolute value difference between each section's hemispheres

# Step 1: Get the max intensity per hemisphere within each section
hemi_max <- df %>%
  group_by(subject, section, hot, hemi) %>%
  summarise(max_intensity = max(mean), .groups = "drop")

# Step 2: Pivot to wide format to get both hemispheres side-by-side
max_pairs <- hemi_max %>%
  pivot_wider(names_from = hemi, values_from = max_intensity)

# Step 3: Calculate absolute asymmetry using max values
asymmetry_max_df <- max_pairs %>%
  mutate(asymmetry = abs(a - b)) %>%
  filter(!is.na(a) & !is.na(b))  # Keep only complete pairs
ggplot(asymmetry_max_df, aes(x = hot, y = asymmetry)) +
  geom_boxplot() +
  labs(title = "Hemisphere Asymmetry via Max Point",
       y = "Absolute Difference (|a - b|) Mean Intensity",
       x = "Treatment Group") +
  theme_minimal()

#It doesn’t seem like intensity range really differs between the groups at all. Look at average intensity across the entire ROI and percent area stained instead - kind of makes more sense anyways - in other studies, they show an increase in area stained (e.g., optical density) vs. pixel values.

#This df has absolute difference values
Moments <- read.csv("C:/Users/gibbsj1/OneDrive - College of Charleston/Jesi CofC/Thesis/Neurobiological Method/Moments.csv")

#create unique section names
Moments$section <- interaction(Moments$round, Moments$subject, Moments$section, sep = "_")
ggplot(Moments, aes(x = hot, y = meandiff)) +
  geom_boxplot() +
  labs(title = "Hemisphere Asymmetry by Treatment",
       y = "Absolute Difference (|a - b|) Mean Intensity",
       x = "Treatment Group") +
  theme_minimal()

ggplot(Moments, aes(x = hot, y = X.areadiff)) +
  geom_boxplot() +
  labs(title = "Hemisphere Asymmetry by Treatment",
       y = "Absolute Difference (|a - b|) Percent Area",
       x = "Treatment Group") +
  theme_minimal()

#Largest percievable difference here. Does not look significant. Supports hypothesis.

#This dataframe has unseparated hemispheres

MomentsU <- read.csv("C:/Users/gibbsj1/OneDrive - College of Charleston/Jesi CofC/Thesis/Neurobiological Method/MomentsUnseparated.csv")

#create unique section names
MomentsU$section <- interaction(MomentsU$round, MomentsU$subject, MomentsU$section, sep = "_")
ggplot(MomentsU, aes(x = hot, y = mean)) +
  geom_boxplot() +
  labs(title = "Mean Intesity by Treatment",
       y = "Mean Intensity",
       x = "Treatment Group") +
  theme_minimal()
## Warning: Removed 7 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

ggplot(MomentsU, aes(x = hot, y = X.area)) +
  geom_boxplot() +
  labs(title = "Percent Area by Treatment",
       y = "Percent Area",
       x = "Treatment Group") +
  theme_minimal()
## Warning: Removed 7 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

#ignore error here

#This df has absolute difference values for the local autothresholding technique

LocalAuto <- read.csv("C:/Users/gibbsj1/OneDrive - College of Charleston/Jesi CofC/Thesis/Neurobiological Method/LocalAuto.csv")

#create unique section names
LocalAuto$section <- interaction(LocalAuto$round, LocalAuto$subject, LocalAuto$section, sep = "_")
ggplot(LocalAuto, aes(x = hot, y = meandiff)) +
  geom_boxplot() +
  labs(title = "Hemisphere Asymmetry by Treatment",
       y = "Absolute Difference (|a - b|) Mean Intensity",
       x = "Treatment Group") +
  theme_minimal()

ggplot(LocalAuto, aes(x = hot, y = X.areadiff)) +
  geom_boxplot() +
  labs(title = "Hemisphere Asymmetry by Treatment",
       y = "Absolute Difference (|a - b|) percent Area",
       x = "Treatment Group") +
  theme_minimal()

#this df has unseparated hemispheres

LocalAutoU <- read.csv("C:/Users/gibbsj1/OneDrive - College of Charleston/Jesi CofC/Thesis/Neurobiological Method/LocalUnseparated.csv")

#create unique section names 
LocalAutoU$section <- interaction(LocalAutoU$round, LocalAutoU$subject, LocalAutoU$section, sep = "_")
ggplot(LocalAutoU, aes(x = hot, y = mean)) +
  geom_boxplot() +
  labs(title = "Mean Intesity by Treatment",
       y = "Mean Intensity",
       x = "Treatment Group") +
  theme_minimal()

ggplot(LocalAutoU, aes(x = hot, y = X.area)) +
  geom_boxplot() +
  labs(title = "Percent Area by Treatment",
       y = "Percent Area",
       x = "Treatment Group") +
  theme_minimal()

#Overall, a trend supporting the hypothesis, particularly with percent area stained.