##Installing packages##

install.packages("tidyr")
library("tidyr")
install.packages("dplyr")
library("dplyr")
install.packages("magrittr")
library("magrittr")
install.packages("outliers")
library("outliers")
install.packages("EnvStats")
library("EnvStats")
install.packages("mvnormtest")
library("mvnormtest")
install.packages("ggplot2")
library("ggplot2")
install.packages("nlme")
library(nlme)
install.packages("mgcv")
library("mgcv")
install.packages("ROCR")
library("ROCR")
install.packages("pROC")
library("pROC")
install.packages("ROCit")
library("ROCit")
install.packages("OptimalCutpoints")
library("OptimalCutpoints")
install.packages("lme4")
library(lme4)

##Importing data##

Heifer<-read.csv("C:/Users/18101200/OneDrive - Harper Adams University/PhD/Determining copper toxicity level/McCaughern et al 2024/McCaughern et al. 2024.csv")
View(Heifer)

##Testing distribution of data##

hist(Heifer$"Liver.copper..mg.kg.DM..12.4.months.of.age")
shapiro.test(Heifer$"Liver.copper..mg.kg.DM..12.4.months.of.age")
hist(Heifer$"GLDH..U.L.12.4.months.of.age")
shapiro.test(Heifer$"GLDH..U.L.12.4.months.of.age")
hist(Heifer$"Weight.Week.32")
shapiro.test(Heifer$"Weight.Week.32")
hist(Heifer$"Plasma.Cu.Week.32..mg.dl.")
shapiro.test(Heifer$"Plasma.Cu.Week.32..mg.dl.")
hist(Heifer$"Days.to.first.estrus")
shapiro.test(Heifer$"Days.to.first.estrus")

##Calculating means and standard deviation of data##


mean(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age, na.rm=TRUE)
sd(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age, na.rm=TRUE)
quantile(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age, 0.25, na.rm=TRUE)
quantile(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age, 0.75, na.rm=TRUE)
min(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age, na.rm=TRUE)
max(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age, na.rm=TRUE)    

mean(Heifer$Days.to.first.estrus, na.rm=TRUE)
sd(Heifer$Days.to.first.estrus, na.rm=TRUE)
quantile(Heifer$Days.to.first.estrus, 0.25, na.rm=TRUE)
quantile(Heifer$Days.to.first.estrus, 0.75, na.rm=TRUE)
max(Heifer$Days.to.first.estrus, na.rm=TRUE)
min(Heifer$Days.to.first.estrus, na.rm=TRUE)

mean(Heifer$GLDH..U.L.12.4.months.of.age, na.rm=TRUE)
sd(Heifer$GLDH..U.L.12.4.months.of.age, na.rm=TRUE)
quantile(Heifer$GLDH..U.L.12.4.months.of.age, 0.25, na.rm=TRUE)
quantile(Heifer$GLDH..U.L.12.4.months.of.age, 0.75, na.rm=TRUE)
max(Heifer$GLDH..U.L.12.4.months.of.age, na.rm=TRUE)
min(Heifer$GLDH..U.L.12.4.months.of.age, na.rm=TRUE)

mean(Heifer$Plasma.Cu.Week.32..mg.dl., na.rm=TRUE)
sd(Heifer$Plasma.Cu.Week.32..mg.dl., na.rm=TRUE)
quantile(Heifer$Plasma.Cu.Week.32..mg.dl., 0.25, na.rm=TRUE)
quantile(Heifer$Plasma.Cu.Week.32..mg.dl., 0.75, na.rm=TRUE)
max(Heifer$Plasma.Cu.Week.32..mg.dl., na.rm=TRUE)
min(Heifer$Plasma.Cu.Week.32..mg.dl., na.rm=TRUE)

mean(Heifer$Weight.Week.32, na.rm=TRUE)
sd(Heifer$Weight.Week.32, na.rm=TRUE)
quantile(Heifer$Weight.Week.32, 0.25, na.rm=TRUE)
quantile(Heifer$Weight.Week.32, 0.75, na.rm=TRUE)
max(Heifer$Weight.Week.32, na.rm=TRUE)
min(Heifer$Weight.Week.32, na.rm=TRUE)

