####################################################################################################
### Code to generate a table that summarizes the output of linear mixed effects models
### used to identify drivers of bubble size distributions and turbulence in mountain streams
####################################################################################################

# Creates Table S5 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/11
###########################################################################################

## load libraries
library(r2glmm)
library(MuMIn)


TableS5a <- data.frame(summary(AvgModel_epsISRall)$coefficients[1,],confint(AvgModel_epsISRall),c(NA,sw(AvgModel_epsISRall)),weighted_r2m_epsISRall,weighted_r2c_epsISRall)
TableS5b <- data.frame(summary(best_model_n_vs_epsISRall)$coefficients$fixed,intervals(best_model_n_vs_epsISRall)$fixed[,c(1,3)],c(NA,NA,NA),r.squaredGLMM(best_model_n_vs_epsISRall)[1],r.squaredGLMM(best_model_n_vs_epsISRall)[2])
TableS5c <- data.frame(summary(AvgModel_epsISRtrust)$coefficients[1,],confint(AvgModel_epsISRtrust),c(NA,sw(AvgModel_epsISRtrust)),weighted_r2m_epsISRtrust,weighted_r2c_epsISRtrust)
TableS5d <- data.frame(summary(best_model_n_vs_epsISRtrust)$coefficients$fixed,intervals(best_model_n_vs_epsISRtrust)$fixed[,c(1,3)],c(NA,NA,NA),r.squaredGLMM(best_model_n_vs_epsISRtrust)[1],r.squaredGLMM(best_model_n_vs_epsISRtrust)[2])

colnames(TableS5a) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS5b) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS5c) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS5d) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")

TableS5 <- rbind(TableS5a,TableS5b,TableS5c,TableS5d)




TableS5$predictor <- gsub("[0-9]", "", rownames(TableS5))
rownames(TableS5) <- NULL


TableS5 <- data.frame(TableS5$predictor,TableS5[,-grep("predictor",names(TableS5))])
names(TableS5)[1] <- "predictor"


TableS5$predictor <- gsub("xh","log10(h)",TableS5$predictor)
TableS5$predictor <- gsub("xq","log10(Q)",TableS5$predictor)
TableS5$predictor <- gsub("xd","log10(d)",TableS5$predictor)
TableS5$predictor <- gsub("xe","log10(epsilon)",TableS5$predictor)
TableS5$predictor <- gsub("xc","log10(EC)",TableS5$predictor)
TableS5$predictor <- gsub("xl","log10(l)",TableS5$predictor)
TableS5$predictor <- gsub("(Intercept)","Intercept",TableS5$predictor)



TableS5$predicted <- c("log10(epsilon)","","","","","","","",
                      "log10(Ntot)","","",
                      "log10(epsilon)","","","","",
                      "log10(Ntot)","","")
TableS5 <- data.frame(TableS5[,8],TableS5[,1:7])

colnames(TableS5)[c(1,2)] <- c("Predicted","Predictor")


print(TableS5)





