
##############################################################################
##############################################################################
### FIGURE S6. same as Fig. 2, panel C and D, but with epsilon from inertial subrange method, 
### with all estimates and only with trustful epsilon estimates
##############################################################################
##############################################################################

# Creates Figure S6 in
# Klaus, M., Durighetto, N., Khan, A. M., Chatton, E., Öquist, M., Peruzzo, P. and Botter, G. (in revision in "npj Hydrosphere").
# Bubble size spectra reveal distinct scaling patterns and downstream sorting in mountain streams 

###########################################################################################
## created by Marcus Klaus, Swedish University of Agricultural Sciences (SLU), Umeå, Sweden
## contact: marcus.klaus@slu.se
## last update: 2026/08/10
###########################################################################################

## load libraries
library(nlme)
library(viridis)
library(MuMIn)
library(dplyr)
library(car)
library(fields)
library(lattice)
library(HLMdiag)

## prepare figure

windows(width = 8, height = 8)
layout(matrix(1:4, 2, 2, byrow = TRUE))
par(mar=c(4.5,5.5,0.5,0.55),las=1,cex=1,mgp=c(3.6, 1, 0))

## print model evaluation plots?
ModelEval=FALSE


##########################################################################################################################################
##### lme model predicting turbulent kinetic energy dissipation based on inertial subrange method, including all estimates
##########################################################################################################################################


# organize / rescale variables
xq=log10(DataSpot$Q)
xh=log10(DataSpot$h)
xd=log10(DataSpot$d)
xl=log10(DataSpot$l)
s=as.factor(DataSpot$Site)
c=as.factor(DataSpot$Catchment)
pH=DataSpot$pH
Temp=DataSpot$Temp
xc=log10(DataSpot$EC)
xe=log10(DataSpot$epsU_isr)

y=xe


my_data=data.frame(y,xq,xh,xd,xl,s,c,xc,Temp,pH)[which(y>-99999),]


## select random effects
epsmod1 <- lme(y~xq+xd+xh+xl+xc+Temp+pH,random=~1|c/s,data=my_data)
epsmod2 <- lme(y~xq+xd+xh+xl+xc+Temp+pH,random=~xd|c/s,data=my_data)
#epsmod3 <- lme(y~xq+xd+xh+xl+xc+Temp+pH,random=~xl|c/s,data=my_data)
anova(epsmod1,epsmod2)
VarCorr(epsmod2)
## -> random slope improves model, but slope and intercept correlate highly, indicating overfitting. keep simple random intercept model

## select fixed effects
full_model <- lme(y~xq+xh+xd+xl+Temp+xc+pH,random=~1|c/s,data=my_data, method="ML")
dredge_epsISRall <- dredge(full_model)


## average best models within dAICc < 2
top_models_epsISRall <- get.models(dredge_epsISRall, subset = delta < 2)
AvgModel_epsISRall <- model.avg(top_models_epsISRall)

best_single_model_epsISRall <- top_models_epsISRall[[1]]



# Extract actual, fitted model objects from the dredge table
top_models_epsISRall <- get.models(dredge_epsISRall, subset = delta < 2)

# Extract Marginal and conditional R2 (R2m, R2c) values
r2m_values_epsISRall <- sapply(top_models_epsISRall, function(m) r.squaredGLMM(m)[1, "R2m"])
r2c_values_epsISRall <- sapply(top_models_epsISRall, function(m) r.squaredGLMM(m)[1, "R2c"])

# Extract the Akaike weights belonging to these top models 
top_weights_epsISRall <- subset(dredge_epsISRall, delta < 2)$weight

# Calculate weighted average R2m
weighted_r2m_epsISRall <- sum(r2m_values_epsISRall * top_weights_epsISRall) / sum(top_weights_epsISRall)
weighted_r2c_epsISRall <- sum(r2c_values_epsISRall * top_weights_epsISRall) / sum(top_weights_epsISRall)