mean(Heifer$Week.32.BCS, na.rm=TRUE)
sd(Heifer$Week.32.BCS, na.rm=TRUE)
quantile(Heifer$Week.32.BCS, 0.25, na.rm=TRUE)
quantile(Heifer$Week.32.BCS, 0.75, na.rm=TRUE)
max(Heifer$Week.32.BCS, na.rm=TRUE)
min(Heifer$Week.32.BCS, na.rm=TRUE)


##Determining a relationship between GLDH and Cu##

HeiferGLDH<-data.frame(Copper=c(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age),Allocation=c(Heifer$Allocation),GLDH=c(Heifer$GLDH..U.L.12.4.months.of.age))
HeiferGLDH<-na.omit(HeiferGLDH)
HeiferGLDH$Copper<-as.numeric(HeiferGLDH$Copper)
HeiferGLDH$Allocation<-as.numeric(HeiferGLDH$Allocation)

GLDHCUGAM <- gam(Copper ~ s(GLDH) + s(Allocation, bs = "re"), data = HeiferGLDH)
summary(GLDHCUGAM)

ggplot(data = HeiferGLDH, aes(x = GLDH, y = Copper)) +
  geom_point(color = "black") +
  geom_smooth(method = "gam", se = FALSE, col = "black") +
  geom_hline(yintercept = 0, color = "black") +
  geom_vline(xintercept = 0, color = "black") +
  labs(title = "GDLH and Copper",
       x = "GDLH U/L",
       y = "Copper mg/kg of DM")
##There is a significant correlation between hepatic Cu and GLDH at 12.4 months 

##Is GLDH affected by weight or BCS

HeiferGLDHWeight<-data.frame(Copper12.4=c(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age), Allocation=c(Heifer$Allocation),GLDH12.4=c(Heifer$GLDH..U.L.12.4.months.of.age), BCS32=c(Heifer$Week.32.BCS), weight32=c(Heifer$Weight.Week.32))
HeiferGLDHWeight<-na.omit(HeiferGLDHWeight)

GLDH12.4GAM <- gam(GLDH12.4 ~ s(Copper12.4) + s(BCS32, bs = "re") + s(weight32) + s(Allocation, bs = "re"), data = HeiferGLDHWeight)
summary(GLDH12.4GAM)
##GLDH activity is not affected by weight or BCS

##Using GLDH to determine Cu threshold at 12.4 months

HeiferGLDH12.4<-data.frame(Copper=c(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age),Allocation=c(Heifer$Allocation),GLDH=c(Heifer$GLDH..U.L.12.4.months.of.age))
HeiferGLDH12.4<-na.omit(HeiferGLDH12.4)
HeiferGLDH12.4$Allocation<-as.numeric(HeiferGLDH12.4$Allocation)
GLDH<-16
HeiferGLDH12.4$ROCGLDH <- ifelse(HeiferGLDH12.4$GLDH> GLDH, 1, 0)
ROCHeiferGLDH12.4<- rocit(score=HeiferGLDH12.4$Copper,class=HeiferGLDH12.4$ROCGLDH)
plot(ROCHeiferGLDH12.4)
HeiferGLDH12.4OptCut<-optimal.cutpoints(X="Copper",status ="ROCGLDH",data=HeiferGLDH12.4, tag.healthy= 0,methods = "Youden", conf.level = 0.95)
print(HeiferGLDH12.4OptCut)
##Level at which liver breakdown as indicated by GLDH occurs = 322.14 mg/kg of DM

##Determining a relationship between Live weight and Cu at all weeks##

HeiferWgt<-data.frame(Allocation=c(Heifer$Allocation),Copper=c(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age),Wk32Wgt=c(Heifer$Weight.Week.32))

