####################################################################################################
### 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 S2 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(MuMIn)

## make Table 1

TableS2a <- data.frame(summary(AvgModel_nmicro)$coefficients[1,],confint(AvgModel_nmicro),c(NA,sw(AvgModel_nmicro)),weighted_r2m_nmicro,weighted_r2c_nmicro)
TableS2b <- data.frame(summary(AvgModel_eps)$coefficients[1,],confint(AvgModel_eps),c(NA,sw(AvgModel_eps)),weighted_r2m_eps,weighted_r2c_eps)
TableS2c <- data.frame(summary(best_model_n_vs_eps)$coefficients$fixed,intervals(best_model_n_vs_eps)$fixed[,c(1,3)],c(NA,NA,NA),r.squaredGLMM(best_model_n_vs_eps)[1],r.squaredGLMM(best_model_n_vs_eps)[2])
TableS2d <- data.frame(summary(AvgModel_alpha)$coefficients[1,],confint(AvgModel_alpha),c(NA,sw(AvgModel_alpha)),weighted_r2m_alpha,weighted_r2c_alpha)
TableS2e <- data.frame(summary(AvgModel_beta)$coefficients[1,],confint(AvgModel_beta),c(NA,sw(AvgModel_beta)),weighted_r2m_beta,weighted_r2c_beta)
TableS2f <- data.frame(summary(AvgModel_aHemp)$coefficients[1,],confint(AvgModel_aHemp),c(NA,sw(AvgModel_aHemp)),weighted_r2m_aHemp,weighted_r2c_aHemp)
TableS2g <- data.frame(summary(AvgModel_Wec)$coefficients[1,],confint(AvgModel_Wec),c(NA,sw(AvgModel_Wec)),weighted_r2m_Wec,weighted_r2c_Wec)

colnames(TableS2a) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS2b) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS2c) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS2d) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS2e) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS2f) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")
colnames(TableS2g) <- c("Estimate","Lower 95% CI bound","Upper 95% CI bound","SW","R2m","R2c")

TableS2 <- rbind(TableS2a,TableS2b,TableS2c,TableS2d,TableS2e,TableS2f,TableS2g)


TableS2$predictor <- gsub("[0-9]", "", rownames(TableS2))
rownames(TableS2) <- NULL


TableS2$predictor <- gsub("xh","log10(h)",TableS2$predictor)
TableS2$predictor <- gsub("xq","log10(Q)",TableS2$predictor)
TableS2$predictor <- gsub("xd","log10(d)",TableS2$predictor)
TableS2$predictor <- gsub("xe","log10(epsilon)",TableS2$predictor)
TableS2$predictor <- gsub("xc","log10(EC)",TableS2$predictor)
TableS2$predictor <- gsub("xl","log10(l)",TableS2$predictor)
TableS2$predictor <- gsub("(Intercept)","Intercept",TableS2$predictor)

TableS2 <- data.frame(TableS2[,7],TableS2[,1:6])

TableS2$predicted <- c("log10(Ntot)","","","","","","",
                      "log10(epsilon)","","","","","","","",
                      "log10(Ntot)","","",
                      "alpha","","","","","","",
                      "beta","","","","","","","",
                      "log10(aH_app)","","","","","","","",
                      "log10(Wec_app)","","","")
TableS2 <- data.frame(TableS2[,8],TableS2[,1:7])

colnames(TableS2)[c(1,2)] <- c("Predicted","Predictor")
print(TableS2)