########################################################
# model evaluation
if(ModelEval==TRUE){
  E2 <- resid(best_single_model_epsISRall , type = "normalized")
  
  xyplot(E2 ~ xd | s,
         data = my_data, ylab = "Residuals",
         xlab = "d",
         panel = function(x,y){
           panel.grid(h = -1, v = 2)
           panel.points(x, y, col = 1)
           panel.loess(x, y, span = 0.5, col = 1,lwd=2)})
  
  boxplot(E2 ~ xd, data = my_data,ylim = c(-3, 3))
  boxplot(E2 ~ xq, data = my_data,ylim = c(-3, 3))
  boxplot(E2 ~ xh, data = my_data,ylim = c(-3, 3))
  
  F2 <- fitted(best_single_model_epsISRall)
  plot(x = F2, y = E2, xlab = "Fitted values")
  plot(x = my_data$xd, y = E2, xlab = "Fitted values")
  plot(x = my_data$xh, y = E2, xlab = "Fitted values")
  plot(x = my_data$xq, y = E2, xlab = "Fitted values")
  
  ## Cook's Distance to identify influential points
  infl <- hlm_influence(best_single_model_epsISRall)
  infl_sorted <- infl[order(infl$cooksd, decreasing = TRUE), ]
  # View Cook's Distance
  dotplot_diag(infl$cooksd, name = "cooks.distance")
}
########################################################


########################
### n vs. d and Q
########################


colors <- viridis(max(round(my_data$xq*10)-min(round(my_data$xq*10))+1))[round(my_data$xq*10)-min(round(my_data$xq*10))+1]


plot(y~xd, pch=1,col=colors, xaxt="n", yaxt="n",xlab="",data=my_data,xlim=log10(c(5,600)),ylim=log10(c(10^-6,10^3)),
     ylab=expression(paste(epsilon," ( ",m^2,s^-3,")")))


# Unique sites
sites <- unique(my_data$s)

for (i in seq_along(sites)) {
  
  site_data <- subset(my_data, s == sites[i])
  
  x_seq <- seq(
    min(site_data$xd, na.rm = TRUE),
    max(site_data$xd, na.rm = TRUE),
    length.out = 100
  )
  
  # Prediction data
  newdata <- data.frame(
    xd = x_seq,
    c    = unique(site_data$c),
    s = sites[i],
    xq   = unique(site_data$xq),
    xh   = unique(site_data$xh),
    xl   = mean(site_data$xl),
    xc   = unique(site_data$xc),
    Temp = unique(site_data$Temp),
    pH   = unique(site_data$pH)
  )
  
  # Model-averaged prediction
  pred <- predict(
    AvgModel_epsISRall,
    newdata = newdata,
    level = 2
  )
  
  lines(
    x_seq,
    pred,
    col = colors[which(my_data$s == sites[i])[1]],
    lwd = 1
  )
}




axis(side=1,at=(log10(c(seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10),seq(100,1000,100)))),labels=NA)
axis(side=1,at=(log10(c(0.001,0.01,0.1,1,10,100,1000))),lwd=2,labels=c(0.001,0.01,0.1,1,10,100,1000))

axis(side=2,at=(log10(c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1,10,100))),labels=c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1,10,100),lwd=2)
axis(side=2,at=(log10(c(seq(1,10,1),seq(10,100,10),seq(100,1000,100))))*100000,labels=NA)
axis(side=2,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100))),lwd=2,labels=NA)

axis(side=2,at=log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100)),labels=c(10^-10,10^-9,10^8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100))
axis(side=2,at=log10(c(seq(1,10,1),seq(10,100,10),seq(100,1000,100)))*100000,labels=NA)


text(log10(0.4),log10(900),"A)",xpd=TRUE,font=2)

mtext("d (cm)", side=1, line=3)  # Move X-axis label further away


colorbar.plot(log10(270),log10(10), 1:100,horizontal = FALSE, strip.width = 0.1, strip.length = 0.3,col=(viridis(38)))
text(log10(320),log10(800),expression(paste("Q (L",s^-1,")")),cex=0.8,col=1)
text(log10(520),log10(100),"144",cex=0.8,col=1)
text(log10(470),log10(1),"1",cex=0.8,col=1)





