Project: s41467-024-53095-7 | Function: mean_trajectory_SEIR (r)

system: You are an expert R programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow R best practices and maintain consistent code style.
Return **only** the complete R function code.

user: Complete this R function:

**DESCRIPTION:** Advanced numerical integration method for computing mean epidemic trajectories with history-dependent transmission dynamics and distributed delays using Gamma distributions.

**KEY TERMS:** {
  "timespan": "time vector for integration",
  "theta": "epidemiological parameters",
  "y_init": "initial conditions",
  "conv_val": "convolution values for delays"
}

**CONTEXT CODE:** library(deSolve)
library(MASS)
library(mvtnorm)
library(dplyr)
library(invgamma)
library(ramcmc)

# Core SEIR differential equation system for epidemiological modeling
SEIR11 <- function(t, s, theta) {
  stop("Function implementation has been masked")
}

mean_trajectory_SEIR <- function(timespan, theta, y_init){
  stop("Function implementation has been masked")
}


mean_trajectory_SEIR_dist_input <- function(timespan, theta, y_init, dist_type){
  beta.param = theta[1]
  alpha1 = theta[2]
  beta1 = theta[3]
  alpha2 = theta[4]
  beta2 = theta[5]

  Ntot = sum(y_init)

  h = timespan[2] - timespan[1]
  St = rep(NA, length(timespan))
  Et = rep(NA, length(timespan))
  It = rep(NA, length(timespan))
  Rt = rep(NA, length(timespan))
  conv_val = rep(NA, length(timespan))

  # hs = rep(NA, length(timespan))
  # hs_tilde = rep(NA, length(timespan))
  St[1] = y_init[1]
  Et[1] = y_init[2]
  It[1] = y_init[3]
  Rt[1] = y_init[4]

  for(ii in 1:length(timespan)){
    conv_val[ii] = conv_two_pdfs(tmax = timespan[ii], theta1 = c(alpha1, beta1), theta2 = c(alpha2, beta2), dist_type = dist_type)
  }

  for(ii in 1:(length(timespan)-1)){

    term1 = beta.param * St[ii] * It[ii] / Ntot
    term2 = beta.param * St[1] * It[1] / Ntot * eval_density(x = timespan[ii], theta = c(alpha1, beta1), dist_type = dist_type, density_type = "cdf") +
      numer_int(timespan[1:ii], beta.param * diff_midpoint_extension(St[1:ii] * It[1:ii], h) / Ntot * eval_density(x = max(timespan[1:ii]) - timespan[1:ii], theta = c(alpha1, beta1), dist_type = dist_type, density_type = "cdf")) +
      Et[1] * eval_density(x = timespan[ii], theta = c(alpha1, beta1), dist_type = dist_type, density_type = "pdf")
    term3 = numer_int(timespan[1:ii], beta.param * St[1:ii] * It[1:ii] / Ntot * conv_val[ii:1]) +  It[1] * eval_density(x = timespan[ii], theta = c(alpha2, beta2), dist_type = dist_type, density_type = "pdf") + Et[1] * conv_val[ii]

    term1 = max(term1, 0) # adjusted to avoid negative values
    term2 = max(term2, 0) # adjusted to avoid negative values
    term3 = max(term3, 0) # adjusted to avoid negative values

    S_increm = - term1
    E_increm = term1 - term2
    I_increm = term2 - term3
    R_increm = term3

    St_tilde = St[ii] + h*S_increm
    Et_tilde = Et[ii] + h*E_increm
    It_tilde = It[ii] + h*I_increm
    Rt_tilde = Rt[ii] + h*R_increm

    term1_tilde = beta.param * St_tilde * It_tilde / Ntot
    term2_tilde = beta.param * St[1] * It[1] / Ntot * eval_density(x = timespan[ii+1], theta = c(alpha1, beta1), dist_type = dist_type, density_type = "cdf") +
      numer_int(timespan[1:(ii+1)], beta.param * diff_midpoint_extension(c(St[1:ii], St_tilde) * c(It[1:ii], It_tilde), h) / Ntot * eval_density(x = max(timespan[1:(ii+1)]) - timespan[1:(ii+1)], theta = c(alpha1, beta1), dist_type = dist_type, density_type = "cdf")) +
      Et[1] * eval_density(x = timespan[ii+1], theta = c(alpha1, beta1), dist_type = dist_type, density_type = "pdf")
    term3_tilde = numer_int(timespan[1:(ii+1)], beta.param * c(St[1:ii], St_tilde) * c(It[1:ii], It_tilde) / Ntot * conv_val[(ii+1):1])  + It[1] * eval_density(x = timespan[ii+1], theta = c(alpha2, beta2), dist_type = dist_type, density_type = "pdf") + Et[1] * conv_val[ii+1]

    term1_tilde = max(term1_tilde, 0) # adjusted to avoid negative values
    term2_tilde = max(term2_tilde, 0) # adjusted to avoid negative values
    term3_tilde = max(term3_tilde, 0) # adjusted to avoid negative values

    S_increm_tilde = - term1_tilde
    E_increm_tilde = term1_tilde - term2_tilde
    I_increm_tilde = term2_tilde - term3_tilde
    R_increm_tilde = term3_tilde

    St[ii+1] = St[ii] + h/2 * (S_increm + S_increm_tilde)
    Et[ii+1] = Et[ii] + h/2 * (E_increm + E_increm_tilde)
    It[ii+1] = It[ii] + h/2 * (I_increm + I_increm_tilde)
    Rt[ii+1] = Rt[ii] + h/2 * (R_increm + R_increm_tilde)

  }
  return(list("St" = St, "Et" = Et, "It" = It, "Rt" = Rt))
}


mean_trajectory_LARGE_dist_input <- function(timespan, param_set, y_init, dist_type = "gamma"){
  beta = param_set$beta
  h_var = param_set$h_var
  e_asym = param_set$e_asym
  e_vacc = param_set$e_vacc
  vacc = param_set$vacc
  rec = param_set$rec
  death = param_set$death
  p_A = param_set$p_A

  k_tauL = param_set$k_tauL
  s_tauL = param_set$s_tauL
  k_tauLh = param_set$k_tauLh
  s_tauLh = param_set$s_tauLh
  k_tauI = param_set$k_tauI
  s_tauI = param_set$s_tauI
  k_tauIA = param_set$k_tauIA
  s_tauIA = param_set$s_tauIA

  Stoi_mat = matrix(nrow = 11, ncol = 17, byrow = TRUE, data = c(-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,
                                                                 0,0,0,0,+1,-1,-1,0,0,0,0,0,0,0,0,0,0,


Complete the function implementation replacing "stop("Not implemented")".