# Load (or install) the required packages 
library(tidyverse) # used to summarize raw data and for plotting
library(ggrepel) # text for ggplot
library(ggmap) # used to show sampling locations in a graphical form
# ggmap works in wgs84 projection (convert coords if needed)
library(maptools) # used for mapping of the sampling locations
library(sp) # used for mapping of the sampling locations
library(rgdal) # used for mapping of the sampling locations
library(gstat) # used for variogram modelling 
library(geodata) # used for variogram modelling 
library(psych) # LDA analysis
library(automap) # automatic variogram mapping 
library(nlme) # used for mixed effects modeling
library(randomForest) # random forest analysis
library(caret) # random forest analysis
library(caTools) # random forest analysis
library(Metrics) # random forest analysis (metrics)

library(conflicted)

rm(list = lsf.str())
rm(list = ls())

soc.main = read.csv(".../SUM_171023.csv")
str(soc.main)
# colnames(soc.main) <- c('region', 'estate', 'plant', 'field', 'establishment')
head(soc.main)

# recode the variables if needed
soc.main$region<-as.factor(soc.main$region)
soc.main$estate<-as.factor(soc.main$estate)
soc.main$planting<-as.numeric(soc.main$planting)
soc.main$age<-as.numeric(soc.main$age)
soc.main$depth<-as.factor(soc.main$depth)
soc.main$sampling<-as.numeric(soc.main$sampling)
soc.main$campaign<-as.factor(soc.main$campaign)
soc.main$ID<-as.numeric(soc.main$ID)
soc.main$geology<-as.factor(soc.main$geology)
soc.main$geology_gen<-as.factor(soc.main$geology_gen)
soc.main$peatlands<-as.factor(soc.main$peatlands)
age_breaks <- c(0, 3, 6, 12, 20, Inf)
age_labels <- c("immature", "young mature", "prime", "mature", "old")
soc.main$age_class <- cut(soc.main$age, breaks = age_breaks, labels = age_labels, right = FALSE)
soc.main$age_class<-as.factor(soc.main$age_class)
soc.main$age_class<-as.factor(soc.main$age_class)

dplyr::count(soc.main)

# soc.main <- soc.main %>% dplyr::mutate(ID = row_number()) # add a row ID column

# What to look out for first -----------------------------

# Missing values

colSums(is.na(soc.main)) # 147 values in CEC

# missing_coords = is.na(soc.main$x) | is.na(soc.main$y) # omit coordinate NAs
# sum(missing_coords) # new data frame with missing coordinates 
# soc.with.coords = subset(soc.main, subset = !missing_coords)
# summary(soc.with.coords) # base R function for data summary 

soc = na.omit(soc.main)
dplyr::count(soc)

library(data.table)

soc %>% group_by(depth, campaign) %>% # we can see we are dealing with min and organic soils
             summarize(min = min(SOC), # function names are very intuitive
             max = max(SOC),
             mean = mean(SOC),
             median = median(SOC),
             sd = sd(SOC),
             n = length(SOC)) # tidyverse equivalent 

# Duplicate values

topsoil = subset(soc, depth =='1')
topsoil.C1 = subset(topsoil, campaign =='1')
topsoil.C2 = subset(topsoil, campaign =='2')
dplyr::count(topsoil.C1)
dplyr::count(topsoil.C2)
topsoil.C1[duplicated(topsoil.C1$x), ]
topsoil.C2[duplicated(topsoil.C2$x), ]

# Erroneous values 
soc %>% group_by(region) %>% 
        dplyr::summarize(min = min(age), 
                         max = max(age),
                         mean = mean(age),
                         median = median(age),
                         sd = sd(age),
                         n = length(age)) # age range of 0 to 28
min(soc$planting) # for the whole dataset
max(soc$planting)
mean(soc$age)
sd(soc$age)

# remove incorrect entries
soc %>% filter_all(any_vars(. %in% c('-1'))) # find the error value
# soc$age[soc$age == -1] <- 0

# Visualizations to detect errors  -----------------------------
SOC.on.map <- ggplot(data = soc,
                     mapping = aes(x = x, y = y, color = SOC)) +
  geom_point(size = 2) +
  scale_color_gradientn(colors = 
                          c("yellow", "brown","black")) + 
  geom_rect(aes(xmin = 100.432291, 
                xmax = 101.441483, 
                ymin = 3.739870, 
                ymax = 5.807858),
                fill = "transparent", 
                color = "blue", 
                linewidth = 1) + 
  geom_rect(aes(xmin = 101.279465, 
                xmax = 103.313575, 
                ymin = 2.676705, 
                ymax = 4.043089),
            fill = "transparent", 
            color = "yellow", 
            linewidth = 1) + 
  geom_rect(aes(xmin = 101.769451, # central west
                xmax = 102.815148, 
                ymin = 2.099553, 
                ymax = 2.95854),
            fill = "transparent", 
            color = "red", 
            linewidth = 1) + 
  geom_rect(aes(xmin = 102.950218, 
                xmax = 103.645534, 
                ymin = 1.597783, 
                ymax = 2.393169),
            fill = "transparent", 
            color = "green", 
            linewidth = 1)

soc <- soc %>% mutate(region = recode(region, 
                                     "CENTRALEAST" = "CENTRAL",
                                     "CENTRALWEST" = "CENTRAL"))
# Data exploration: Outliers  -----------------------------
# topsoil = subset(soc.with.coords, depth =='1')
# View(topsoil)
boxplot(soc$SOC,
        ylab = "SOC (in %)",
        xlab = "", main = "Boxplot of SOC observations")

organic.soil = soc[soc$SOC > 12, ] # IPCC classification
mineral.soil = soc[soc$SOC < 12, ] # a class to be modeled should have
# at least 5 observations
high.values.on.map <- ggplot(data = mineral.soil,
                      mapping = aes(x = x, y = y, color = SOC)) +
                      geom_point(size = 2) +
  scale_color_gradientn(colors = 
                          c("yellow", "brown","black")) + 
  geom_rect(aes(xmin = 100.432291, 
                xmax = 101.441483, 
                ymin = 3.739870, 
                ymax = 5.807858),
            fill = "transparent", 
            color = "blue", 
            linewidth = 1) + 
  geom_rect(aes(xmin = 101.279465, 
                xmax = 103.313575, 
                ymin = 2.676705, 
                ymax = 4.043089),
            fill = "transparent", 
            color = "yellow", 
            linewidth = 1) + 
  geom_rect(aes(xmin = 101.769451, # central west
                xmax = 102.815148, 
                ymin = 2.099553, 
                ymax = 2.95854),
            fill = "transparent", 
            color = "red", 
            linewidth = 1) + 
  geom_rect(aes(xmin = 102.950218, 
                xmax = 103.645534, 
                ymin = 1.597783, 
                ymax = 2.393169),
            fill = "transparent", 
            color = "green", 
            linewidth = 1)
# View(mineral.soil)
dplyr::count(mineral.soil)
dplyr::count(organic.soil) # 1 location fits the definition of organic soils

boxplot(mineral.soil$SOC,
        ylab = "SOC (in %)",
        xlab = "", main = "Boxplot of SOC observations")

library(ggrepel)
ggplot(mineral.soil, aes(x = 1, y = SOC)) +
  geom_point() +
  geom_text_repel(data = dplyr::filter(mineral.soil, SOC > 6), 
                  aes(label=ID), 
                  box.padding = 1) + theme_minimal()

dotchart(mineral.soil$SOC,
         ylab = "Order of observations",
         xlab = "SOC", main = "Cleveland dotplot")

dotchart(mineral.soil$SOC,
         groups = factor(mineral.soil$region),
         ylab = "Order of observations",
         xlab = "SOC", main = "Cleveland dotplot")

mineral.soil %>% group_by(region, campaign, depth) %>%
                 summarize(mean = mean(SOC),
                           sd = sd(SOC)/sqrt(length(SOC)),
                           n = length(SOC),
                           min = min(SOC), 
                           max = max(SOC),
                           median = median(SOC))

mineral.soil[mineral.soil$SOC > 7.5, ]
which(mineral.soil$SOC > 7.5, arr.ind=TRUE) # rows 376:378, 385:387
mineral.soil <- mineral.soil[-c(376:378, 385:387) ,]

dotchart(mineral.soil$SOC,
         groups = factor(mineral.soil$region),
         ylab = "Order of observations",
         xlab = "SOC", main = "Cleveland dotplot")

boxplot(mineral.soil$pH,
        ylab = "pH (-)",
        xlab = "", main = "Boxplot of pH observations")

dotchart(mineral.soil$pH,
         groups = factor(mineral.soil$region),
         ylab = "Order of observations",
         xlab = "SOC", main = "Cleveland dotplot")
            
mineral.soil %>% group_by(region, depth, campaign) %>% 
                 summarize(min = min(pH), 
                           max = max(pH),
                           mean = mean(pH),
                           median = median(pH),
                           sd = sd(pH)/sqrt(length(pH)),
                           n = length(pH))

mineral.soil[mineral.soil$pH > 8, ]
which(mineral.soil$pH > 7.5, arr.ind=TRUE) # rows 160:162, 172:174, 232:234, 238:240, 313:315
mineral.soil = mineral.soil[-c(160:162, 172:174, 232:234, 238:240, 313:315) ,]

dotchart(mineral.soil$pH,
         groups = factor(mineral.soil$region),
         ylab = "Order of observations",
         xlab = "SOC", main = "Cleveland dotplot")

boxplot(mineral.soil$CEC,
        ylab = "CEC (in %)",
        xlab = "", main = "Boxplot of CEC observations")

dotchart(mineral.soil$CEC,
         groups = factor(mineral.soil$region),
         ylab = "Order of observations",
         xlab = "SOC", main = "Cleveland dotplot")

mineral.soil %>% group_by(region, depth, campaign) %>% 
                 summarize(min = min(CEC), 
                           max = max(CEC),
                           mean = mean(CEC),
                           median = median(CEC),
                           sd = sd(CEC)/sqrt(length(CEC)),
                           n = length(CEC)) 

# Resampling in 2018/2019 -----------------------------
conflicts_prefer(plyr::summarize)
c1 = subset(mineral.soil, campaign =='1')
c2 = subset(mineral.soil, campaign =='2')

table(resampled1$region)
table(resampled2$region)

# write.csv(resampled1, '/Users/karolinagolicz/Downloads/resampled1.csv', row.names = TRUE)
# write.csv(resampled2, '/Users/karolinagolicz/Downloads/resampled2.csv', row.names = TRUE)
resampled = read.csv("~/Desktop/Malay paper/Analysis/SEM modelling/resampled.csv")
str(resampled)
resampled$region<-as.factor(resampled$region)
resampled$estate<-as.factor(resampled$estate)
resampled$planting<-as.numeric(resampled$planting)
resampled$age<-as.numeric(resampled$age)
resampled$depth<-as.factor(resampled$depth)
resampled$sampling<-as.numeric(resampled$sampling)
resampled$campaign<-as.factor(resampled$campaign)
resampled$ID<-as.numeric(resampled$ID)
resampled$geology<-as.factor(resampled$geology)
resampled$geology_gen<-as.factor(resampled$geology_gen)
resampled$peatlands<-as.factor(resampled$peatlands)
age_breaks <- c(0, 3, 6, 12, 20, Inf)
age_labels <- c("immature", "young mature", "prime", "mature", "old")
resampled$age_class <- cut(resampled$age, breaks = age_breaks, labels = age_labels, right = FALSE)
resampled$age_class<-as.factor(resampled$age_class)

head(resampled)
r = resampled[-c(22:24, 139:141), -c(16:47)]
view(r)

dfa <- r %>% group_by(region, campaign) %>% 
  summarize( 
    mean = median(age),
    sd = sd(age),
    n = length(age)) 

c1 = subset(dfa, campaign == '1')
c2 = subset(dfa, campaign == '2')

df <- r %>% group_by(depth, region, campaign) %>% 
              summarize( 
              mean = mean(SOC),
              sd = sd(SOC),
              n = length(SOC)) 
as.data.frame(df)

c1 = subset(df, campaign == '1')
c2 = subset(df, campaign == '2')

c1$mean-c2$mean

df_united <- df  %>% unite("id", depth, region, remove = FALSE)
df_united['id']<- factor(df_united$id, order = TRUE, 
                     levels = c("3_SOUTHERN",
                                "2_SOUTHERN",
                                "1_SOUTHERN",
                                "3_NORTHERN", 
                                "2_NORTHERN", 
                                "1_NORTHERN"))
df_united$id <- factor(df_united$id,
                     levels = c("3_SOUTHERN",
                                "2_SOUTHERN",
                                "1_SOUTHERN",
                                "3_NORTHERN", 
                                "2_NORTHERN", 
                                "1_NORTHERN"),
                     labels = c("Southern (30-45cm)",
                                 "Southern (15-30cm)",
                                 "Southern (0-15cm)",
                                 "Northern (30-45cm)",
                                 "Northern (15-30cm)",
                                 "Northern (0-15cm)")) 
conflicts_prefer(ggplot2::margin)

legend_title <- "Sampling campaign"

ggplot(df_united, aes(x = mean, y = id)) +
  geom_line() +
  geom_point(aes(color = campaign), size = 3) +
  theme(legend.position = "bottom") +
  theme_linedraw() + 
  labs(x ="Soil pH (-)", y = "Region and soil depth profile") + 
  theme(text = element_text(size = 13)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) +
  theme(legend.position="top") +
  scale_color_manual(legend_title, values=c("black", "grey")) + 
  theme(axis.text.x = element_text(color="black", size=12),
        axis.text.y = element_text(color="black", size=12)) 

resampled %>% group_by(region, campaign) %>% 
              summarize(mean = mean(SOC),
                        sd = sd(SOC),
                        n = length(SOC), 
                        min = min(SOC), 
                        max = max(SOC),
                        median = median(SOC)) 

resampled %>% group_by(region, campaign) %>% 
  summarize(mean = mean(pH),
            se = sd(pH),
            n = length(pH), 
            min = min(pH), 
            max = max(pH),
            median = median(pH)) 

resampled %>% group_by(region, campaign) %>% 
              summarize(mean = mean(CEC),
                        se = sd(CEC),
                        n = length(CEC), 
                        min = min(CEC), 
                        max = max(CEC),
                        median = median(CEC)) 

bwplot(pH ~ campaign | region, data = resampled,
       strip = strip.custom(bg = 'white'),
       cex = .5, layout = c(1, 2),
       xlab = "Sampling capaign", ylab = "SOC (%)",
       par.settings = list(box.rectangle = list(col = 1),
                           box.umbrella  = list(col = 1),
                           plot.symbol   = list(cex = .5, col = 1)),
       scales = list(x = list(relation = "same"),
                     y = list(relation = "same")))