##########################################################################################################################################
### Micrubble abundance vs. turbulent kinetic energy dissipation based on inertial subrange method, 
### including all estimates 
##########################################################################################################################################



###################################
##### lme model n vs eps as function of q

y=log10(DataSpot$N_tot)

my_data=data.frame(y,xe,xq,xh,xd,xl,Temp,xc,c,s)[which(y>-99999 & xe>-9999),]

## select random effects
epsmod1 <- lme(y~xe+xd,random=~1|c/s,data=my_data)
#epsmod2 <- lme(y~xe+xd,random=~xd|c/s,data=my_data)
#epsmod3 <- lme(y~xe+xd,random=~xl|c/s,data=my_data)

## random slope improves model, but slope and intercept correlate highly, indicating overfitting. keep simple random intercept model

## best model
best_model_n_vs_epsISRall <- lme(y~xe+xd,random=~1|c/s,data=my_data)


## model output
summary(best_model_n_vs_epsISRall)
r.squaredGLMM(best_model_n_vs_epsISRall)


########################################################
# model evaluation
if(ModelEval==TRUE){
  E2 <- resid(best_model_n_vs_epsISRall , type = "normalized")
  
  xyplot(E2 ~ xd | s,
         data = my_data, ylab = "Residuals",
         xlab = "d",
         panel = function(x,y){
           panel.grid(h = -1, v = 2)
           panel.points(x, y, col = 1)
           panel.loess(x, y, span = 0.5, col = 1,lwd=2)})
  
  boxplot(E2 ~ xd, data = my_data,ylim = c(-3, 3))
  boxplot(E2 ~ xe, data = my_data,ylim = c(-3, 3))
  
  F2 <- fitted(best_model_n_vs_epsISRall)
  plot(x = F2, y = E2, xlab = "Fitted values")
  plot(x = my_data$xd, y = E2, xlab = "Fitted values")
  plot(x = my_data$xe, y = E2, xlab = "Fitted values")
  
  ## Cook's Distance to identify influential points
  infl <- hlm_influence(best_model_n_vs_epsISRall)
  infl_sorted <- infl[order(infl$cooksd, decreasing = TRUE), ]
  # View Cook's Distance
  dotplot_diag(infl$cooksd, name = "cooks.distance")
}
########################################################



colors <- inferno(max(round(my_data$xd*10)-min(round(my_data$xd*10))+1))[round(my_data$xd*10)-min(round(my_data$xd*10))+1]

plot(y~xe, pch=1, col=colors, xaxt="n", yaxt="n",xlab="",data=my_data,
     ylab=expression(paste(N[tot]," ( ",m^-3,")")),xlim=log10(c(10^-6,10^3)),ylim=log10(c(5*10^4,3*10^8)))

# Create a sequence of Q values for prediction
xe_seq <- seq(min(my_data$xe), max(my_data$xe), length.out = 155)

# Generate a new data frame with mean values for other predictors; d=0.1 m
pred_data <- data.frame(
  xe = xe_seq,
  xq = mean(my_data$xq, na.rm = TRUE),
  xh = mean(my_data$xh, na.rm = TRUE),
  xd = log10(10),
  xl = mean(my_data$xl, na.rm = TRUE)
)
# Get predictions
pred_data$predicted <- predict(best_model_n_vs_epsISRall, newdata = pred_data,level=0)
lines(xe_seq, pred_data$predicted, col = inferno(8)[2], lwd = 2)  # Add line

# Generate a new data frame with mean values for other predictors; d=0.3 m
pred_data <- data.frame(
  xe = xe_seq,
  xq = mean(my_data$xq, na.rm = TRUE),
  xh = mean(my_data$xh, na.rm = TRUE),
  xd = log10(50),
  xl = mean(my_data$xl, na.rm = TRUE)
)
# Get predictions
pred_data$predicted <- predict(best_model_n_vs_epsISRall, newdata = pred_data,level=0)
lines(xe_seq, pred_data$predicted, col = inferno(8)[3], lwd = 2)  # Add line

# Generate a new data frame with mean values for other predictors; d=0.5 m
pred_data <- data.frame(
  xe = xe_seq,
  xq = mean(my_data$xq, na.rm = TRUE),
  xh = mean(my_data$xh, na.rm = TRUE),
  xd = log10(100),
  xl = mean(my_data$xl, na.rm = TRUE)
)
# Get predictions
pred_data$predicted <- predict(best_model_n_vs_epsISRall, newdata = pred_data,level=0)
lines(xe_seq, pred_data$predicted, col = inferno(8)[5], lwd = 2)  # Add line

# Generate a new data frame with mean values for other predictors; d=1 m
pred_data <- data.frame(
  xe = xe_seq,
  xq = mean(my_data$xq, na.rm = TRUE),
  xh = mean(my_data$xh, na.rm = TRUE),
  xd = log10(200),
  xl = mean(my_data$xl, na.rm = TRUE)
)
# Get predictions
pred_data$predicted <- predict(best_model_n_vs_epsISRall, newdata = pred_data,level=0)
lines(xe_seq, pred_data$predicted, col = inferno(8)[6], lwd = 2)  # Add line



axis(side=1,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100))),labels=c(10^-10,10^-9,10^8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100))
axis(side=1,at=(log10(c(seq(1,10,1),seq(10,100,10),seq(100,1000,100))))*100000,labels=NA)
axis(side=1,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100,1000,10000))),lwd=2,labels=NA)


axis(side=2,at=(log10(c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1,10))),labels=c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1,10),lwd=2)
axis(side=2,at=(log10(c(seq(0.00000001,0.0000001,0.00000001),seq(0.0000001,0.000001,0.0000001),seq(0.000001,0.00001,0.000001),seq(0.00001,0.0001,0.00001),seq(0.0001,0.001,0.0001),seq(0.001,0.01,0.001),seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10),seq(100,1000,100))*1000000)),labels=NA)
axis(side=2,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10))),lwd=2,labels=NA)

text(log10(3*10^-11),log10(27*10^7),"B)",xpd=TRUE,font=2)



mtext(expression(paste(epsilon," ( ",m^2,s^-3,")")), side=1, line=3)  # Move X-axis label further away


colorbar.plot(log10(20),log10(20*10^4), 1:100,horizontal = FALSE, strip.width = 0.1, strip.length = 0.3,col=((inferno(38))))
text(log10(110),log10(13.4*10^5),"d (cm)",cex=0.8,col=1)
text(log10(400),log10(53*10^4),"580",cex=0.8,col=1)
text(log10(400),log10(8.5*10^4),"5",cex=0.8,col=1)




##########################################################################################################################################
##### lme model predicting turbulent kinetic energy dissipation based on inertial subrange method, 
##### including only estimates that passed the quality check
##########################################################################################################################################

y=log10(DataSpot$epsU_isr)

## selection (quality checks)
sel = which(y>-99999 & DataSpot$SNRU>15 & DataSpot$corrU>70 & DataSpot$epsU_isr/DataSpot$epsUmin_isr>3 & DataSpot$R2U_isr>0)

my_data=data.frame(y,xq,xh,xd,xl,s,c,xc,Temp,pH)[sel,]


## select random effects
epsmod1 <- lme(y~xq+xh+xd+xl+Temp+xc+pH,random=~1|c/s,data=my_data)
epsmod2 <- lme(y~xq+xh+xd+xl+Temp+xc+pH,random=~xd|c/s,data=my_data)
#epsmod3 <- lme(y~xq+xh+xd+xl+Temp+xc+pH,random=~xl|c/s,data=my_data)
anova(epsmod1,epsmod2)
VarCorr(epsmod2)
## random slope improves model, but slope and intercept correlate highly, indicating overfitting. keep simple random intercept model

# remove influential points (see Cooks'd analysis below)
my_data <- my_data[-91,]

## select fixed effects
full_model <- lme(y~xq+xh+xd+xl+Temp+xc+pH,random=~1|c/s,data=my_data, method="ML")
dredge_epsISRtrust <- dredge(full_model)


## average best models within dAICc < 2
top_models_epsISRtrust <- get.models(dredge_epsISRtrust, subset = delta < 2)
AvgModel_epsISRtrust <- model.avg(top_models_epsISRtrust)

best_single_model_epsISRtrust <- top_models_epsISRtrust[[1]]



# Extract actual, fitted model objects from the dredge table
top_models_epsISRtrust <- get.models(dredge_epsISRtrust, subset = delta < 2)

# Extract Marginal and conditional R2 (R2m, R2c) values
r2m_values_epsISRtrust <- sapply(top_models_epsISRtrust, function(m) r.squaredGLMM(m)[1, "R2m"])
r2c_values_epsISRtrust <- sapply(top_models_epsISRtrust, function(m) r.squaredGLMM(m)[1, "R2c"])

# Extract the Akaike weights belonging to these top models 
top_weights_epsISRtrust <- subset(dredge_epsISRtrust, delta < 2)$weight

# Calculate weighted average R2m
weighted_r2m_epsISRtrust <- sum(r2m_values_epsISRtrust * top_weights_epsISRtrust) / sum(top_weights_epsISRtrust)
weighted_r2c_epsISRtrust <- sum(r2c_values_epsISRtrust * top_weights_epsISRtrust) / sum(top_weights_epsISRtrust)



########################################################
# model evaluation
if(ModelEval==TRUE){
  E2 <- resid(best_single_model_epsISRtrust , type = "normalized")
  
  xyplot(E2 ~ xd | s,
         data = my_data, ylab = "Residuals",
         xlab = "d",
         panel = function(x,y){
           panel.grid(h = -1, v = 2)
           panel.points(x, y, col = 1)
           panel.loess(x, y, span = 0.5, col = 1,lwd=2)})
  
  boxplot(E2 ~ xd, data = my_data,ylim = c(-3, 3))
  boxplot(E2 ~ xq, data = my_data,ylim = c(-3, 3))
  boxplot(E2 ~ xh, data = my_data,ylim = c(-3, 3))
  
  F2 <- fitted(best_single_model_epsISRtrust)
  plot(x = F2, y = E2, xlab = "Fitted values")
  plot(x = my_data$xd, y = E2, xlab = "Fitted values")
  plot(x = my_data$xh, y = E2, xlab = "Fitted values")
  plot(x = my_data$xq, y = E2, xlab = "Fitted values")
  
  ## Cook's Distance to identify influential points
  infl <- hlm_influence(best_single_model_epsISRtrust)
  infl_sorted <- infl[order(infl$cooksd, decreasing = TRUE), ]
  # View Cook's Distance
  dotplot_diag(infl$cooksd, name = "cooks.distance")
}
########################################################


########################
### xe vs. d and Q
########################



colors <- viridis(max(round(my_data$xq*10)-min(round(my_data$xq*10))+1))[round(my_data$xq*10)-min(round(my_data$xq*10))+1]


plot(y~xd, pch=1,col=colors, xaxt="n", yaxt="n",xlab="",data=my_data,
     ylab=expression(paste(epsilon," ( ",m^2,s^-3,")")),xlim=log10(c(4,400)),ylim=log10(c(10^-6,10^3)))




# Unique sites
sites <- unique(my_data$s)

for (i in seq_along(sites)) {
  
  site_data <- subset(my_data, s == sites[i])
  
  x_seq <- seq(
    min(site_data$xd, na.rm = TRUE),
    max(site_data$xd, na.rm = TRUE),
    length.out = 100
  )
  
  # Prediction data
  newdata <- data.frame(
    xd = x_seq,
    c    = unique(site_data$c),
    s = sites[i],
    xq   = unique(site_data$xq),
    xh   = unique(site_data$xh),
    xl   = mean(site_data$xl),
    xc   = unique(site_data$xc),
    Temp = unique(site_data$Temp),
    pH   = unique(site_data$pH)
  )
  
  # Model-averaged prediction
  pred <- predict(
    AvgModel_epsISRtrust,
    newdata = newdata,
    level = 2
  )
  
  lines(
    x_seq,
    pred,
    col = colors[which(my_data$s == sites[i])[1]],
    lwd = 1
  )
}



