model {
# Noisy Input for ni_gam_decomp
        for (i in 1:n_obs) {
          # Main likelihood
          y[i] ~ dnorm(mu_y[i], sigmasq_all[i]^-1)
          sigmasq_all[i] <- sigma_y^2 + y_err[i]^2 + NI_var_term[i]^2

          # Mean structure
          mu_y[i] <- r[i] + g_z_x[i] + h_z_x[i] + l[i]

          # Derivative for the Total component
          mu_deriv[i] <- r_deriv[i] + l_deriv[i] + g_z_x_deriv[i]

          # Site specific vertical offset
          h_z_x[i] <- intercept[site[i]]

          # Linear Local component
          g_z_x[i] <- t[i] * b_g[site[i]]

          # Linear Local component + Site specific vertical offset
          g_h_z_x[i] <- h_z_x[i] + g_z_x[i]

          # Linear Local component
          g_z_x_deriv[i] <- b_g[site[i]]

          # Model residuals
          residuals[i] <- y[i] - mu_y[i]

        }

      # Informed Regional component
      r <- B_t %*% b_t
      for (j in 1:n_knots_t) {
        b_t[j] ~ dnorm(b_t_value[j], b_t_sd_value[j]^-2)
      }

      # Derivative for Regional Term
      r_deriv <- B_t_deriv %*% b_t

      # Prediction for Regional term
      r_pred <- B_t_pred %*% b_t


      # Derivative for Predicted Regional Term
      r_pred_deriv <- B_t_pred_deriv %*% b_t


      # Non-linear local component
      l <- B_st %*% b_st
      for (j in 1:n_knots_st) {
        b_st[j] ~ dnorm(0, sigma_beta_l^-2)
      #  #b_st[j] ~ dnorm(0, tau_st)
      }

      # Non-linear local component
      #l <- B_st %*% b_st
      ##b_st[1] ~ dnorm(0,0.1)
      #b_st[1] ~ dnorm(0,1)
      #for (j in 2:n_knots_st) {
      #  b_st[j] ~ dnorm(b_st[1], sigma_st^-2)
      #  #b_st[j] ~ dnorm(b_st[1], tau_st)
      #}

      # Derivative of Non-linear local
      l_deriv <- B_st_deriv %*% b_st

      # Non-linear local component on prediction grid
      l_pred <- B_st_pred %*% b_st

      # Derivative of Non-linear local on prediction grid
      l_pred_deriv <- B_st_deriv_pred %*% b_st

      # Prior on intercept
      for (j in 1:n_sites) {
        intercept[j] ~ dnorm(h_value[j], h_sd_value[j]^-2)
      }

      # Linear Local component
      for (j in 1:n_sites) {
        b_g[j] ~ dnorm(linear_rate[j], linear_rate_err[j]^-2)
      }

    # Total for the prediction grid
    for(i in 1: n_pred){
        # Site specific vertical offset
        h_z_x_pred[i] <- intercept[site_pred[i]]

        # Linear Local component
        g_z_x_pred[i] <- t_pred[i] * b_g[site_pred[i]]
        # Linear Local component Derivative
        g_z_x_pred_deriv[i] <- b_g[site_pred[i]]

        # Linear Local component + Site specific vertical offset
        g_h_z_x_pred[i] <- h_z_x_pred[i] + g_z_x_pred[i]

        # Mean structure
        mu_pred[i] <- r_pred[i] + l_pred[i] + g_z_x_pred[i] + h_z_x_pred[i]
        # Creating prediction intervals without uncertainty
        sigmasq_all_pred[i] <- sigma_y^2
        y_pred[i] ~ dnorm(mu_pred[i], sigmasq_all_pred[i]^-1)

        # Derivative for the Total component
        mu_pred_deriv[i] <- r_pred_deriv[i] + l_pred_deriv[i] + g_z_x_pred_deriv[i]


    }

    # Priors
    sigma_beta_l ~ dt(0, 1^-2, 1)T(0,)
    sigma_y ~ dt(0, 1^-2, 1)T(0,)
    sigma_beta_r <- b_t_sd_value
    sigma_beta_h <- h_sd_value

  }