data_summary <- function(data, varname, grps){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sd(x[[col]], na.rm=TRUE))
  }
  data_sum<-ddply(data, grps, .fun=summary_func, varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

conflicts_prefer(plyr::rename)
df_pH <- data_summary(resampled, varname="pH", grps=c("region", "campaign", "depth"))

f <- ggplot(df_pH, aes(x = campaign, y = pH, 
                     ymin = pH-sd, ymax = pH+sd))

conflicts_prefer(ggplot2::margin)

f + geom_pointrange(aes(color = region), 
                    position = position_dodge(0.5)) +
  theme_linedraw() + 
  labs(x ="Sampling campaigns", y = "Soil pH (-)") + 
  theme(text = element_text(size = 14)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) +
  theme(legend.position="top") +
  scale_color_manual(values=c("dodgerblue3", "forestgreen")) + 
  theme(legend.title=element_blank()) + 
  theme(axis.text.x = element_text(color="black", size=13),
        axis.text.y = element_text(color="black", size=13))

df_CEC <- data_summary(resampled, varname="CEC", grps=c("region", "campaign"))

g <- ggplot(df_CEC, aes(x = campaign, y = CEC, 
                       ymin = CEC-sd, ymax = CEC+sd))
g + geom_pointrange(aes(color = region), 
                    position = position_dodge(0.5)) +
  theme_linedraw() + 
  labs(x ="Sampling campaigns", y = "Soil CEC (ccmol per)") + 
  theme(text = element_text(size = 13)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) +
  theme(legend.position="top") +
  scale_color_manual(values=c("dodgerblue3", "forestgreen")) + 
  theme(legend.title=element_blank()) + 
  theme(axis.text.x = element_text(color="black", size=12),
        axis.text.y = element_text(color="black", size=12))

df_SOC <- data_summary(resampled, varname="SOC", grps=c("region", "campaign"))
h <- ggplot(df_SOC, aes(x = campaign, y = SOC, 
                        ymin = SOC-sd, ymax = SOC+sd))
h + geom_pointrange(aes(color = region), 
                    position = position_dodge(0.5)) +
  theme_linedraw() + 
  labs(x ="Sampling campaigns", y = "Soil OC (%)") + 
  theme(text = element_text(size = 13)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) +
  theme(legend.position="top") +
  scale_color_manual(values=c("dodgerblue3", "forestgreen")) + 
  theme(legend.title=element_blank()) + 
  theme(axis.text.x = element_text(color="black", size=12),
        axis.text.y = element_text(color="black", size=12))

f + geom_pointrange(aes(colour = depth), 
                    position = position_dodge(0.5)) + 
  theme_linedraw() +
  facet_grid(region ~ .) + 
  theme(text = element_text(size = 13)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) + 
  labs(x ="Sampling campaigns", y = "Soil pH (-)") + 
  theme(legend.position="top") + theme(legend.title = element_blank()) + 
  scale_colour_grey(name="Depth",
                       breaks=c(1,2,3),
                       labels=c("0-15cm","15-30cm","30-45cm")) 

df_CEC <- data_summary(resampled, varname="CEC", grps=c("region", "campaign", "depth"))

g <- ggplot(df_CEC, aes(x = campaign, y = CEC, 
                        ymin = CEC-sd, ymax = CEC+sd))

g  + geom_pointrange(aes(colour = depth), 
                     position = position_dodge(0.5)) + 
  theme_linedraw() +
  facet_grid(region ~ .) + 
  theme(text = element_text(size = 13)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) + 
  labs(x ="Sampling campaigns", y = "Soil cation exchange capacity (ccmol per kg)") + 
  theme(legend.position="top") + theme(legend.title = element_blank()) + 
  scale_colour_grey(name="Depth",
                    breaks=c(1,2,3),
                    labels=c("0-15cm","15-30cm","30-45cm")) 

df_SOC <- data_summary(resampled, varname="SOC", grps=c("region", "campaign", "depth"))
h <- ggplot(df_SOC, aes(x = campaign, y = SOC, 
                        ymin = SOC-sd, ymax = SOC+sd))

h  + geom_pointrange(aes(colour = depth), 
                     position = position_dodge(0.5)) + 
  theme_linedraw() +
  facet_grid(region ~ .) + 
  theme(text = element_text(size = 13)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) + 
  labs(x ="Sampling campaigns", y = "Soil organic carbon (%)") + 
  theme(legend.position="top") + theme(legend.title = element_blank()) + 
  scale_colour_grey(name="Depth",
                    breaks=c(1,2,3),
                    labels=c("0-15cm","15-30cm","30-45cm")) 

# aes(color = 'region'), 
# position = position_dodge(.5)

# Data exploration: Homogeneity  -----------------------------
# Keep in mind that this is exploratory, the same techniques must be 
# employed to test homogeneity of the model residuals (not raw data)
boxplot(SOC ~ factor(region),
              varwidth = TRUE, xlab = "Region",
              main = "Boxplot of SOC conditional on \ region", 
              ylab = "SOC", data = mineral.soil) # variation in the observations 
boxplot(SOC ~ factor(campaign),
              varwidth = TRUE, xlab = "Campaign",
              main = "Boxplot of SOC conditional on \ campaign", 
              ylab = "SOC", data = mineral.soil)
boxplot(SOC ~ factor(peatlands),
        varwidth = TRUE, xlab = "Peatland",
        main = "Boxplot of SOC conditional on \ peatland", 
        ylab = "SOC", data = mineral.soil)
boxplot(SOC ~ factor(age_class),
        varwidth = TRUE, xlab = "Age class",
        main = "Boxplot of SOC conditional on \ age class", 
        ylab = "SOC", data = mineral.soil)
bwplot(SOC ~ campaign | region, data = mineral.soil,
                                strip = strip.custom(bg = 'white'),
                                cex = .5, layout = c(2, 2),
                                xlab = "Sampling capaign", ylab = "SOC",
                                par.settings = list(box.rectangle = list(col = 1),
                                                    box.umbrella  = list(col = 1),
                                                    plot.symbol   = list(cex = .5, col = 1)),
                                                    scales = list(x = list(relation = "same"),
                                                    y = list(relation = "same")))
bartlett.test(mineral.soil$cubSOC, mineral.soil$campaign) # sensitive to 
# non-normality

# Data exploration: Normality  -----------------------------
# Keep in mind that this is exploratory, the same techniques must be 
# employed to test normality of the model residuals (not raw data)

hist(mineral.soil$SOC) # normality of the raw data implies normality of the residuals

logSOC = log(mineral.soil$SOC)
sqrtSOC = sqrt(mineral.soil$SOC)
cubSOC = mineral.soil$SOC^(1/3)

hist(cubSOC) # cube root transformation works best

# mineral.soil$cubSOC <- mineral.soil$SOC^(1/3)

# Data exploration: Relationships between variables  --------------------

colnames(mineral.soil)

cor(mineral.soil[c(6, 10:12, 16, 17, 19:23, 27:45)], method = "spearman") 

# my_cols <- c("#FFCC33", "#CC9900", "#663300") 

pairs(mineral.soil[c(6, 10:12)], pch = 19)

pairs.panels(mineral.soil[c(6, 10:12)],
             gap = 0,
             bg = c("red", "green", "blue")[mineral.soil$region],
             pch = 21)
# Booth et al. (1994) suggest that correlations between pairs of variables 
# with magnitudes greater than ± 0.5 indicate high collinearity

# plotscale variables 

plotscale = mineral.soil[c(6, 10:12)]

library(devtools) 
library(nlcor)
plot(mineral.soil$SOC, mineral.soil$pH)
c <- nlcor(mineral.soil$SOC, mineral.soil$pH, plt = T)
c$cor.estimate
c$adjusted.p.value
print(c$cor.plot) # 0.14 for pH, 0.45 for CEC; plot-scale covariates to be retained

# regional variables: region
boxplot(mineral.soil$SOC ~ mineral.soil$region) # possible covariate

# regional variables: climate
cor(mineral.soil[c(11, 44:46)], method = "spearman") 
pairs(mineral.soil[c(11, 44:46)], pch = 19)
climate = mineral.soil[c(11, 44:46)] # MAP to be retained as a covariate 

# regional variables: vegetation
vegetation = mineral.soil[c(11, 30:43)]
cor(mineral.soil[c(11, 30:43)], method = "spearman") # all are collinear

plot(mineral.soil$SOC, mineral.soil$mdNDVI_C2)
c <- nlcor(mineral.soil$SOC, mineral.soil$mdNDVI_C2, plt = T)
c$cor.estimate
c$adjusted.p.value
print(c$cor.plot) # retain mdNDVI_C2 and mdNDVI (collinear, add one at a time)

# regional variables: topography
topography = mineral.soil[c(11, 16:23, 28:29)]
cor(mineral.soil[c(11, 16:23, 28:29)], method = "spearman") # ELE and dist_coast
pairs(mineral.soil[c(11, 16:23, 28:29)], pch = 19) 

# regional variables: parent material
parent_material = mineral.soil[c(11, 24:26)]
boxplot(mineral.soil$SOC ~ mineral.soil$geology) # significant predictor or random effect
boxplot(mineral.soil$SOC ~ mineral.soil$geology_gen)
boxplot(mineral.soil$SOC ~ mineral.soil$peatlands) # potentially significant predictor
topsoil = subset(mineral.soil, depth == '1')
plot(topsoil$dist_peatland, topsoil$SOC) 

c <- nlcor(topsoil$SOC, topsoil$dist_peatland, plt = T)
c$cor.estimate
c$adjusted.p.value
print(c$cor.plot)

# Data exploration: Interactions  -----------------------------
topsoil = subset(mineral.soil, depth =='1')
subsoil = subset(mineral.soil, depth =='2')
deepsoil = subset(mineral.soil, depth =='3')

bwplot(SOC ~ age_class | region, data = deepsoil,
       strip = strip.custom(bg = 'white'),
       cex = .5, layout = c(2, 2),
       xlab = "Age class", ylab = "SOC (in %)",
       par.settings = list(
         box.rectangle = list(col = 1),
         box.umbrella  = list(col = 1),
         plot.symbol   = list(cex = .5, col = 1)),
         scales = list(x = list(relation = "same"),
                       y = list(relation = "same")))

M1 <- lm(SOC ~ CEC * region * age_class, data = topsoil)
summary(M1)
anova(M1) # CEC and age class are significant but is the relationship linear?

coplot(SOC ~ CEC | age_class, ylab = "SOC", xlab = "CEC", data = topsoil)

coplot(SOC ~ CEC | geology, ylab = "SOC", xlab = "...", data = topsoil)

# we can explore them further by adding 'region' and 'texture class'
interaction.plot(x.factor = topsoil$CEC, 
                 trace.factor = topsoil$age_class, 
                 response = topsoil$SOC, fun = median, 
                 xlab = "Cation exchange capacity", ylab = "Soil organic carbon",
                 trace.label = "Age", col = c("blue", "green", "orange", "black"))
# if lines are parallel = no interaction, if they intersect, there is an interaction

# Data exploration: Spatial and temporal dependencies  -------------------

# reproject x and y to metric [epsg 3375 with CRS("+init=epsg:3375")]

coordinates(topsoil) = ~ x+y
proj4string(topsoil) <- CRS("+init=epsg:4326") # WGS 84
CRS.new <- CRS("+proj=omerc +no_uoff +lat_0=4 +lonc=102.25 +alpha=323.025796466667
+gamma=323.130102361111 +k=0.99984 +x_0=804671 +y_0=0 +ellps=GRS80 +units=m
+no_defs")
topsoil = spTransform(topsoil, CRS.new)

bubble(topsoil, 'SOC', 
       col = 'brown', maxsize=3, do.sqrt=FALSE,
       xlab = "X-coordinates", ylab = "Y-coordinates") 
var = variogram(SOC ~ 1, data = mineral.soil) 
plot(var)

# var.M1 = vgm(psill=1.1, model="Exp", nugget=0.22, range=112500)
# fitted.model = fit.variogram(var, model=var.M1)    
# plot(var, model=var.M1)

auto.variogram = autofitVariogram(SOC ~ 1, mineral.soil)
plot(auto.variogram) # note sensitivity to skewed values and issues with transformation

colnames(mineral.soil)

SEM <- mineral.soil[c('ID', 'region', 'estate', 'depth', 'campaign',
                    'pH', 'SOC', 'CEC', 'x', 'y', 'ELE', 'geology',
                    'peatlands', 'dist_peatland', 'dist_coast',
                    'mdNDVI_C2', 'mdNDVI', 'MAP', 'age_class')]

write.csv(SEM, '/Users/karolinagolicz/Downloads/SEM.csv', row.names = TRUE)

p1 <- ggplot(age_effect, aes(age_class, logSOC_pred,
                             ymin = logSOC_pred + age_effect$upconf, ymax = logSOC_pred - age_effect$loconf))

p1  + geom_pointrange(aes(colour = depth), 
                      position = position_dodge(0.5)) + 
  theme_linedraw() +
  facet_grid(region ~ .) + 
  theme(text = element_text(size = 13)) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) + 
  labs(x ="Plantation age", y = "Soil organic carbon (log transformed)") + 
  theme(legend.position="top") + theme(legend.title = element_blank()) + 
  scale_colour_grey(name="Depth",
                    breaks=c(1,2,3),
                    labels=c("0-15cm","15-30cm","30-45cm")) 