axis(side=1,at=(log10(c(seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10),seq(100,1000,100)))),labels=NA)
axis(side=1,at=(log10(c(0.001,0.01,0.1,1,10,100,1000))),lwd=2,labels=c(0.001,0.01,0.1,1,10,100,1000))

axis(side=2,at=(log10(c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1,10,100))),labels=c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1,10,100),lwd=2)
axis(side=2,at=(log10(c(seq(1,10,1),seq(10,100,10),seq(100,1000,100))))*100000,labels=NA)
axis(side=2,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100))),lwd=2,labels=NA)

axis(side=2,at=log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100)),labels=c(10^-10,10^-9,10^8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100))
axis(side=2,at=log10(c(seq(1,10,1),seq(10,100,10),seq(100,1000,100)))*100000,labels=NA)


text(log10(0.4),log10(900),"C)",xpd=TRUE,font=2)

mtext("d (cm)", side=1, line=3)  # Move X-axis label further away



##########################################################################################################################################
### Micrubble abundance vs. turbulent kinetic energy dissipation based on inertial subrange method, 
### including only estimates that passed the quality check
##########################################################################################################################################


###################################
##### lme model n vs eps as function of q

y=log10(DataSpot$N_tot)


## selection
sel = which(y>-99999 & DataSpot$SNRU>15 & DataSpot$corrU>70 & DataSpot$epsU_isr/DataSpot$epsUmin_isr>3 & DataSpot$R2U_isr>0)



my_data=data.frame(y,xe,xq,xh,xd,xl,Temp,xc,s,c,pH)[sel,]

## select random effects
epsmod1 <- lme(y~xe+xd,random=~1|c/s,data=my_data)
epsmod2 <- lme(y~xe+xd,random=~xd|c/s,data=my_data)
epsmod3 <- lme(y~xe+xd,random=~xl|c/s,data=my_data)
anova(epsmod1,epsmod2)
VarCorr(epsmod2)
anova(epsmod1,epsmod3)
VarCorr(epsmod3)
## random slope improves model, but slope and intercept correlate highly, indicating overfitting. keep simple random intercept model


# remove influential points (see Cooks'd analysis below)
my_data <- my_data[-91,]


## best model
best_model_n_vs_epsISRtrust <- lme(y~xe+xd,random=~1|c/s,data=my_data)


## model output
summary(best_model_n_vs_epsISRtrust)
r.squaredGLMM(best_model_n_vs_epsISRtrust)


########################################################
# model evaluation
if(ModelEval==TRUE){
  E2 <- resid(best_model_n_vs_epsISRtrust , type = "normalized")
  
  xyplot(E2 ~ xd | s,
         data = my_data, ylab = "Residuals",
         xlab = "d",
         panel = function(x,y){
           panel.grid(h = -1, v = 2)
           panel.points(x, y, col = 1)
           panel.loess(x, y, span = 0.5, col = 1,lwd=2)})
  
  boxplot(E2 ~ xd, data = my_data,ylim = c(-3, 3))
  boxplot(E2 ~ xe, data = my_data,ylim = c(-3, 3))
  
  F2 <- fitted(best_model_n_vs_epsISRtrust)
  plot(x = F2, y = E2, xlab = "Fitted values")
  plot(x = my_data$xd, y = E2, xlab = "Fitted values")
  plot(x = my_data$xe, y = E2, xlab = "Fitted values")
  
  ## Cook's Distance to identify influential points
  infl <- hlm_influence(best_model_n_vs_epsISRtrust)
  infl_sorted <- infl[order(infl$cooksd, decreasing = TRUE), ]
  # View Cook's Distance
  dotplot_diag(infl$cooksd, name = "cooks.distance")
}
########################################################



colors <- inferno(max(round(my_data$xd*10)-min(round(my_data$xd*10))+1))[round(my_data$xd*10)-min(round(my_data$xd*10))+1]

