简体   繁体   中英

Maximum likelihood estimation in simulation study with R

I generate 10000 values from my distribution and saved in object val_1 . Then I saved val_1 in object pop.
Then, estimated the parameter of same distribution in re-sampling study with no. of iterations 5000 and sample size 100. Below code gave following error:

Error in optim(par = starts, fn = likelihood, x = data, method = "BFGS",  : 
non-finite finite-difference value [1]
In addition:   
There were 50 or more warnings (use warnings() to see the first 50)

My code:

set.seed(83)
pop <- val_1
head(pop)
sam.size <- 100
sam <- sample(pop, sam.size, replace = T)
length(sam)
nsims <- 5000
bs <-array(0,dim=c(nsims ,4))
for(i in 1:nsims)  {   #bs sims
    bs.sam.ind <- sample(1:sam.size, sam.size, replace = T)
    repest<- (goodness.fit(pdf = pdf_ngexp , cdf = cdf_ngexp,
    starts = c(1,1,1,1), sam[bs.sam.ind], method = "BFGS",
    domain = c(0,Inf),mle = NULL, lim_inf = c(0,0,0,0),
    lim_sup = c(2,2,2,2), S = 250, prop=0.1, N=50))
    bs[i,] <-  repest$mle
     }
    head(bs);tail(bs) 
   > head(bs)
      [,1]      [,2]     [,3]     [,4]
[1,] 1.3047830 2.0713999 1.615993 1.953030
[2,] 0.3889123 0.1611767 6.671304 4.033765
[3,] 0.7419812 0.7278994 2.856462 3.914601
[4,] 0.6067144 0.1701769 2.512169 3.086249
[5,] 0.9250573 1.1543839 3.925454 9.867746
[6,] 0.4469384 0.1334538 4.648391 4.711571
 > tail(bs)
    [,1] [,2] [,3] [,4]
[4995,]    0    0    0    0
[4996,]    0    0    0    0
[4997,]    0    0    0    0
[4998,]    0    0    0    0
[4999,]    0    0    0    0
[5000,]    0    0    0    0

We don't have your data, so we can't reproduce the error. But here's the general way to debug problems like this:

  • Save the dataset that triggered the error. It might still be in the variables constructed in the loop; if not, add print messages to your loop so you can figure out which iteration causes it, run again, and stop at the bad iteration. (It's good that you have set.seed(83) ; without set.seed() , this step could be really hard.)

  • Confirm that you still get the same error starting with the bad dataset. Hopefully nothing will be random about it.

  • Now, using the bad dataset, try debugging goodness.fit directly on that dataset. Do this by calling debug(goodness.fit) , then repeating the call from your loop. Find which statement in that function fails, and repeat this process: save whatever data is being sent to it, confirm the error is reproducible, etc.

  • At some point you'll have narrowed down the error to its cause. Then you need to decide whether it was caused by a bug in your code, a bug in the package code, or it's just unavoidable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM