Project: s41467-025-55873-3 | Function: neg_logLike_infection (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:** Calculates negative log-likelihood for infection probability model fitting. Implements maximum likelihood estimation for HIV infection dynamics incorporating censored survival data and inoculation timing.

**KEY TERMS:** {
  "prob_inoc_infect": "Probability of infection per inoculation",
  "week_of_infection": "Time to infection (weeks)",
  "infect_cens": "Infection censoring indicator",
  "loglike_animal": "Animal-specific log-likelihood"
}

**CONTEXT CODE:** #Steffen Docken
#14-3-23
#Script to contain codes used in CCR5_KO_fitting.R



neg_logLike_infection <- function(params, Infection_df, param.id){
  stop("Function implementation has been masked")
}


neg_logLike_infection_Arm <- function(params, Infection_df){
  Arm_ind_link <- c("100% CCR5-KO" = 1, "75% CCR5-KO" = 2, "50% CCR5-KO" = 3,
                      "25% CCR5-KO" = 4, "0% CCR5-KO" = 5)
  
  Infection_df =  Infection_df %>% mutate(Arm_ind = Arm_ind_link[Arm])
  
  Infection_df$prob_inoc_infect =
    prob_infect_Arm(params = params, Arm_ind = Infection_df$Arm_ind)
  #browser()
  Infection_df = Infection_df %>%
    mutate(loglike_animal = log((1-prob_inoc_infect)^(week_of_infection -infect_cens)) +
             log((prob_inoc_infect)^infect_cens)) #calculating log-likelihood of 
  #each animal getting infected at the observed time
  #browser()
  logLike_val = max(sum(Infection_df$loglike_animal), -1e8) #summing the animal specific
  #log-likelihoods. If logLikelihood evaluates to -Inf, this also sets it to -1e8
  #to avoid errors.
  if ((max(params)>1)|(min(params)<0)){
    logLike_val=-1e8 #ensuring probability of infection is between 0 and 1
  }
  
  return(-logLike_val)
  
}

fit_infection_prob_Arm <- function(Infection_df){
  
  Infection_df =  Infection_df %>% mutate(Arm_ind = Arm_ind_link[Arm])
  
  params = rep(0,5) #will contain probability of infection per inoculation for 
  #100% KO, 75%, etc.
  
  for (ii in 1:5) {
    Infection_df_it = filter(Infection_df, Arm_ind == ii)
    
    params[ii] = ifelse(sum(Infection_df_it$infect_cens) == 0,
                        0, #if no animals were infected
      1/(1 + sum(Infection_df_it$week_of_infection - 
                              Infection_df_it$infect_cens)/
                      sum(Infection_df_it$infect_cens)))
  }
  
  return(params)
  
}

fit_infection_prob_all <- function(Infection_df){
    
  avg_infect_risk = ifelse(sum(Infection_df$infect_cens) == 0,
                      0, #if no animals were infected
                      1/(1 + sum(Infection_df$week_of_infection - 
                                   Infection_df$infect_cens)/
                           sum(Infection_df$infect_cens)))
  
  params = rep(avg_infect_risk, 5) #repeating average infection risk 5 times to 
  # output optimization in consistent form.
  
  return(params)
  
}


prob_infect_week <- function(week_of_infection, prob_inoc_infect, infect_cens){
  #function to calculate the probability of getting infected on a specific week
  #(i.e., after 'week_of_infection' inoculations)
  #prob_inoc_infect is the probability of a single inoculation causing an infection
  #infect_cens: 1 infection detected, 0 censoring occured at current week
  
  
  Prob_infect_week = (1-prob_inoc_infect)^(week_of_infection -infect_cens)*
    (prob_inoc_infect)^infect_cens
  
  
  return(Prob_infect_week)
  
}

prob_infect_core <- function(theta, x) {
  # theta: Model parameters vector (c(p1, p2, ..., pn, exp_term))
  # x: CCR5 fraction or cell concentration

  # Assuming polynomial functional form (default)
  core_prob <- 1 - exp(-theta[1] * x - theta[2] * x^2)

  # Check if logarithmic functional form
  if (length(theta) > 3 && theta[3] != 0) {
    core_prob <- 1 - exp(-theta[1] * log(x) - theta[2] * x^2)
  }

  # Check if reciprocal term functional form
  if (length(theta) > 4 && theta[4] != 0) {
    core_prob <- 1 - exp(-theta[1] * x - theta[2] * x^2 - theta[3] * 1 / x)
  }

  # Exponential hazard function term


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