library(ggpubr)
library(tidyverse)
library (psych)
library(dplyr)

getwd()
options(scipen = 999) ###!!!### Disable scientific notation in outputs

Donnees = read.csv("Data.csv",header = T, sep = ";", stringsAsFactors = F)

Study <- Donnees[,1]
Training_set <- Donnees[,2]
Type <- Donnees[,3]
Ns <- Donnees[,4]
LNs <- Donnees[,5]
NB_Markers <- Donnees[,6]
LNB_Markers <- Donnees[,7]
h2.ped. <- Donnees[,8]
h2.mark. <- Donnees[,9]
Plant <- Donnees[10]
PA_ABLUP <- Donnees[,11]
PA_GBLUP <- Donnees[,12]
PACC_ABLUP <- Donnees[,11]
PACC_GBLUP <- Donnees[,14]
Structure <- Donnees[,15]
Gsize <- Donnees[,16]
Mlength <- Donnees[,17]
NB_cM <- Donnees[,18]
LNB_cM <- Donnees[,19]

Donnees$Plant <- as.factor(Donnees$Plant)
Donnees$Training_set <- as.numeric(Donnees$Training_set)
Donnees$NB_Markers <- as.numeric(Donnees$NB_Markers)
Donnees$LNB_Markers <- as.numeric(Donnees$LNB_Markers)
Donnees$Ns <- as.numeric(Donnees$Ns)
Donnees$LNs <- as.numeric(Donnees$LNs)
Donnees$h2.ped. <- as.numeric(Donnees$h2.ped.)
Donnees$h2.mark. <- as.numeric(Donnees$h2.mark.)
Donnees$PA_ABLUP <-as.numeric(Donnees$PA_ABLUP)
Donnees$PA_GBLUP <-as.numeric(Donnees$PA_GBLUP)
Donnees$PACC_ABLUP <-as.numeric(Donnees$PACC_ABLUP)
Donnees$PACC_GBLUP <-as.numeric(Donnees$PACC_GBLUP)
Donnees$Structure <- as.factor(Donnees$Structure)
Donnees$Gsize <- as.numeric(Donnees$Gsize)
Donnees$Mlength <- as.numeric(Donnees$Mlength)
Donnees$NB_cM <- as.numeric(Donnees$NB_cM)
Donnees$LNB_cM <- as.numeric(Donnees$LNB_cM)

#############################################
# Wilcox test h2.ped. vs h2.mark.
#############################################

# Data in two numeric vectors
# ++++++++++++++++++++++++++
#
ABLUP <- Donnees[,8]
GBLUP <- Donnees[,9]
paired_difference <- ABLUP - GBLUP 

res<-wilcox.test(ABLUP, GBLUP, paired = TRUE, alternative = "two.sided")
res

###################### 
#Growth traits
######################

Donnees_G <- Donnees[Donnees$Type !="W" & Donnees$Type !="O",]
# ++++++++++++++++++++++++++
ABLUP <- Donnees_G[,8]
GBLUP <- Donnees_G[,9]
paired_difference <- ABLUP - GBLUP 

res<-wilcox.test(ABLUP, GBLUP, paired = TRUE, alternative = "two.sided")
res

###################### 
#Wood traits
######################

Donnees_W <- Donnees[Donnees$Type !="W" & Donnees$Type !="O",]
# ++++++++++++++++++++++++++
ABLUP <- Donnees_W[,8]
GBLUP <- Donnees_W[,9]
paired_difference <- ABLUP - GBLUP 

res<-wilcox.test(ABLUP, GBLUP, paired = TRUE, alternative = "two.sided")
res


######################
#Other traits
######################

Donnees_O <- Donnees[Donnees$Type !="G" & Donnees$Type !="W",]
# ++++++++++++++++++++++++++
ABLUP <- Donnees_O[,8]
GBLUP <- Donnees_O[,9]
paired_difference <- ABLUP - GBLUP 

res<-wilcox.test(ABLUP, GBLUP, paired = TRUE, alternative = "two.sided")
res


#####################
#Conifers
#####################

