# loading in collocated in situ nitrate concentration measurements and remote # sensing/reanalysis predictor variables to generate generalized additive # models (GAMs) for the prediction of nitrate # Clear all plots and workspace variables; set working directory graphics.off(); rm(list = ls()); setwd("..."); # set directory here library(mgcv); # load in mgcv package for GAMs # ----------------------------- surface GAM ---------------------------------- # loading in data xy_surf <- read.csv("RB2025_no3surf.csv"); # set parameters for GAM here alpha1 <- 0.05; pf1 <- 1.3; kn1 <- 10; kn2 <- 5; kn3 <- 4; data1 <- xy_surf; # make the model model1 <- gam(no3_surf..uM. ~ s(sst_0_lead..deg.C., k = kn2) + s(tau_15_lead..Pa., k = kn3) + s(log_chl_5_lag..mg.m.3., k = kn1) + s(xoff..km., k = kn1) + s(dx..km., k = kn1) + s(dy..km., k = kn1) + s(doy, k = kn2), family = Tweedie(p = pf1, link = "log"), data = data1) # plot model (show partial dependence plots) plot(model1) # this model can be used to predict nitrate at the surface as a function of the # above predictor variables using the "predict.gam" function # ---------------------------------------------------------------------------- # --------------------------------- MLD GAM ---------------------------------- # loading in data xy_mld <- read.csv("RB2025_no3mld.csv"); # set parameters for GAM here alpha1 <- 0.05; pf1 <- 1.3; kn1 <- 10; kn2 <- 5; kn3 <- 4; data2 <- xy_mld; # make the model model2 <- gam(no3_mld..uM. ~ s(sst_0_lead..deg.C., k = kn2) + s(tau_15_lead..Pa., k = kn3) + s(log_chl_5_lag..mg.m.3., k = kn1) + s(xoff..km., k = kn1) + s(dx..km., k = kn1) + s(dy..km., k = kn1) + s(doy, k = kn2), family = Tweedie(p = pf1, link = "log"), data = data2) # plot model (show partial dependence plots) plot(model2) # this model can be used to predict nitrate at the mixed layer depth as a # function of the above predictor variables using the "predict.gam" function # ----------------------------------------------------------------------------