NEWOLD <- rbind(NEW, OLD)
WM_FA <- NEWOLD %>%
    dplyr::select(contains("modid") | contains("FA") | contains("condition"))

WM_RD <- NEWOLD %>%
    dplyr::select(contains("modid") | contains("RD") | contains("condition"))

WM_AD <- NEWOLD %>%
    dplyr::select(contains("modid") | contains("AD") | contains("condition"))


View(WM_FA)

# View(AL)
FA_long <- WM_FA %>%
    tidyr::pivot_longer(cols = c(starts_with("Lt_SLF_FA"), starts_with("Lt_IFOF_FA"),
        starts_with("Lt_UNC_FA"), starts_with("CC_FA"), starts_with("Lt_SLF_RD"),
        starts_with("Lt_IFOF_RD"), starts_with("Lt_UNC_RD"),
        starts_with("CC_RD"), starts_with("Lt_SLF_AD"), starts_with("Lt_IFOF_AD"),
        starts_with("Lt_UNC_AD"), starts_with("CC_AD")), names_to = c(".value",
        "level"), names_pattern = "(.*)_(.*)")
# View(FA_long)

RD_long <- WM_RD %>%
    tidyr::pivot_longer(cols = c(starts_with("Lt_SLF_FA"), starts_with("Lt_IFOF_FA"),
        starts_with("Lt_UNC_FA"), starts_with("CC_FA"), starts_with("Lt_SLF_RD"),
        starts_with("Lt_IFOF_RD"), starts_with("Lt_UNC_RD"),
        starts_with("CC_RD"), starts_with("Lt_SLF_AD"), starts_with("Lt_IFOF_AD"),
        starts_with("Lt_UNC_AD"), starts_with("CC_AD")), names_to = c(".value",
        "level"), names_pattern = "(.*)_(.*)")


AD_long <- WM_AD %>%
    tidyr::pivot_longer(cols = c(starts_with("Lt_SLF_FA"), starts_with("Lt_IFOF_FA"),
        starts_with("Lt_UNC_FA"), starts_with("CC_FA"), starts_with("Lt_SLF_RD"),
        starts_with("Lt_IFOF_RD"), starts_with("Lt_UNC_RD"),
        starts_with("CC_RD"), starts_with("Lt_SLF_AD"), starts_with("Lt_IFOF_AD"),
        starts_with("Lt_UNC_AD"), starts_with("CC_AD")), names_to = c(".value",
        "level"), names_pattern = "(.*)_(.*)")
AD2_long <- AD_long[which(AD_long$level == "AD2"), ]
AD3_long <- AD_long[which(AD_long$level == "AD3"), ]

FA2_long <- FA_long[which(FA_long$level == "FA2"), ]
FA3_long <- FA_long[which(FA_long$level == "FA3"), ]

RD2_long <- RD_long[which(RD_long$level == "RD2"), ]
RD3_long <- RD_long[which(RD_long$level == "RD3"), ]

AD2_long$level <- NULL
AD3_long$level <- NULL
RD2_long$level <- NULL
RD3_long$level <- NULL
FA2_long$level <- NULL
FA3_long$level <- NULL


data_plot_AD2 <- pivot_longer(AD2_long, cols = -(1:2), names_to = "Variable",
    values_to = "Value")
data_plot_AD3 <- pivot_longer(AD3_long, cols = -(1:2), names_to = "Variable",
    values_to = "Value")
data_plot_RD2 <- pivot_longer(RD2_long, cols = -(1:2), names_to = "Variable",
    values_to = "Value")
data_plot_RD3 <- pivot_longer(RD3_long, cols = -(1:2), names_to = "Variable",
    values_to = "Value")
data_plot_FA2 <- pivot_longer(FA2_long, cols = -(1:2), names_to = "Variable",
    values_to = "Value")
data_plot_FA3 <- pivot_longer(FA3_long, cols = -(1:2), names_to = "Variable",
    values_to = "Value")
dataplotAD2 <- ggplot(data = data_plot_AD2, aes(x = Variable,
    y = Value, fill = condition)) + geom_violin(alpha = 5, position = position_dodge(width = 0.75),
    size = 1, color = NA) + geom_boxplot(notch = TRUE, outlier.size = -1,
    color = "black", lwd = 1, alpha = 0.2, show.legend = F) +
    geom_point(shape = 21, size = 2, position = position_jitterdodge(),
        color = "black", alpha = 0.1) + ggbeeswarm::geom_quasirandom(shape = 21,
    size = 2, dodge.width = 0.7, color = "black", alpha = 0.1,
    show.legend = F) + theme_minimal() + ylab(c("AD")) + xlab(c("")) +
    rremove("legend.title") + theme(plot.title = element_text(size = 35),
    plot.subtitle = element_text(size = 25)) + scale_y_continuous(breaks = c(seq(0,
    2.25, 0.25))) + theme(legend.position = "none") + ggtitle("AD YEAR 2")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
dataplotAD3 <- ggplot(data = data_plot_AD3, aes(x = Variable,
    y = Value, fill = condition)) + geom_violin(alpha = 5, position = position_dodge(width = 0.75),
    size = 1, color = NA) + geom_boxplot(notch = TRUE, outlier.size = -1,
    color = "black", lwd = 1, alpha = 0.2, show.legend = F) +
    geom_point(shape = 21, size = 2, position = position_jitterdodge(),
        color = "black", alpha = 0.1) + ggbeeswarm::geom_quasirandom(shape = 21,
    size = 2, dodge.width = 0.7, color = "black", alpha = 0.1,
    show.legend = F) + theme_minimal() + ylab(c("AD YEAR 3")) +
    xlab(c("")) + rremove("legend.title") + theme(plot.title = element_text(size = 35),
    plot.subtitle = element_text(size = 25)) + scale_y_continuous(breaks = c(seq(0,
    2.25, 0.25))) + ggtitle("AD YEAR 3")


dataplotRD2 <- ggplot(data = data_plot_RD2, aes(x = Variable,
    y = Value, fill = condition)) + geom_violin(alpha = 5, position = position_dodge(width = 0.75),
    size = 1, color = NA) + geom_boxplot(notch = TRUE, outlier.size = -1,
    color = "black", lwd = 1, alpha = 0.2, show.legend = F) +
    geom_point(shape = 21, size = 2, position = position_jitterdodge(),
        color = "black", alpha = 0.1) + ggbeeswarm::geom_quasirandom(shape = 21,
    size = 2, dodge.width = 0.7, color = "black", alpha = 0.1,
    show.legend = F) + theme_minimal() + ylab(c("RD Year 2")) +
    xlab(c("")) + rremove("legend.title") + theme(plot.title = element_text(size = 35),
    plot.subtitle = element_text(size = 25)) + scale_y_continuous(breaks = c(seq(0,
    2.25, 0.25))) + theme(legend.position = "none") + ggtitle("RD YEAR 2")


dataplotRD3 <- ggplot(data = data_plot_RD3, aes(x = Variable,
    y = Value, fill = condition)) + geom_violin(alpha = 5, position = position_dodge(width = 0.75),
    size = 1, color = NA) + geom_boxplot(notch = TRUE, outlier.size = -1,
    color = "black", lwd = 1, alpha = 0.2, show.legend = F) +
    geom_point(shape = 21, size = 2, position = position_jitterdodge(),
        color = "black", alpha = 0.1) + ggbeeswarm::geom_quasirandom(shape = 21,
    size = 2, dodge.width = 0.7, color = "black", alpha = 0.1,
    show.legend = F) + theme_minimal() + ylab(c("RD Year 3")) +
    xlab(c("")) + rremove("legend.title") + theme(plot.title = element_text(size = 35),
    plot.subtitle = element_text(size = 25)) + scale_y_continuous(breaks = c(seq(0,
    2.25, 0.25))) + ggtitle("RD YEAR 3")