Donnees_C <- Donnees[Donnees$Plant !="Broadleaved",]
# ++++++++++++++++++++++++++
ABLUP <- Donnees_C[,8]
GBLUP <- Donnees_C[,9]
paired_difference <- ABLUP - GBLUP 

res<-wilcox.test(ABLUP, GBLUP, paired = TRUE, alternative = "two.sided")
res

#####################
#Broadleaved
#####################

Donnees_F <- Donnees[Donnees$Plant !="Conifers",]
# ++++++++++++++++++++++++++
ABLUP <- Donnees_F[,8]
GBLUP <- Donnees_F[,9]
paired_difference <- ABLUP - GBLUP 

res<-wilcox.test(ABLUP, GBLUP, paired = TRUE, alternative = "two.sided")
res

##################### 
#Full-sibs
#####################

Donnees_FS <- Donnees[Donnees$Structure !="hs",]
# ++++++++++++++++++++++++++
ABLUP <- Donnees_FS[,8]
GBLUP <- Donnees_FS[,9]
paired_difference <- ABLUP - GBLUP 

res<-wilcox.test(ABLUP, GBLUP, paired = TRUE, alternative = "two.sided")
res

##################### 
#Half-sibs
#####################

Donnees_HS <- Donnees[Donnees$Structure !="fs",]
# ++++++++++++++++++++++++++
ABLUP <- Donnees_HS[,8]
GBLUP <- Donnees_HS[,9]
paired_difference <- ABLUP - GBLUP 

res<-wilcox.test(ABLUP, GBLUP, paired = TRUE, alternative = "two.sided")
res


########################################
# Regression LNB_cM, LNs, Training_set
# for h2.mark., PA_GBLUP, or PACC_GBLUP
########################################
res <- lm(h2.mark. ~ LNB_cM + LNs + Training_set, data = Donnees)
summary(res)
plot(res)
shapiro.test(res$residuals)

############################################
# Regression LNB_Markers, LNs, Training_set
# for h2.mark., PA_GBLUP, or PACC_GBLUP
############################################

res <- lm(PA_GBLUP ~ LNB_Markers + LNs + Training_set, data = Donnees)
summary(res)
shapiro.test(res$residuals)


###########################################
# Partial correlation coefficients
###########################################

my_data <- Donnees[, c(2, 5, 8, 9, 11, 12, 13, 14)]
#####
#PACC
#####
ABC <- partial.r(data=my_data, x=c("PACC_GBLUP","LNs"), y="Training_set")
ACB <- partial.r(data=my_data, x=c("PACC_GBLUP","Training_set"), y="LNs")
r_LNs <- ABC["PACC_GBLUP", "LNs"]
r_Training_set <- ACB["PACC_GBLUP", "Training_set"]

ABC1 <- partial.r(data=my_data, x=c("PACC_ABLUP","LNs"), y="Training_set")
ACB1 <- partial.r(data=my_data, x=c("PACC_ABLUP","Training_set"), y="LNs")
r_LNs1 <- ABC1["PACC_ABLUP", "LNs"]
r_Training_set1 <- ACB1["PACC_ABLUP", "Training_set"]

#####
#PA
#####
ABC <- partial.r(data=my_data, x=c("PA_GBLUP","LNs"), y="Training_set")
ACB <- partial.r(data=my_data, x=c("PA_GBLUP","Training_set"), y="LNs")
r_LNs <- ABC["PA_GBLUP", "LNs"]
r_Training_set <- ACB["PA_GBLUP", "Training_set"]

ABC1 <- partial.r(data=my_data, x=c("PA_ABLUP","LNs"), y="Training_set")
ACB1 <- partial.r(data=my_data, x=c("PA_ABLUP","Training_set"), y="LNs")
r_LNs1 <- ABC1["PA_ABLUP", "LNs"]
r_Training_set1 <- ACB1["PA_ABLUP", "Training_set"]

#####
#h2
#####
ABC <- partial.r(data=my_data, x=c("h2.mark.","LNs"), y="Training_set")
ACB <- partial.r(data=my_data, x=c("h2.mark.","Training_set"), y="LNs")
r_LNs <- ABC["h2.mark.", "LNs"]
r_Training_set <- ACB["h2.mark.", "Training_set"]