plot(y~xe, pch=1, col=colors, xaxt="n", yaxt="n",xlab="",data=my_data,
     ylab=expression(paste(N[tot]," ( ",m^-3,")")),xlim=log10(c(10^-6,10^3)),ylim=log10(c(5*10^4,3*10^8)))


# Create a sequence of Q values for prediction
xe_seq <- seq(min(my_data$xe), max(my_data$xe), length.out = 155)

# Generate a new data frame with mean values for other predictors; d=0.1 m
pred_data <- data.frame(
  xe = xe_seq,
  xq = mean(my_data$xq, na.rm = TRUE),
  xh = mean(my_data$xh, na.rm = TRUE),
  xd = log10(10),
  xl = mean(my_data$xl, na.rm = TRUE),
  xc = mean(my_data$xc, na.rm = TRUE)
)
# Get predictions
pred_data$predicted <- predict(best_model_n_vs_epsISRtrust, newdata = pred_data,level=0)
lines(xe_seq, pred_data$predicted, col = inferno(8)[2], lwd = 2)  # Add line

# Generate a new data frame with mean values for other predictors; d=0.3 m
pred_data <- data.frame(
  xe = xe_seq,
  xq = mean(my_data$xq, na.rm = TRUE),
  xh = mean(my_data$xh, na.rm = TRUE),
  xd = log10(50),
  xl = mean(my_data$xl, na.rm = TRUE),
  xc = mean(my_data$xc, na.rm = TRUE)
)
# Get predictions
pred_data$predicted <- predict(best_model_n_vs_epsISRtrust, newdata = pred_data,level=0)
lines(xe_seq, pred_data$predicted, col = inferno(8)[3], lwd = 2)  # Add line

# Generate a new data frame with mean values for other predictors; d=0.5 m
pred_data <- data.frame(
  xe = xe_seq,
  xq = mean(my_data$xq, na.rm = TRUE),
  xh = mean(my_data$xh, na.rm = TRUE),
  xd = log10(100),
  xl = mean(my_data$xl, na.rm = TRUE),
  xc = mean(my_data$xc, na.rm = TRUE)
)
# Get predictions
pred_data$predicted <- predict(best_model_n_vs_epsISRtrust, newdata = pred_data,level=0)
lines(xe_seq, pred_data$predicted, col = inferno(8)[5], lwd = 2)  # Add line

# Generate a new data frame with mean values for other predictors; d=1 m
pred_data <- data.frame(
  xe = xe_seq,
  xq = mean(my_data$xq, na.rm = TRUE),
  xh = mean(my_data$xh, na.rm = TRUE),
  xd = log10(200),
  xl = mean(my_data$xl, na.rm = TRUE),
  xc = mean(my_data$xc, na.rm = TRUE)
)
# Get predictions
pred_data$predicted <- predict(best_model_n_vs_epsISRtrust, newdata = pred_data,level=0)
lines(xe_seq, pred_data$predicted, col = inferno(8)[6], lwd = 2)  # Add line



axis(side=1,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100))),labels=c(10^-10,10^-9,10^8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100))
axis(side=1,at=(log10(c(seq(1,10,1),seq(10,100,10),seq(100,1000,100))))*100000,labels=NA)
axis(side=1,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10,100,1000,10000))),lwd=2,labels=NA)


axis(side=2,at=(log10(c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1,10))),labels=c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1,10),lwd=2)
axis(side=2,at=(log10(c(seq(0.00000001,0.0000001,0.00000001),seq(0.0000001,0.000001,0.0000001),seq(0.000001,0.00001,0.000001),seq(0.00001,0.0001,0.00001),seq(0.0001,0.001,0.0001),seq(0.001,0.01,0.001),seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10),seq(100,1000,100))*1000000)),labels=NA)
axis(side=2,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1,10))),lwd=2,labels=NA)


text(log10(3*10^-11),max(y,na.rm=TRUE)*0.99,"D)",xpd=TRUE,font=2)


mtext(expression(paste(epsilon," ( ",m^2,s^-3,")")), side=1, line=3)  # Move X-axis label further away