dataplotFA2 <- ggplot(data = data_plot_FA2, aes(x = Variable,
    y = Value, fill = condition)) + geom_violin(alpha = 5, position = position_dodge(width = 0.75),
    size = 1, color = NA) + geom_boxplot(notch = TRUE, outlier.size = -1,
    color = "black", lwd = 1, alpha = 0.2, show.legend = F) +
    geom_point(shape = 21, size = 2, position = position_jitterdodge(),
        color = "black", alpha = 0.1) + ggbeeswarm::geom_quasirandom(shape = 21,
    size = 2, dodge.width = 0.7, color = "black", alpha = 0.1,
    show.legend = F) + theme_minimal() + ylab(c("FA YEAR 2")) +
    xlab(c("")) + rremove("legend.title") + theme(plot.title = element_text(size = 35),
    plot.subtitle = element_text(size = 25)) + scale_y_continuous(breaks = c(seq(0,
    2.25, 0.25))) + theme(legend.position = "none") + ggtitle("FA YEAR 2")


dataplotFA3 <- ggplot(data = data_plot_FA3, aes(x = Variable,
    y = Value, fill = condition)) + geom_violin(alpha = 5, position = position_dodge(width = 0.75),
    size = 1, color = NA) + geom_boxplot(notch = TRUE, outlier.size = -1,
    color = "black", lwd = 1, alpha = 0.2, show.legend = F) +
    geom_point(shape = 21, size = 2, position = position_jitterdodge(),
        color = "black", alpha = 0.1) + ggbeeswarm::geom_quasirandom(shape = 21,
    size = 2, dodge.width = 0.7, color = "black", alpha = 0.1,
    show.legend = F) + theme_minimal() + ylab(c("FA Year 3")) +
    xlab(c("")) + rremove("legend.title") + theme(plot.title = element_text(size = 35),
    plot.subtitle = element_text(size = 25)) + scale_y_continuous(breaks = c(seq(0,
    2.25, 0.25))) + ggtitle("FA YEAR 3")

ggarrange(dataplotAD2, dataplotAD3)
## Warning: Removed 2256 rows containing non-finite outside the scale range
## (`stat_ydensity()`).
## Warning: Removed 2256 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 2256 rows containing missing values or values outside the scale range
## (`position_quasirandom()`).
## Warning: Removed 2256 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2384 rows containing non-finite outside the scale range
## (`stat_ydensity()`).
## Warning: Removed 2384 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 2384 rows containing missing values or values outside the scale range
## (`position_quasirandom()`).
## Warning: Removed 2384 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggarrange(dataplotRD2, dataplotRD3)
## Warning: Removed 2256 rows containing non-finite outside the scale range
## (`stat_ydensity()`).
## Warning: Removed 2256 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 2256 rows containing missing values or values outside the scale range
## (`position_quasirandom()`).
## Warning: Removed 2256 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2384 rows containing non-finite outside the scale range
## (`stat_ydensity()`).
## Warning: Removed 2384 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 2384 rows containing missing values or values outside the scale range
## (`position_quasirandom()`).
## Warning: Removed 2384 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggarrange(dataplotFA2, dataplotFA3)
## Warning: Removed 2256 rows containing non-finite outside the scale range
## (`stat_ydensity()`).
## Warning: Removed 2256 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 2256 rows containing missing values or values outside the scale range
## (`position_quasirandom()`).
## Warning: Removed 2256 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2384 rows containing non-finite outside the scale range
## (`stat_ydensity()`).
## Warning: Removed 2384 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 2384 rows containing missing values or values outside the scale range
## (`position_quasirandom()`).
## Warning: Removed 2384 rows containing missing values or values outside the scale range
## (`geom_point()`).

AD2_long <- AD2_long %>%
    group_by(modid, condition) %>%
    mutate(CCDUPL = ifelse(duplicated(CC) | duplicated(CC, fromLast = TRUE),
        1, 0))


AD2_long <- AD2_long %>%
    group_by(modid, condition) %>%
    mutate(SLFDUPL = ifelse(duplicated(Lt_SLF) | duplicated(Lt_SLF,
        fromLast = TRUE), 1, 0))

AD2_long <- AD2_long %>%
    group_by(modid, condition) %>%
    mutate(UNCDUPL = ifelse(duplicated(Lt_UNC) | duplicated(Lt_UNC,
        fromLast = TRUE), 1, 0))


AD2_long <- AD2_long %>%
    group_by(modid, condition) %>%
    mutate(IFOFDUPL = ifelse(duplicated(Lt_IFOF) | duplicated(Lt_IFOF,
        fromLast = TRUE), 1, 0))

View(AD2_long)

# Check if any column contains 0
condition <- apply(AD2_long[, -1], 1, function(x) any(x == 0))

# Select subject keys where any column contains 0
subject_keys_with_zero <- AD2_long$modid[condition]
if (length(subject_keys_with_zero) == 0) {
    subject_keys_with_zero <- NA
}
table(subject_keys_with_zero)
## subject_keys_with_zero
## MOD1022 MOD1481 MOD1621 
##       2       2       2
colnames(AD2_long) <- paste(colnames(AD2_long), "AD2", sep = "_")

AD2_long$modid <- AD2_long$modid_AD2
AD2_long$modid_AD2 <- NULL
AD2_long$condition <- AD2_long$condition_AD2
AD2_long$condition_AD2 <- NULL
RD2_long <- RD2_long %>%
    group_by(modid, condition) %>%
    mutate(CCDUPL = ifelse(duplicated(CC) | duplicated(CC, fromLast = TRUE),
        1, 0))


RD2_long <- RD2_long %>%
    group_by(modid, condition) %>%
    mutate(SLFDUPL = ifelse(duplicated(Lt_SLF) | duplicated(Lt_SLF,
        fromLast = TRUE), 1, 0))

RD2_long <- RD2_long %>%
    group_by(modid, condition) %>%
    mutate(UNCDUPL = ifelse(duplicated(Lt_UNC) | duplicated(Lt_UNC,
        fromLast = TRUE), 1, 0))


RD2_long <- RD2_long %>%
    group_by(modid, condition) %>%
    mutate(IFOFDUPL = ifelse(duplicated(Lt_IFOF) | duplicated(Lt_IFOF,
        fromLast = TRUE), 1, 0))

View(RD2_long)

# Check if any column contains 0
condition <- apply(RD2_long[, -1], 1, function(x) any(x == 0))

# Select subject keys where any column contains 0
subject_keys_with_zero <- RD2_long$modid[condition]
if (length(subject_keys_with_zero) == 0) {
    subject_keys_with_zero <- NA
}
table(subject_keys_with_zero)
## subject_keys_with_zero
## MOD1022 MOD1481 MOD1621 
##       2       2       2
colnames(RD2_long) <- paste(colnames(RD2_long), "RD2", sep = "_")

RD2_long$modid <- RD2_long$modid_RD2
RD2_long$modid_RD2 <- NULL
RD2_long$condition <- RD2_long$condition_RD2
RD2_long$condition_RD2 <- NULL
FA2_long <- FA2_long %>%
    group_by(modid, condition) %>%
    mutate(CCDUPL = ifelse(duplicated(CC) | duplicated(CC, fromLast = TRUE),
        1, 0))


FA2_long <- FA2_long %>%
    group_by(modid, condition) %>%
    mutate(SLFDUPL = ifelse(duplicated(Lt_SLF) | duplicated(Lt_SLF,
        fromLast = TRUE), 1, 0))

FA2_long <- FA2_long %>%
    group_by(modid, condition) %>%
    mutate(UNCDUPL = ifelse(duplicated(Lt_UNC) | duplicated(Lt_UNC,
        fromLast = TRUE), 1, 0))


FA2_long <- FA2_long %>%
    group_by(modid, condition) %>%
    mutate(IFOFDUPL = ifelse(duplicated(Lt_IFOF) | duplicated(Lt_IFOF,
        fromLast = TRUE), 1, 0))

View(FA2_long)

# Check if any column contains 0
condition <- apply(FA2_long[, -1], 1, function(x) any(x == 0))

# Select subject keys where any column contains 0
subject_keys_with_zero <- FA2_long$modid[condition]
if (length(subject_keys_with_zero) == 0) {
    subject_keys_with_zero <- NA
}
table(subject_keys_with_zero)
## subject_keys_with_zero
## MOD1022 MOD1481 MOD1621 
##       2       2       2
colnames(FA2_long) <- paste(colnames(FA2_long), "FA2", sep = "_")
FA2_long$modid <- FA2_long$modid_FA2
FA2_long$modid_FA2 <- NULL
FA2_long$condition <- FA2_long$condition_FA2
FA2_long$condition_FA2 <- NULL
AD3_long <- AD3_long %>%
    group_by(modid, condition) %>%
    mutate(CCDUPL = ifelse(duplicated(CC) | duplicated(CC, fromLast = TRUE),
        1, 0))



AD3_long <- AD3_long %>%
    group_by(modid, condition) %>%
    mutate(SLFDUPL = ifelse(duplicated(Lt_SLF) | duplicated(Lt_SLF,
        fromLast = TRUE), 1, 0))

AD3_long <- AD3_long %>%
    group_by(modid, condition) %>%
    mutate(UNCDUPL = ifelse(duplicated(Lt_UNC) | duplicated(Lt_UNC,
        fromLast = TRUE), 1, 0))


AD3_long <- AD3_long %>%
    group_by(modid, condition) %>%
    mutate(IFOFDUPL = ifelse(duplicated(Lt_IFOF) | duplicated(Lt_IFOF,
        fromLast = TRUE), 1, 0))

View(AD3_long)
colnames(AD3_long) <- paste(colnames(AD3_long), "AD3", sep = "_")
AD3_long$modid <- AD3_long$modid_AD3
AD3_long$condition <- AD3_long$condition_AD3
AD3_long$modid_AD3 <- NULL
AD3_long$condition_AD3 <- NULL
RD3_long <- RD3_long %>%
    group_by(modid, condition) %>%
    mutate(CCDUPL = ifelse(duplicated(CC) | duplicated(CC, fromLast = TRUE),
        1, 0))


RD3_long <- RD3_long %>%
    group_by(modid, condition) %>%
    mutate(SLFDUPL = ifelse(duplicated(Lt_SLF) | duplicated(Lt_SLF,
        fromLast = TRUE), 1, 0))

RD3_long <- RD3_long %>%
    group_by(modid, condition) %>%
    mutate(UNCDUPL = ifelse(duplicated(Lt_UNC) | duplicated(Lt_UNC,
        fromLast = TRUE), 1, 0))


RD3_long <- RD3_long %>%
    group_by(modid, condition) %>%
    mutate(IFOFDUPL = ifelse(duplicated(Lt_IFOF) | duplicated(Lt_IFOF,
        fromLast = TRUE), 1, 0))

View(RD3_long)

colnames(RD3_long) <- paste(colnames(RD3_long), "RD3", sep = "_")

RD3_long$modid <- RD3_long$modid_RD3
RD3_long$condition <- RD3_long$condition_RD3
RD3_long$condition_RD3 <- NULL
RD3_long$modid_RD3 <- NULL
FA3_long <- FA3_long %>%
    group_by(modid, condition) %>%
    mutate(CCDUPL = ifelse(duplicated(CC) | duplicated(CC, fromLast = TRUE),
        1, 0))


FA3_long <- FA3_long %>%
    group_by(modid, condition) %>%
    mutate(SLFDUPL = ifelse(duplicated(Lt_SLF) | duplicated(Lt_SLF,
        fromLast = TRUE), 1, 0))

FA3_long <- FA3_long %>%
    group_by(modid, condition) %>%
    mutate(UNCDUPL = ifelse(duplicated(Lt_UNC) | duplicated(Lt_UNC,
        fromLast = TRUE), 1, 0))


FA3_long <- FA3_long %>%
    group_by(modid, condition) %>%
    mutate(IFOFDUPL = ifelse(duplicated(Lt_IFOF) | duplicated(Lt_IFOF,
        fromLast = TRUE), 1, 0))


colnames(FA3_long) <- paste(colnames(FA3_long), "FA3", sep = "_")

FA3_long$modid <- FA3_long$modid_FA3
FA3_long$condition <- FA3_long$condition_FA3
FA3_long$modid_FA3 <- NULL
FA3_long$condition_FA3 <- NULL
FA <- merge(FA3_long, FA2_long, by = c("modid", "condition"))
RD <- merge(RD3_long, RD2_long, by = c("modid", "condition"))
AD <- merge(AD3_long, AD2_long, by = c("modid", "condition"))


write.csv(FA, "FAcomp.csv")
write.csv(RD, "RDcomp.csv")
write.csv(AD, "ADcomp.csv")