WeightCUGAM<- gam(Copper ~ s(Wk32Wgt), data = HeiferWgt)
summary(WeightCUGAM)
## Only 8.54% of deviance can be related to Cu therefore not high enough to run receiver operator curves on.

##Determining a relationship between Plasma Cu and Cu at all weeks##

HeiferPlasma<-data.frame(Allocation=c(Heifer$Allocation),Copper=c(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age),Wk32Plasma=c(Heifer$Plasma.Cu.Week.32..mg.dl.))

PlasmaCUGAM<- gam(Copper ~ s(Wk32Plasma),data = HeiferPlasma)
summary(PlasmaCUGAM)

ggplot(data = HeiferPlasma, aes(x =Wk32Plasma, y= Copper)) +
  geom_point(color = "black") +
  geom_smooth(method = "gam", se = FALSE, col = "black") +
  geom_hline(yintercept = 0, color = "black") +
  geom_vline(xintercept = 0, color = "black") +
  labs(title = "Plasma Copper and hepatic Copper",
       x = "Plasma Copper mg/dl",
       y = "Hepatic Copper mg/kg of DM")

##Liver Cu levels can account for 48% of the differences seen in plasma at week 40 on-wards##
##Not sufficient numbers of animals which are considered diseased to run ROC curves.##

##Fertility variables 

HeiferFertility<-data.frame(Allocation=c(Heifer$Allocation),Copper=c(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age),FirstS=c(Heifer$X1st.conception),SecondS=c(Heifer$X1st.and.2nd.conception),Days2Oestrus=c(Heifer$Days.to.first.estrus))

Days2OCUGAM<- gam(Copper ~ s(Days2Oestrus),data = HeiferFertility)
summary(Days2OCUGAM)

HeiferFertility$GAMFirstS <- ifelse(HeiferFertility$FirstS == "Yes", 1, 0)
GAMFirstS <- glm(GAMFirstS ~ Copper + Allocation, family = binomial, data = HeiferFertility)
summary(GAMFirstS)
null_model <- glm(GAMFirstS ~ 1, family = binomial, data = HeiferFertility)
logLik_full <- logLik(GAMFirstS)
logLik_null <- logLik(null_model)
r2_mcfadden <- 1 - (logLik_full / logLik_null)
print(r2_mcfadden)

HeiferFertility$GAMSecondS <- ifelse(HeiferFertility$SecondS == "Yes", 1, 0)
GAMSecondS<- glm(GAMSecondS ~ Copper + Allocation, family = binomial, data = HeiferFertility)
summary(GAMSecondS)
null_model <- glm(GAMSecondS ~ 1, family = binomial, data = HeiferFertility)
logLik_full <- logLik(GAMSecondS)
logLik_null <- logLik(null_model)
r2_mcfadden <- 1 - (logLik_full / logLik_null)
print(r2_mcfadden)

##Hepatic Cu levels have a statistically significant impact on conception rate to first service and first and second service but not days to first oestrus.

FirstSMean<-data.frame(FirstS=c("Yes","No"), MeanCu= c(mean(HeiferFertility[HeiferFertility$FirstS == 'Yes', 'Copper'], na.rm = TRUE), mean(HeiferFertility[HeiferFertility$FirstS == 'No', 'Copper'], na.rm = TRUE)), SDCu= c(sd(HeiferFertility[HeiferFertility$FirstS == 'Yes', 'Copper'], na.rm = TRUE), sd(HeiferFertility[HeiferFertility$FirstS == 'No', 'Copper'], na.rm = TRUE)))
ggplot(FirstSMean, aes(x = FirstS, y = MeanCu)) +
  geom_bar(stat = "identity", fill = "grey", color = "black", width = 0.3)  + geom_errorbar(aes(ymin = MeanCu - SDCu, ymax = MeanCu + SDCu), width = 0.2, color = "black")+
  scale_x_discrete(labels = c("Not in-calf", "In-calf")) +
  labs(x = "First Service", y = "Mean Copper", fill = "Response") +
  theme_minimal()

