R Optimx error : Cannot evaluate function at initial parameters

54 views Asked by At

I have a project that involves estimating parameters for a Lee_Carter model (mx = exp(ax + bx *kt)). The aim is to predict future values of Life expectancy at birth (e0) without having the Kt parameter, my instructor told me I could use the optimx package to minimize e0_estimated - e0_expert (that I have values for), I first started to look for an equation for e0_estmated starting from the generic Lee_Carter Model which is why I've computed various conversions.

Whenever I run the optimization process using optimx, I keep getting an error message that says:

"Error in optimx.check(par, optcfg$ufn, optcfg$ugr, optcfg$uhess, lower, : Cannot evaluate function at initial parameters."

here's a shortened version of the code I'm working with

# Shortened matrices ax et bx
ax <- c(-4.3198094, -5.8995814, -6.1240039, -7.0870399, -7.0525417)
bx <- c(0.04522097, 0.02488256, 0.02452534, -3.2110186, -3.0955417)

data <- data.frame(
  DATE = as.Date(c("2019-01-01", "2020-01-01", "2021-01-01", "2022-01-01", "2023-01-01")),
  LE = matrix(c(77.76000, 77.05507, 76.34821, 75.64329, 74.93836), ncol = 1)
)
e0_expert <- data$LE

# Convert mx to qx
convert1 <- function(mx, qx, ax, bx, kt) {
  mx <- exp(ax + kt * bx)
  qx <- (2 * mx) / (2 + mx)
  return(qx)
}

# Calculate lx using qx
calculate_lx <- function(qx, l0 = 10000) {
  n <- length(qx)
  lx <- numeric(n) 
  lx[1] <- l0
  lx[2] <- l0 * (1 - qx[1])
  for (i in 2:n) {
    lx[i] <- lx[i - 1] * (1 - qx[i - 1])
  }
  return(lx)
}

# Convert lx to dx
calculate_dx <- function(dx, lx) {
  d0 <- l0 - lx[2]
  n <- length(lx)
  dx <- numeric(n)
  dx[1] <- d0
  for (i in 2:(n-1)) {
    dx[i] <- lx[i] - lx[i+1]
  }
  return(dx)
}

# Convert dx to Lx
calculate_Lx <- function(Lx, dx) {
  L0 <- d0 * lx[2] + 0.5
  n <- length(dx)
  Lx <- numeric(n)
  Lx[1] <- L0
  for (i in 2:n-1){
    Lx[i] <- dx[i] * lx[i+1] + 0.5
  }
  return(Lx)
}

# Convert qx to ex
convert2 <- function(Lx, lx, ex) {
  n <- length(Lx)
  ex <- numeric(n)
  ex <- sum(Lx) / lx
  e0_estime <- ex[1]
  return(e0_estime)
}

# Function to calculate the difference between the estimation and the projection
calculate_error <- function(kt, e0_expert, ax, bx) {
  e0_estime <- convert2(Lx, lx, e0_estime)
  error <- e0_estime - e0_expert
  error_value <- sum(error^2)
  cat("Error:", error_value, "\n")
  if (error_value < 50) { 
    cat("Converged to a solution: kt =", kt, "Error =", error_value, "\n")
  }
  return(error_value)
}

# Perform optimization using the defined function and parameters
result <- optimx(
  par = c(72.047557, 70.373062, 66.536728, 63.807676,   58.721023),
  fn = calculate_error, 
  e0_expert = e0_expert, 
  ax = ax, 
  bx = bx, 
  method = "Nelder-Mead",
  control = list(trace = 2, maxit = 1000, abstol = 1e-100, minimize = TRUE)
)

# kt optimal
optimal_kt <- result$par

cat("Optimal_kt:", optimal_kt, "\n")

I've tried various approaches to troubleshoot this error, but it's been persisting, impeding my progress, and I have no idea where the problem may lie (i'm quite new to R, might explain why)

1

There are 1 answers

1
user2554330 On

You have the optimx() call:

optimx(
  par = c(72.047557, 70.373062, 66.536728, 63.807676,   58.721023),
  fn = calculate_error, 
  e0_expert = e0_expert, 
  ax = ax, 
  bx = bx, 
  method = "Nelder-Mead",
  control = list(trace = 2, maxit = 1000, abstol = 1e-100, minimize = TRUE)
)

The first value it will try to calculate is

calculate_error(c(72.047557, 70.373062, 66.536728, 63.807676,   58.721023), 
                e0_expert = e0_expert, 
                ax = ax, 
                bx = bx)

(because fn is defined to be calculate_error.) When I try that, I get an error:

Error in calculate_error(c(72.047557, 70.373062, 66.536728, 63.807676,  : 
  object 'Lx' not found

And that's your problem. Your objective function uses the variable Lx without ever assigning a value to it.

You might get different results, if you happen to have a variable named Lx in your workspace. If it's just an oversight that it didn't get into your question, then you should fix your question to get any help with the real error. If it's some variable that you forgot about, then you need to rewrite calculate_error so that it works without it.