2

I've tried to run the code below for maximum likelihood estimation(MLE) in a simulation study. When I run the code I kept getting the warning message "There were 50 or more warnings (use warnings() to see the first 50)" and also when I checked the MLE, there is 0 iterations and a return code 100: Initial value out of range.

lambda <- 0.02
beta <- 0.5
n <- 100
N <- 1000
lambda_hat <- beta_hat <- cp <- NULL

library(survival); library(maxLik)

set.seed(20)
i <- 1
for (i in 1:N) {
  u <- runif(n)
  c_i <- rexp(n, 0.00001)
  t_i <- (log(1 - (1/lambda)*log(1 - u)))^(1/beta)
  s_i <- 1*(t_i < c_i)
  t <- pmin(t_i, c_i)
  data <- data.frame(u, t_i, c_i, s_i, t)
  data
  LLF <- function(para) {
    t1 <- data$t_i
    lambda <- para[1]
    beta <- para[2]
    e <- sum(log(lambda*beta*(data$t_i)^(beta - 1)*exp(t^beta) + 
                   lambda*(1 - exp(t^beta))))
    r <- log(exp(lambda)^(1-exp(t)^(beta)))
    LL <- e + r
    return(LL)
  }
  mle <- maxLik(LLF, start=c(para=c(0.02, 0.5)))
  lambda_hat[i] <- mle$estimate[1]
  beta_hat[i] <- mle$estimate[2]
}

This is the warning message I got when I used warnings()

Warning messages:
1: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
2: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
3: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
4: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
5: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
6: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
7: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
8: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
9: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
10: In log(lambda * beta * (data$t_i)^(beta - 1) * exp((t^beta)) +  ... :
  NaNs produced
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 4
    There are probably values `<= 0` occuring in your `log()`. – jay.sf Sep 28 '21 at 04:29
  • Does it mean that the parameter value is wrong or it does not converge when I run? – Rachel Yeoh Sep 28 '21 at 04:31
  • 1
    I think it's the `lambda*(1-exp(t^beta))` part which is smaller than the first summand. – jay.sf Sep 28 '21 at 04:41
  • 2
    You should use `set.seed()` outside the loop, otherwise the values are the same in every iteration. Also call `library`es outside to avoid multiple loading. Hope you don't mind, I edited it into your question. – jay.sf Sep 28 '21 at 04:46

0 Answers0