ggplot(HeiferFertility, aes(x = FirstS, y = Copper)) +
  geom_violin(fill = "lightgrey", color = "grey") +
  geom_jitter(width = 0.1, alpha = 0.5, color = "black") +
  scale_x_discrete(labels = c("Not in-calf", "In-calf")) +
  labs(x = "First Service", y = "Copper Levels mg/kg of DM", fill = "Response") +
  theme_minimal()

SecondSMean<-data.frame(SecondS=c("Yes","No"), MeanCu= c(mean(HeiferFertility[HeiferFertility$SecondS == 'Yes', 'Copper'], na.rm = TRUE), mean(HeiferFertility[HeiferFertility$SecondS == 'No', 'Copper'], na.rm = TRUE)), SDCu= c(sd(HeiferFertility[HeiferFertility$SecondS == 'Yes', 'Copper'], na.rm = TRUE), sd(HeiferFertility[HeiferFertility$SecondS == 'No', 'Copper'], na.rm = TRUE)))
ggplot(SecondSMean, aes(x = SecondS, y = MeanCu)) +
  geom_bar(stat = "identity", fill = "grey", color = "black", width = 0.3)  + geom_errorbar(aes(ymin = MeanCu - SDCu, ymax = MeanCu + SDCu), width = 0.2, color = "black")+
  scale_x_discrete(labels = c("Not in-calf", "In-calf")) +
  labs(x = "First and second Service", y = "Mean Copper", fill = "Response") +
  theme_minimal()

ggplot(HeiferFertility, aes(x = SecondS, y = Copper)) +
  geom_violin(fill = "lightgrey", color = "grey") +
  geom_jitter(width = 0.1, alpha = 0.5, color = "black") +
  scale_x_discrete(labels = c("Not in-calf", "In-calf")) +
  labs(x = "First and second Service", y = "Copper Levels mg/kg of DM", fill = "Response") +
  theme_minimal()

ROCHeiferFirstS<- rocit(score=HeiferFertility$Copper,class=HeiferFertility$FirstS)
plot(ROCHeiferFirstS)
HeiferFirstSOptCut<-optimal.cutpoints(X="Copper",status ="FirstS",data=HeiferFertility, tag.healthy= "Yes" ,methods = "Youden", conf.level = 0.95)
print(HeiferFirstSOptCut)

ROCHeiferFirstSecS<- rocit(score=HeiferFertility$Copper,class=HeiferFertility$SecondS)
plot(ROCHeiferFirstSecS)
HeiferFirstSecSOptCut<-optimal.cutpoints(X="Copper",status ="SecondS",data=HeiferFertility, tag.healthy= "Yes" ,methods = "Youden", conf.level = 0.95)
print(HeiferFirstSecSOptCut)

##Is conception rate affected by other variables 

HeiferFertilityVar<-data.frame(Allocation=c(Heifer$Allocation),Copper=c(Heifer$Liver.copper..mg.kg.DM..12.4.months.of.age),FirstS=c(Heifer$X1st.conception),SecondS=c(Heifer$X1st.and.2nd.conception),Days2Oestrus=c(Heifer$Days.to.first.estrus),PSMW=c(Heifer$Weight.Week.32), PSMBCS=c(Heifer$Week.32.BCS))
HeiferFertilityVar$GAMFirstS <- ifelse(HeiferFertilityVar$FirstS == "Yes", 1, 0)
HeiferFertilityVar$GAMSecondS <- ifelse(HeiferFertilityVar$SecondS == "Yes", 1, 0)

GAMFirstSVar <- gam(GAMFirstS ~ Copper + Allocation +Days2Oestrus + PSMW + PSMBCS, family = binomial, data = HeiferFertilityVar)
summary(GAMFirstSVar)
GAMSecondSVar <- gam(GAMSecondS ~ Copper + Allocation +Days2Oestrus + PSMW + PSMBCS, family = binomial, data = HeiferFertilityVar)
summary(GAMSecondSVar)

##Conception rate is not affected by days to first observed oestrus, weight at planned start of mating or body condition score at planned start of mating