ABC1 <- partial.r(data=my_data, x=c("h2.ped.","LNs"), y="Training_set")
ACB1 <- partial.r(data=my_data, x=c("h2.ped.","Training_set"), y="LNs")
r_LNs1 <- ABC1["h2.ped.", "LNs"]
r_Training_set1 <- ACB1["h2.ped.", "Training_set"]

###############################
# Other package
###############################

library(tidyverse)
library(ppcor)

################################################
# PA_GBLUP or PACC_GBLUP vs LNs or Training_set
################################################

##############################################
# Graphs with partial correlation coefficients
##############################################

#####################################
# Fitting a 2 variable model
# y = PA_GBLUP, PACC_GBLUP
# x = LNs
#####################################
MISSING <- is.na(Donnees$PA_GBLUP) |
  is.na(Donnees$LNs) |
  is.na(Donnees$PACC_GBLUP) |
  is.na(Donnees$Training_set)

my_data <- subset(Donnees, subset = !MISSING)

y <- pcor.test(my_data$PACC_GBLUP, my_data$LNs, my_data[,c("Training_set")])
p_r_LNs <- y[,c("estimate")]
pv_p_r_LNs <- y[,c("p.value")]


fit_1 <- lm(PACC_GBLUP ~ LNs + Training_set, data = my_data)
summary(fit_1)

# Step 1 : Create prediction dataframes. For variable 1 we want:
# - Create a vector of minimum to maximum values for variable 1
# - Set Other variables as their mean

pred_LNs = tibble(LNs = seq(min(my_data$LNs,na.rm = T), max(my_data$LNs, na.rm = T), by = 0.1)) %>% # Sequence for variable 1
  mutate(Training_set = mean(my_data$Training_set, na.rm = T),
  ) # Creating the mean of variable 2

# Look at the head:
head(pred_LNs)
# The height is now constant, we will now predict volume values from Girth

# This is how we predict, with confidence interval:
predict(fit_1, new = pred_LNs, interval="confidence", level = 0.95)

# Adding to dataframe:
pred_LNs[,c("fit", "lwr", "upr")] =predict(fit_1, new = pred_LNs, interval="confidence", level = 0.95)


# Here would be the Plot:
# - Points are observations
# - Line is predictions

# Get info from model :
estimate_LNs = coef(summary(fit_1))["LNs", "Estimate"]
P_LNs = coef(summary(fit_1))["LNs", "Pr(>|t|)"]

options(scipen=999) # Delete scientific notation (for text on plot)

ggplot(data = pred_LNs, aes(x = LNs, y = fit)) +
  geom_line(color = "blue") + # Line of predictions
  #geom_line(aes(y = upr), linetype = "dashed") + # Upper confidence interval
  #geom_line(aes(y = lwr), linetype = "dashed") + # Lower confidence interval
  geom_ribbon(aes(x=LNs, ymax=upr, ymin=lwr), fill = "gray", alpha = 0.5) +
  geom_point(data = my_data, aes(y = PACC_GBLUP, x = LNs, color = Plant, )) + # As points, we want the observed values from the base dataframe
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  labs(x = "Log status number", y = "Marker-based predictive accuracy") +
  annotate("text", x = 1.2, y = 1.1, label =
             paste0("list(r == ", round(p_r_LNs, 2), ", italic(P) == ", round(pv_p_r_LNs, 4), ")"), parse = T) +
  theme_classic() +
  theme(legend.position = "top")


##########################################
#Training_set
##########################################

MISSING <- is.na(Donnees$PA_GBLUP) |
  is.na(Donnees$LNs) |
  is.na(Donnees$PACC_GBLUP) |
  is.na(Donnees$Training_set)

my_data <- subset(Donnees, subset = !MISSING)

y <- pcor.test(my_data$PACC_GBLUP, my_data$Training_set, my_data[,c("LNs")])
p_r_Ts <- y[,c("estimate")]
pv_p_r_Ts <- y[,c("p.value")]


#### Fitting a 2 variable model

fit_1 <- lm(PACC_GBLUP ~ Training_set + LNs, data = my_data)
summary(fit_1)

# Step 1 : Create prediction dataframes. For variable 1 we want:
# - Create a vector of minimum to maximum values for variable 1
# - Set Other variables as their mean

pred_Ts = tibble(Training_set = seq(min(my_data$Training_set,na.rm = T), max(my_data$Training_set, na.rm = T), by = 0.1)) %>% # Sequence for variable 1
  mutate(LNs = mean(my_data$LNs, na.rm = T),
         #Var3 = mean(trees$Var3, na.rm = T), # If you have 3 variables
         #Var4 = mean(trees$Var4, na.rm = T), # If you have 4 variables
  ) # Creating the mean of variable 2

# Look at the head:
head(pred_Ts)
# The height is now constant, we will now predict volume values from Girth

# This is how we predict, with confidence interval:
predict(fit_1, new = pred_Ts, interval="confidence", level = 0.95)

# Adding to dataframe:
pred_Ts[,c("fit", "lwr", "upr")] =predict(fit_1, new = pred_Ts, interval="confidence", level = 0.95)



# Here would be the Plot:
# - Points are observations
# - Line is predictions

# Get info from model :
estimate_Ts = coef(summary(fit_1))["Training_set", "Estimate"]
P_Ts = coef(summary(fit_1))["Training_set", "Pr(>|t|)"]

options(scipen=999) # Delete scientific notation (for text on plot)

ggplot(data = pred_Ts, aes(x = Training_set, y = fit)) +
  geom_line(color = "blue") + # Line of predictions
  #geom_line(aes(y = upr), linetype = "dashed") + # Upper confidence interval
  #geom_line(aes(y = lwr), linetype = "dashed") + # Lower confidence interval
  geom_ribbon(aes(x=Training_set, ymax=upr, ymin=lwr), fill = "gray", alpha = 0.5) +
  geom_point(data = my_data, aes(y = PACC_GBLUP, x = Training_set, color = Plant, )) + # As points, we want the observed values from the base dataframe
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  labs(x = "Size of the training set", y = "Marker-based predictive accuracy") +
  annotate("text", x = 500, y = 1.09, label =
             paste0("list(r == ", round(p_r_Ts, 2), ", italic(P) == ", round(pv_p_r_Ts, 4), ")"), parse = T) +
  theme_classic() +
  theme(legend.position = "top")




################################################
# PA_ABLUP or PACC_ABLUP vs LNs or Training_set
################################################

##############################################
# Graphs with partial correlation coefficients
##############################################

#####################################
# Fitting a 2 variable model
# y = PA_ABLUP, PACC_ABLUP
# x = LNs
#####################################
MISSING <- is.na(Donnees$PA_ABLUP) |
  is.na(Donnees$LNs) |
  is.na(Donnees$PACC_ABLUP) |
  is.na(Donnees$Training_set)

my_data <- subset(Donnees, subset = !MISSING)

y <- pcor.test(my_data$PACC_ABLUP, my_data$LNs, my_data[,c("Training_set")])
p_r_LNs <- y[,c("estimate")]
pv_p_r_LNs <- y[,c("p.value")]


fit_1 <- lm(PACC_ABLUP ~ LNs + Training_set, data = my_data)
summary(fit_1)

# Step 1 : Create prediction dataframes. For variable 1 we want:
# - Create a vector of minimum to maximum values for variable 1
# - Set Other variables as their mean

pred_LNs = tibble(LNs = seq(min(my_data$LNs,na.rm = T), max(my_data$LNs, na.rm = T), by = 0.1)) %>% # Sequence for variable 1
  mutate(Training_set = mean(my_data$Training_set, na.rm = T),
  ) # Creating the mean of variable 2

# Look at the head:
head(pred_LNs)
# The height is now constant, we will now predict volume values from Girth

# This is how we predict, with confidence interval:
predict(fit_1, new = pred_LNs, interval="confidence", level = 0.95)

# Adding to dataframe:
pred_LNs[,c("fit", "lwr", "upr")] =predict(fit_1, new = pred_LNs, interval="confidence", level = 0.95)


# Here would be the Plot:
# - Points are observations
# - Line is predictions

# Get info from model :
estimate_LNs = coef(summary(fit_1))["LNs", "Estimate"]
P_LNs = coef(summary(fit_1))["LNs", "Pr(>|t|)"]

options(scipen=999) # Delete scientific notation (for text on plot)

ggplot(data = pred_LNs, aes(x = LNs, y = fit)) +
  geom_line(color = "blue") + # Line of predictions
  #geom_line(aes(y = upr), linetype = "dashed") + # Upper confidence interval
  #geom_line(aes(y = lwr), linetype = "dashed") + # Lower confidence interval
  geom_ribbon(aes(x=LNs, ymax=upr, ymin=lwr), fill = "gray", alpha = 0.5) +
  geom_point(data = my_data, aes(y = PACC_ABLUP, x = LNs, color = Plant, )) + # As points, we want the observed values from the base dataframe
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  labs(x = "Log status number", y = "ABLUP pedigree-based predictive accuracy") +
  annotate("text", x = 1.5, y = 1.0, label =
             paste0("list(r == ", round(p_r_LNs, 2), ", italic(P) == ", round(pv_p_r_LNs, 4), ")"), parse = T) +
  theme_classic() +
  theme(legend.position = "top")


##########################################
#Training_set
##########################################

MISSING <- is.na(Donnees$PA_ABLUP) |
  is.na(Donnees$LNs) |
  is.na(Donnees$PACC_ABLUP) |
  is.na(Donnees$Training_set)

my_data <- subset(Donnees, subset = !MISSING)

y <- pcor.test(my_data$PACC_ABLUP, my_data$Training_set, my_data[,c("LNs")])
p_r_Ts <- y[,c("estimate")]
pv_p_r_Ts <- y[,c("p.value")]


#### Fitting a 2 variable model

fit_1 <- lm(PACC_ABLUP ~ Training_set + LNs, data = my_data)
summary(fit_1)

# Step 1 : Create prediction dataframes. For variable 1 we want:
# - Create a vector of minimum to maximum values for variable 1
# - Set Other variables as their mean

pred_Ts = tibble(Training_set = seq(min(my_data$Training_set,na.rm = T), max(my_data$Training_set, na.rm = T), by = 0.1)) %>% # Sequence for variable 1
  mutate(LNs = mean(my_data$LNs, na.rm = T),
         #Var3 = mean(trees$Var3, na.rm = T), # If you have 3 variables
         #Var4 = mean(trees$Var4, na.rm = T), # If you have 4 variables
  ) # Creating the mean of variable 2

# Look at the head:
head(pred_Ts)
# The height is now constant, we will now predict volume values from Girth

# This is how we predict, with confidence interval:
predict(fit_1, new = pred_Ts, interval="confidence", level = 0.95)

# Adding to dataframe:
pred_Ts[,c("fit", "lwr", "upr")] =predict(fit_1, new = pred_Ts, interval="confidence", level = 0.95)



# Here would be the Plot:
# - Points are observations
# - Line is predictions

# Get info from model :
estimate_Ts = coef(summary(fit_1))["Training_set", "Estimate"]
P_Ts = coef(summary(fit_1))["Training_set", "Pr(>|t|)"]

options(scipen=999) # Delete scientific notation (for text on plot)

ggplot(data = pred_Ts, aes(x = Training_set, y = fit)) +
  geom_line(color = "blue") + # Line of predictions
  #geom_line(aes(y = upr), linetype = "dashed") + # Upper confidence interval
  #geom_line(aes(y = lwr), linetype = "dashed") + # Lower confidence interval
  geom_ribbon(aes(x=Training_set, ymax=upr, ymin=lwr), fill = "gray", alpha = 0.5) +
  geom_point(data = my_data, aes(y = PACC_ABLUP, x = Training_set, color = Plant, )) + # As points, we want the observed values from the base dataframe
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  labs(x = "Size of the training set", y = "ABLUP pedigree-based predictive accuracy") +
  annotate("text", x = 500, y = 1.0, label =
             paste0("list(r == ", round(p_r_Ts, 2), ", italic(P) == ", round(pv_p_r_Ts, 4), ")"), parse = T) +
  theme_classic() +
  theme(legend.position = "top")




######################################################################

################################################
# h2.mark. vs LNs or Training_set
################################################

##############################################
# Graphs with partial correlation coefficients
##############################################

#####################################
# Fitting a 2 variable model
# y = h2.mark.
# x = LNs
#####################################

library(tidyverse)
library(ppcor)



MISSING <- is.na(Donnees$h2.mark.) |
  is.na(Donnees$LNs) |
  is.na(Donnees$Training_set)

my_data <- subset(Donnees, subset = !MISSING)

y <- pcor.test(my_data$h2.mark., my_data$LNs, my_data[,c("Training_set")])
p_r_LNs <- y[,c("estimate")]
pv_p_r_LNs <- y[,c("p.value")]


fit_1 <- lm(h2.mark. ~ LNs + Training_set, data = my_data)
summary(fit_1)

# Step 1 : Create prediction dataframes. For variable 1 we want:
# - Create a vector of minimum to maximum values for variable 1
# - Set Other variables as their mean

pred_LNs = tibble(LNs = seq(min(my_data$LNs,na.rm = T), max(my_data$LNs, na.rm = T), by = 0.1)) %>% # Sequence for variable 1
  mutate(Training_set = mean(my_data$Training_set, na.rm = T),
  ) # Creating the mean of variable 2

# Look at the head:
head(pred_LNs)
# The height is now constant, we will now predict volume values from Girth

# This is how we predict, with confidence interval:
predict(fit_1, new = pred_LNs, interval="confidence", level = 0.95)

# Adding to dataframe:
pred_LNs[,c("fit", "lwr", "upr")] =predict(fit_1, new = pred_LNs, interval="confidence", level = 0.95)


# Here would be the Plot:
# - Points are observations
# - Line is predictions

# Get info from model :
estimate_LNs = coef(summary(fit_1))["LNs", "Estimate"]
P_LNs = coef(summary(fit_1))["LNs", "Pr(>|t|)"]

options(scipen=999) # Delete scientific notation (for text on plot)

ggplot(data = pred_LNs, aes(x = LNs, y = fit)) +
  geom_line(color = "blue") + # Line of predictions
  #geom_line(aes(y = upr), linetype = "dashed") + # Upper confidence interval
  #geom_line(aes(y = lwr), linetype = "dashed") + # Lower confidence interval
  geom_ribbon(aes(x=LNs, ymax=upr, ymin=lwr), fill = "gray", alpha = 0.5) +
  geom_point(data = my_data, aes(y = h2.mark., x = LNs, color = Plant, )) + # As points, we want the observed values from the base dataframe
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  labs(x = "Log status number", y = "Marker-based heritability") +
  annotate("text", x = 1.3, y = 0.65, label =
             paste0("list(r == ", round(p_r_LNs, 2), ", italic(P) == ", round(pv_p_r_LNs, 4), ")"), parse = T) +
  theme_classic() +
  theme(legend.position = "top")


##########################################
#Training_set
##########################################

MISSING <- is.na(Donnees$h2.mark.) |
  is.na(Donnees$LNs) |
  is.na(Donnees$Training_set)

my_data <- subset(Donnees, subset = !MISSING)

y <- pcor.test(my_data$h2.mark., my_data$Training_set, my_data[,c("LNs")])
p_r_Ts <- y[,c("estimate")]
pv_p_r_Ts <- y[,c("p.value")]


#### Fitting a 2 variable model

fit_1 <- lm(h2.mark. ~ Training_set + LNs, data = my_data)
summary(fit_1)

# Step 1 : Create prediction dataframes. For variable 1 we want:
# - Create a vector of minimum to maximum values for variable 1
# - Set Other variables as their mean

pred_Ts = tibble(Training_set = seq(min(my_data$Training_set,na.rm = T), max(my_data$Training_set, na.rm = T), by = 0.1)) %>% # Sequence for variable 1
  mutate(LNs = mean(my_data$LNs, na.rm = T),
         #Var3 = mean(trees$Var3, na.rm = T), # If you have 3 variables
         #Var4 = mean(trees$Var4, na.rm = T), # If you have 4 variables
  ) # Creating the mean of variable 2

# Look at the head:
head(pred_Ts)
# The height is now constant, we will now predict volume values from Girth

# This is how we predict, with confidence interval:
predict(fit_1, new = pred_Ts, interval="confidence", level = 0.95)

# Adding to dataframe:
pred_Ts[,c("fit", "lwr", "upr")] =predict(fit_1, new = pred_Ts, interval="confidence", level = 0.95)



# Here would be the Plot:
# - Points are observations
# - Line is predictions

# Get info from model :
estimate_Ts = coef(summary(fit_1))["Training_set", "Estimate"]
P_Ts = coef(summary(fit_1))["Training_set", "Pr(>|t|)"]

options(scipen=999) # Delete scientific notation (for text on plot)

ggplot(data = pred_Ts, aes(x = Training_set, y = fit)) +
  geom_line(color = "blue") + # Line of predictions
  #geom_line(aes(y = upr), linetype = "dashed") + # Upper confidence interval
  #geom_line(aes(y = lwr), linetype = "dashed") + # Lower confidence interval
  geom_ribbon(aes(x=Training_set, ymax=upr, ymin=lwr), fill = "gray", alpha = 0.5) +
  geom_point(data = my_data, aes(y = h2.mark., x = Training_set, color = Plant, )) + # As points, we want the observed values from the base dataframe
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  labs(x = "Size of the training set", y = "Marker-based heritability") +
  annotate("text", x = 500, y = 0.7, label =
             paste0("list(r == ", round(p_r_Ts, 2), ", italic(P) == ", round(pv_p_r_Ts, 4), ")"), parse = T) +
  theme_classic() +
  theme(legend.position = "top")


################################################
# ABLUP h2 vs LNs or Training_set
################################################

##############################################
# Graphs with partial correlation coefficients
##############################################

#####################################
# Fitting a 2 variable model
# y = h2.ped.
# x = LNs
#####################################
MISSING <- is.na(Donnees$h2.ped.) |
  is.na(Donnees$LNs) |
  is.na(Donnees$Training_set)

my_data <- subset(Donnees, subset = !MISSING)

y <- pcor.test(my_data$h2.ped., my_data$LNs, my_data[,c("Training_set")])
p_r_LNs <- y[,c("estimate")]
pv_p_r_LNs <- y[,c("p.value")]


fit_1 <- lm(h2.ped. ~ LNs + Training_set, data = my_data)
summary(fit_1)

# Step 1 : Create prediction dataframes. For variable 1 we want:
# - Create a vector of minimum to maximum values for variable 1
# - Set Other variables as their mean

pred_LNs = tibble(LNs = seq(min(my_data$LNs,na.rm = T), max(my_data$LNs, na.rm = T), by = 0.1)) %>% # Sequence for variable 1
  mutate(Training_set = mean(my_data$Training_set, na.rm = T),
  ) # Creating the mean of variable 2

# Look at the head:
head(pred_LNs)
# The height is now constant, we will now predict volume values from Girth

# This is how we predict, with confidence interval:
predict(fit_1, new = pred_LNs, interval="confidence", level = 0.95)

# Adding to dataframe:
pred_LNs[,c("fit", "lwr", "upr")] =predict(fit_1, new = pred_LNs, interval="confidence", level = 0.95)


# Here would be the Plot:
# - Points are observations
# - Line is predictions

# Get info from model :
estimate_LNs = coef(summary(fit_1))["LNs", "Estimate"]
P_LNs = coef(summary(fit_1))["LNs", "Pr(>|t|)"]

options(scipen=999) # Delete scientific notation (for text on plot)

ggplot(data = pred_LNs, aes(x = LNs, y = fit)) +
  geom_line(color = "blue") + # Line of predictions
  #geom_line(aes(y = upr), linetype = "dashed") + # Upper confidence interval
  #geom_line(aes(y = lwr), linetype = "dashed") + # Lower confidence interval
  geom_ribbon(aes(x=LNs, ymax=upr, ymin=lwr), fill = "gray", alpha = 0.5) +
  geom_point(data = my_data, aes(y = h2.ped., x = LNs, color = Plant, )) + # As points, we want the observed values from the base dataframe
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  labs(x = "Log status number", y = "ABLUP pedigree-based heritability") +
  annotate("text", x = 1.2, y = 0.9, label =
             paste0("list(r == ", round(p_r_LNs, 2), ", italic(P) == ", round(pv_p_r_LNs, 4), ")"), parse = T) +
  theme_classic() +
  theme(legend.position = "top")


##########################################
#Training_set
##########################################

MISSING <- is.na(Donnees$h2.ped.) |
  is.na(Donnees$LNs) |
  is.na(Donnees$Training_set)

my_data <- subset(Donnees, subset = !MISSING)

y <- pcor.test(my_data$h2.ped., my_data$Training_set, my_data[,c("LNs")])
p_r_Ts <- y[,c("estimate")]
pv_p_r_Ts <- y[,c("p.value")]


#### Fitting a 2 variable model

fit_1 <- lm(h2.ped. ~ Training_set + LNs, data = my_data)
summary(fit_1)

# Step 1 : Create prediction dataframes. For variable 1 we want:
# - Create a vector of minimum to maximum values for variable 1
# - Set Other variables as their mean

pred_Ts = tibble(Training_set = seq(min(my_data$Training_set,na.rm = T), max(my_data$Training_set, na.rm = T), by = 0.1)) %>% # Sequence for variable 1
  mutate(LNs = mean(my_data$LNs, na.rm = T),
         #Var3 = mean(trees$Var3, na.rm = T), # If you have 3 variables
         #Var4 = mean(trees$Var4, na.rm = T), # If you have 4 variables
  ) # Creating the mean of variable 2

# Look at the head:
head(pred_Ts)
# The height is now constant, we will now predict volume values from Girth

# This is how we predict, with confidence interval:
predict(fit_1, new = pred_Ts, interval="confidence", level = 0.95)

# Adding to dataframe:
pred_Ts[,c("fit", "lwr", "upr")] =predict(fit_1, new = pred_Ts, interval="confidence", level = 0.95)



# Here would be the Plot:
# - Points are observations
# - Line is predictions

# Get info from model :
estimate_Ts = coef(summary(fit_1))["Training_set", "Estimate"]
P_Ts = coef(summary(fit_1))["Training_set", "Pr(>|t|)"]

options(scipen=999) # Delete scientific notation (for text on plot)

ggplot(data = pred_Ts, aes(x = Training_set, y = fit)) +
  geom_line(color = "blue") + # Line of predictions
  #geom_line(aes(y = upr), linetype = "dashed") + # Upper confidence interval
  #geom_line(aes(y = lwr), linetype = "dashed") + # Lower confidence interval
  geom_ribbon(aes(x=Training_set, ymax=upr, ymin=lwr), fill = "gray", alpha = 0.5) +
  geom_point(data = my_data, aes(y = h2.ped., x = Training_set, color = Plant, )) + # As points, we want the observed values from the base dataframe
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  labs(x = "Size of the training set", y = "ABLUP pedigree-based heritability") +
  annotate("text", x = 550, y = 1.09, label =
             paste0("list(r == ", round(p_r_Ts, 2), ", italic(P) == ", round(pv_p_r_Ts, 4), ")"), parse = T) +
  theme_classic() +
  theme(legend.position = "top")


######################################################
# Regressions with cofactors
# y = h2.mark., PA_GBLUP, PACC_GBLUP
# Plant (Conifers, Broadleaved), Type (FS, HS), h2str
######################################################

h2.mark. <- as.numeric(Donnees$h2.mark.)
A <- mutate(Donnees, h2str = case_when(h2.mark. < 0.2 ~ 'low',
                           h2.mark. >= 0.2 & h2.mark. < 0.35 ~ 'med',
                           TRUE ~ 'high' ))
h2str <- as.factor(A$h2str)

res <- lm(h2.mark. ~ LNB_cM + LNs + Training_set + Plant + Type, data = Donnees)
summary(res)
anova(res)
