L-BFGS-B Needs Finite Falue of 'fn' (In one step (step) and (step -1 +dstep))

27 Views Asked by At

I have tried data fitting to a model including their confidence interval, and it works smoothly without confidence interval.

However, when I want to include a confidence interval inside my plot it seems like the lower bounds of my confidence interval are problematic.

My code looked like this:

dat_sel <- dat_sel %>% 
  select(Mosquito_totaldissected, Human_mf_intensity_per20uL) %>% 
  arrange(Mosquito_totaldissected)

n_iter <- 1  # Number of iterations

# Create empty vectors to store the estimated parameters and confidence intervals
est_params <- matrix(0, nrow = n_iter, ncol = 3)
lower_bounds <- matrix(0, nrow = n_iter, ncol = 3)
upper_bounds <- matrix(0, nrow = n_iter, ncol = 3)

# Iterate the model
library(bbmle)
for (i in 1:n_iter) {
  total <- min(dat_sel$Mosquito_totaldissected):max(dat_sel$Mosquito_totaldissected)
  parasite <- rgamma(sum(dat_sel$Mosquito_totaldissected), 1, 1)
  probs <- SSlogis(parasite, Asym = 0.8, xmid = 1, scal = 0.1)
  count <- rbinom(sum(dat_sel$Mosquito_totaldissected), total, probs)
  
  LL <- function(Asym, xmid, scal) {
    -sum(dbinom(count, size = total, prob = SSlogis(parasite, Asym, xmid, scal), log = TRUE))
  }
  
  # starts <- list(list(Asym = 0.5, xmid = 1, scal = 0.1),
  # list(Asym = 0.7, xmid = 0.8, scal = 0.2),
  # list(Asym = 0.9, xmid = 1.2, scal = 0.3))
  
  
  result <- mle2(
    LL, method = "L-BFGS-B", 
    # control = list(maxit = 1000), # because error occurs
    lower = c(Asym = 0, xmid = -Inf, scal = 0.01),
    upper = c(Asym = 1, xmid = Inf, scal = Inf),
    start = list(Asym = 0.5, xmid = 1, scal = 0.1) # starts[[i]] # 
  )
  
  # Store the estimated parameters and confidence intervals for each iteration
  est_params[i, ] <- coef(result)
  conf_intervals <- confint(result)
  lower_bounds[i, ] <- conf_intervals[, "2.5 %"]
  upper_bounds[i, ] <- conf_intervals[, "97.5 %"]
}

# Calculate the mean of the estimated parameters and confidence intervals
mean_est_params <- apply(est_params, 2, mean)
mean_lower_bounds <- apply(lower_bounds, 2, mean)
mean_upper_bounds <- apply(upper_bounds, 2, mean)

# Generate a sequence of parasite counts
parasite_counts <- seq(0, 300, by = 0.1)

# Calculate the mean probabilities of infection based on the mean estimated parameters
mean_probs <- SSlogis(parasite_counts, Asym = mean_est_params["Asym"], xmid = mean_est_params["xmid"], scal = mean_est_params["scal"])

# Calculate the upper and lower bounds of the mean confidence interval for the probabilities of infection
mean_lower_probs <- SSlogis(parasite_counts, Asym = mean_lower_bounds["Asym"], xmid = mean_lower_bounds["xmid"], scal = mean_lower_bounds["scal"])
mean_upper_probs <- SSlogis(parasite_counts, Asym = mean_upper_bounds["Asym"], xmid = mean_upper_bounds["xmid"], scal = mean_upper_bounds["scal"])

# Plot the mean probabilities and the mean confidence interval
plot(parasite_counts, mean_probs, 
     ylim = c(0, 1),
     type = "l",
     xlab = "Parasite Count per 20uL", ylab = "Probability of Infection per-capita")
lines(parasite_counts, mean_lower_probs, lty = 2, col = "red")
lines(parasite_counts, mean_upper_probs, lty = 2, col = "red")

I tried some online solutions like creating some start points but it did not work and the error is still occuring:

Profiling...

Warning messages:
1: In onestep(step) :
  Error encountered in profile: Error in optim(par = c(Asym = 0.799893084851012, scal = 0.0995524248874847 : 
  L-BFGS-B needs finite values of 'fn'

2: In onestep(step - 1 + dstep) :
  Error encountered in profile: Error in optim(par = c(Asym = 0.799893084851012, scal = 0.0995524248874847 : 
  L-BFGS-B needs finite values of 'fn'
0

There are 0 best solutions below