deVriesSchultink_2018_trastuzumab_LVEF <- function() {
  description <- paste(
    "Effect-compartment PD model for left-ventricular ejection fraction",
    "(LVEF) decline during adjuvant trastuzumab treatment in",
    "HER2-positive early breast cancer (de Vries Schultink 2018).",
    "Trastuzumab pharmacokinetics are an inlined deterministic forcing",
    "function from the previously published Bruno 2005 two-compartment",
    "linear popPK (de Vries Schultink 2018 Methods: 'The trastuzumab PK",
    "profiles were obtained using fixed effect parameters from a",
    "previously published PK model for HER2-positive breast cancer",
    "patients [18]'); typical population values plus WT / HER2_ECD /",
    "MET_GE4 covariate effects are retained from Bruno 2005 Table 3.",
    "Cardiac damage is generated by cumulative trastuzumab concentration",
    "via an effect compartment Ceff that integrates plasma Ctrastuzumab",
    "and decays at rate log(2)/T1/2rec; LVEF declines through a sigmoid",
    "Emax expression LVEF = LVEF0 * (1 - Ceff / (Ceff + EC50)), and the",
    "EC50 is modulated by the per-subject peak post-anthracycline",
    "troponin T (TROPONIN_T_MAX), with a higher TROPONIN_T_MAX lowering",
    "EC50 and increasing sensitivity to trastuzumab-induced cardiotoxic",
    "decline. Companion file `deVriesSchultink_2018_anthracycline_troponinT.R`",
    "supplies the upstream K-PD anthracycline-troponin T model whose peak",
    "output is used here as a covariate."
  )
  reference <- paste(
    "de Vries Schultink AHM, Boekhout AH, Gietema JA, Burylo AM, Dorlo",
    "TPC, van Hasselt JGC, Schellens JHM, Huitema ADR. Pharmacodynamic",
    "modeling of cardiac biomarkers in breast cancer patients treated",
    "with anthracycline and trastuzumab regimens. J Pharmacokinet",
    "Pharmacodyn. 2018;45(3):431-442. doi:10.1007/s10928-018-9579-8.",
    "Trastuzumab PK structure and parameter values inherited from",
    "Bruno R, Washington CB, Lu JF, Lieberman G, Banken L, Klein P.",
    "Population pharmacokinetics of trastuzumab in patients with HER2+",
    "metastatic breast cancer. Cancer Chemother Pharmacol.",
    "2005;56(4):361-369. doi:10.1007/s00280-005-1026-z (see also the",
    "registered `Bruno_2005_trastuzumab.R` model file for full",
    "PK provenance)."
  )
  vignette <- "deVriesSchultink_2018_cardiotoxicity"
  units <- list(time = "day", dosing = "mg", concentration = "mg/L")

  # Issue #482: what each ODE state holds, in what amount units, in what
  # biological matrix. analyte/specimen proposed by a local model from the
  # model description; units derived from the units block. verified = FALSE
  # means NOT checked against the source paper.
  compartmentData <- list(
    central     = list(analyte = "trastuzumab", units = "mg", specimen = "plasma", verified = FALSE),
    peripheral1 = list(analyte = "trastuzumab", units = "mg", specimen = "plasma", verified = FALSE),
    effect      = list(analyte = "cardiac damage", units = "mg", specimen = "not applicable", verified = FALSE)
  )

  covariateData <- list(
    WT = list(
      description        = "Baseline body weight",
      units              = "kg",
      type               = "continuous",
      reference_category = NULL,
      notes              = "Time-fixed baseline value. Inherited from Bruno 2005: power effect on central volume V with exponent 0.556, reference 70 kg. No retained WT effect on linear CL in Bruno 2005's final model.",
      source_name        = "WT"
    ),
    HER2_ECD = list(
      description        = "Baseline serum HER2 shed extracellular domain (ECD) concentration",
      units              = "ng/mL",
      type               = "continuous",
      reference_category = NULL,
      notes              = "Time-fixed baseline. Inherited from Bruno 2005: power effects on linear CL (exponent 0.041) and on V (exponent 0.105), reference 8.23 ng/mL. ECD is capped at 200 ng/mL inside the model() block before the power law is applied, following Bruno 2005's observation that ECD levels above 200 ng/mL did not drive further increases in clearance. The de Vries Schultink 2018 cohort comprised early-breast-cancer patients (vs Bruno 2005's MBC cohort); ECD distribution in that cohort is not tabulated and is taken from Bruno 2005 for the purpose of reproducing the Bruno 2005-derived typical trastuzumab profile.",
      source_name        = "ECD"
    ),
    MET_GE4 = list(
      description        = "Indicator of baseline number of metastatic sites >= 4",
      units              = "(binary)",
      type               = "binary",
      reference_category = "0 (fewer than four metastatic sites at baseline)",
      notes              = "Inherited from Bruno 2005: multiplicative-additive effect on linear CL: CL_typ = CL_pop * (1 + 0.221 * MET_GE4). For the de Vries Schultink 2018 early-breast-cancer cohort this covariate is effectively 0 for all patients (no metastatic disease at the time of adjuvant trastuzumab treatment), but the parameter is retained to keep the PK forcing function consistent with the registered Bruno 2005 trastuzumab model.",
      source_name        = "MET"
    ),
    TROPONIN_T_MAX = list(
      description        = "Peak post-anthracycline high-sensitive serum troponin T concentration",
      units              = "ng/L",
      type               = "continuous",
      reference_category = NULL,
      notes              = "Time-fixed per subject (a determined historical scalar by the time the LVEF observation period begins). Power covariate effect on EC50 with exponent -1.16, median-centered at 18 ng/L: EC50_i = EC50_pop * (TROPONIN_T_MAX / 18)^(-1.16). A higher peak troponin T lowers EC50, i.e. increases sensitivity to trastuzumab-induced LVEF decline. Per de Vries Schultink 2018 covariate-effect equation and Table 2: a TROPONIN_T_MAX of 31 ng/L is associated with a twofold lower EC50 relative to the cohort-median patient. Inclusion of TROPONIN_T_MAX reduced the EC50 inter-individual variability from 98.0% in the base model to 82.9% (15.1% of EC50 IIV explained). TROPONIN_T_MAX is the per-subject output peak from the companion `deVriesSchultink_2018_anthracycline_troponinT.R` K-PD model.",
      source_name        = "TRPmax"
    )
  )

  population <- list(
    species        = "human",
    n_subjects     = 206L,
    n_studies      = 1L,
    n_observations = 1444L,
    age_range      = "25-69 years",
    age_median     = "50 years",
    weight_range   = NA_character_,
    weight_median  = NA_character_,
    sex_female_pct = 100,
    race_ethnicity = "Not explicitly tabulated in de Vries Schultink 2018; clinical-trial cohort recruited at Dutch sites (Antoni van Leeuwenhoek / NKI Amsterdam, University Medical Center Groningen).",
    disease_state  = "HER2-positive early (stage I-III) breast cancer; LVEF-evaluable cohort (n = 206) of the parent candesartan-vs-placebo cardioprotection trial (Boekhout 2016, JAMA Oncology 2:1030-1037, doi:10.1001/jamaoncol.2016.1726). All patients had favorable cardiac history at baseline (max age 69 y; only 13% > 60 y; LVEF >= 55% at entry).",
    dose_range     = "Adjuvant trastuzumab for 52 weeks following anthracycline therapy. Two regimens used: 3-weekly schedule (8 mg/kg loading then 6 mg/kg q3w, n = 62) and weekly schedule (4 mg/kg loading then 2 mg/kg weekly, n = 144), administered IV. Median 23 [5-46] trastuzumab cycles per subject. Patients were randomized 1:1 to receive concomitant candesartan or placebo from the first trastuzumab dose until 26 weeks after the last dose; candesartan did not affect LVEF model parameters (treatment-group covariate non-significant).",
    regions        = "Netherlands.",
    notes          = "Total 1444 LVEF measurements available (median 8 [2-9] per patient, 96% by MUGA scan, 4% by echocardiography). Clinically relevant LVEF decline defined as a drop below 0.45 or a 0.15 absolute decrement from baseline; 37/206 (18%) of patients experienced a clinically relevant decline during trastuzumab. Patient and measurement details from de Vries Schultink 2018 Table 1. The trastuzumab PK was simulated deterministically (Bruno 2005 fixed-effect typical values + covariates; no PK IIV); LVEF is the only observation with associated residual error and inter-individual variability."
  )

  ini({
    # Trastuzumab PK structural parameters (Bruno 2005 Table 3, inherited
    # via de Vries Schultink 2018 Methods). The Bruno 2005 paper
    # parameterised disposition with the microconstants K12 and K21 rather
    # than Q and Vp; the canonical reparameterisation is
    #     Q  = K12 * V1            = 0.164 /day * 2.95 L = 0.4838 L/day
    #     Vp = K12 * V1 / K21      = 0.164 * 2.95 / 0.101 = 4.7901 L
    # Reference subject: 70 kg, HER2_ECD = 8.23 ng/mL, MET_GE4 = 0.
    # All four structural PK THETAs are wrapped in fixed() because de Vries
    # Schultink 2018 used Bruno 2005's fixed effects without re-estimation.
    lcl  <- fixed(log(0.225));   label("Trastuzumab linear CL for the Bruno 2005 reference subject (L/day)") # Bruno 2005 Table 3, inherited
    lvc  <- fixed(log(2.95));    label("Trastuzumab central volume V1 for the Bruno 2005 reference subject (L)") # Bruno 2005 Table 3, inherited
    lq   <- fixed(log(0.4838));  label("Trastuzumab inter-compartmental Q (L/day) = K12 * V1")              # Bruno 2005 Table 3, inherited; K12 = 0.164 /day, V1 = 2.95 L
    lvp  <- fixed(log(4.7901));  label("Trastuzumab peripheral volume Vp (L) = K12 * V1 / K21")              # Bruno 2005 Table 3, inherited; K21 = 0.101 /day

    # Bruno 2005 covariate effects on trastuzumab PK (also inherited
    # fixed). These are kept so that the per-subject trastuzumab profile
    # is consistent with Bruno 2005 even when WT / HER2_ECD vary; for the
    # de Vries Schultink 2018 cohort MET_GE4 is effectively 0 throughout.
    e_met_ge4_cl  <- fixed(0.221); label("Bruno 2005 MET_GE4 multiplicative-additive on linear CL (unitless)") # Bruno 2005 Table 3, MET on CL
    e_her2_ecd_cl <- fixed(0.041); label("Bruno 2005 HER2_ECD power exponent on linear CL (unitless; reference 8.23 ng/mL, ECD capped at 200 ng/mL)") # Bruno 2005 Table 3, ECD on CL
    e_wt_vc       <- fixed(0.556); label("Bruno 2005 WT power exponent on central volume V (unitless; reference 70 kg)")          # Bruno 2005 Table 3, WT on V
    e_her2_ecd_vc <- fixed(0.105); label("Bruno 2005 HER2_ECD power exponent on V (unitless; reference 8.23 ng/mL, ECD capped at 200 ng/mL)") # Bruno 2005 Table 3, ECD on V

    # LVEF effect-compartment PD parameters (de Vries Schultink 2018
    # Table 2, trastuzumab-LVEF model rows). Equations from p. 438:
    #   dCeff/dt = Ctrastuzumab - log(2)/T1/2rec * Ceff
    #   LVEF = LVEF0 * (1 - Ceff / (Ceff + EC50))
    # The PD parameters are estimated, not fixed.
    llvef0   <- log(0.599);   label("LVEF baseline (fraction; pre-trastuzumab)")                    # Table 2: LVEF0 = 0.599, RSE 0.6%
    lthalfrec <- log(67.9);   label("LVEF recovery half-life T1/2rec (day)")                         # Table 2: T1/2rec = 67.9 days, RSE 17.2%
    lec50    <- log(2.18e5);  label("Trastuzumab effect-compartment sensitivity EC50 (mg/L)")        # Table 2: EC50 = 2.18e5 mg/L, RSE 23.4%

    # Covariate effect of TROPONIN_T_MAX (peak post-anthracycline
    # troponin T) on EC50: power covariate with median-centered
    # normalisation (median = 18 ng/L). The exponent -1.16 means higher
    # troponin T peaks lower EC50 (increase sensitivity).
    e_troponin_t_max_ec50 <- -1.16; label("TROPONIN_T_MAX power exponent on EC50 (unitless; reference median 18 ng/L)") # Table 2: maximum troponin T effect on EC50 = -1.16, RSE 23.4%

    # Inter-individual variability (de Vries Schultink 2018 Table 2;
    # CV%). Block correlation between LVEF0 and EC50 with r = 0.585.
    # omega^2 = log(CV^2 + 1). Off-diagonal cov = r * sqrt(var1 * var2).
    #   omega^2_LVEF0 = log(0.0707^2 + 1) = 0.0049745
    #   omega^2_EC50  = log(0.829^2 + 1)  = 0.5091924
    #   cov           = 0.585 * sqrt(0.0049745 * 0.5091924) = 0.029439
    # No IIV reported on T1/2rec (paper: BSV could not be identified for
    # the recovery; not included in the final model).
    etallvef0 + etalec50 ~ c(0.0049745,
                             0.029439, 0.5091924)                                                    # Table 2: omega_LVEF0 = 7.07% CV, omega_EC50 = 82.9% CV, correlation 0.585

    # Residual error on LVEF: proportional 7.8% (Table 2, shrinkage
    # 8.3%). Single PD output -> use the bare `propSd` form. No residual
    # error on the deterministic trastuzumab Cc (the PK is a fixed-effect
    # forcing function, per de Vries Schultink 2018 Methods).
    propSd <- 0.078; label("Proportional residual SD on LVEF (fraction)")                            # Table 2: proportional residual error LVEF = 7.8%
  })

  model({
    # ---------------------------------------------------------------------
    # Trastuzumab PK (Bruno 2005 inherited; no IIV per de Vries Schultink
    # 2018 Methods 'fixed effect parameters'). The HER2_ECD cap at 200
    # ng/mL reproduces Bruno 2005's saturation observation.
    # ---------------------------------------------------------------------
    her2_ecd_eff <- min(HER2_ECD, 200)

    # Typical CL and V at the reference (Bruno 2005 covariate equations,
    # no eta added because trastuzumab PK is deterministic in this model):
    cl  <- exp(lcl) *
             (her2_ecd_eff / 8.23)^e_her2_ecd_cl *
             (1 + e_met_ge4_cl * MET_GE4)
    vc  <- exp(lvc) *
             (WT / 70)^e_wt_vc *
             (her2_ecd_eff / 8.23)^e_her2_ecd_vc
    q   <- exp(lq)
    vp  <- exp(lvp)

    kel <- cl / vc
    k12 <- q  / vc
    k21 <- q  / vp

    # Trastuzumab plasma concentration in the central compartment (mg/L
    # = ug/mL since dose in mg and vc in L). Used as the forcing input to
    # the LVEF effect compartment.
    Cc <- central / vc

    d/dt(central)     <- -kel * central - k12 * central + k21 * peripheral1
    d/dt(peripheral1) <-                   k12 * central - k21 * peripheral1

    # ---------------------------------------------------------------------
    # LVEF effect-compartment PD layer (de Vries Schultink 2018 eqs p. 438):
    #   dCeff/dt = Ctrastuzumab - log(2)/T1/2rec * Ceff
    #   LVEF     = LVEF0 * (1 - Ceff / (Ceff + EC50))
    #
    # Note on units: the dCeff equation has NO rate constant on the
    # Ctrastuzumab input, so Ceff has units of (mg/L) * day. EC50 must
    # carry the same compound unit for the Hill expression to be
    # dimensionally consistent; the paper labels EC50 in mg/L (Table 2),
    # but the math requires mg.day/L. With Ctrastuzumab_ss approximately
    # 50-100 mg/L and krec = log(2)/67.9 = 0.0102/day, Ceff_ss is
    # approximately 5,000-10,000 mg.day/L; EC50 = 2.18e5 then gives a
    # small fractional LVEF decline consistent with the observed
    # clinically-relevant decline threshold of 0.15 from baseline. The
    # unit-labelling subtlety is documented in vignette Assumptions and
    # deviations.
    # ---------------------------------------------------------------------
    lvef0   <- exp(llvef0 + etallvef0)
    krec    <- log(2) / exp(lthalfrec)
    ec50_i  <- exp(lec50 + etalec50) *
                 (TROPONIN_T_MAX / 18)^e_troponin_t_max_ec50

    d/dt(effect) <- Cc - krec * effect

    LVEF <- lvef0 * (1 - effect / (effect + ec50_i))

    LVEF ~ prop(propSd)
  })
}
