Devred, Emmanuel
2009-Aug-19 13:41 UTC
[R] Confidence interval on parameters from optim function
Hi everyone, I have two questions: I would like to get confidence intervals on the coefficients derived from the optim() function. I apply optim() to a given function f> res <-optim(c(0.08,0.04,1.),f,NULL,method="L-BFGS-B",lower=c(0.,0.,0.)) And I would like to get the p-value and confidence intervals associated with> res$parMy second question deals with error message. I am doing a loop with the optim() function in it, when I get an error message like below, the loop is stopped, however I would like to change my initial values to avoid this error message and keep the loop going, so if it crashes when I am away the program can still run, any idea ? Error in optim(c(0.08, 0.04, 1), f, NULL, method = "L-BFGS-B", lower c(0, : L-BFGS-B needs finite values of 'fn' Thank you for any information on these two problems. Emmanuel ------------------------------------------- Dr. Emmanuel Devred Bedford Institute of Oceanography, 1 Challenger Drive, Dartmouth, Nova Scotia, B2Y 4A2, Canada Ph: (1) 902 426-4681 Fax: (1) 902 426-9388 devrede@mar.dfo-mpo.gc.ca http://myweb.dal.ca/edevred/ ------------------------------------------- [[alternative HTML version deleted]]
Ravi Varadhan
2009-Aug-19 14:19 UTC
[R] Confidence interval on parameters from optim function
Hi Emmanuel, You can obtain standard error estimate from the Hessian matrix evaluated at the optimum as: sqrt(diag(solve(ans$hessian))), where `ans' is the object returned by `optim'. However, `optim' does not return the Hessian matrix by default. So, you need to specify `hessian = TRUE' when you call `optim'. There are two issues that I would think about: 1. You need to pay attention to the standard regularity conditions that are required in order for the confidence interval to be valid. One important condition is that the solution be in the interior of the parameter space. Since you are using box-constraints, I would check to make sure that the solution is not on the boundary. 2. The Hessian returned by `optim' is a bit inaccurate. Often, this is not a big deal. There are situations, however, where this matters, as you might get an indefinite Hessian due to inaccuracy. So, I generally prefer to use the `hessian' function in the "numDeriv" package. Ravi. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Devred, Emmanuel Sent: Wednesday, August 19, 2009 9:41 AM To: r-help at r-project.org Subject: [R] Confidence interval on parameters from optim function Hi everyone, I have two questions: I would like to get confidence intervals on the coefficients derived from the optim() function. I apply optim() to a given function f> res <-optim(c(0.08,0.04,1.),f,NULL,method="L-BFGS-B",lower=c(0.,0.,0.)) And I would like to get the p-value and confidence intervals associated with> res$parMy second question deals with error message. I am doing a loop with the optim() function in it, when I get an error message like below, the loop is stopped, however I would like to change my initial values to avoid this error message and keep the loop going, so if it crashes when I am away the program can still run, any idea ? Error in optim(c(0.08, 0.04, 1), f, NULL, method = "L-BFGS-B", lower c(0, : L-BFGS-B needs finite values of 'fn' Thank you for any information on these two problems. Emmanuel ------------------------------------------- Dr. Emmanuel Devred Bedford Institute of Oceanography, 1 Challenger Drive, Dartmouth, Nova Scotia, B2Y 4A2, Canada Ph: (1) 902 426-4681 Fax: (1) 902 426-9388 devrede at mar.dfo-mpo.gc.ca http://myweb.dal.ca/edevred/ ------------------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Ravi Varadhan
2009-Aug-19 14:26 UTC
[R] Confidence interval on parameters from optim function
Emmanuel, I didn't answer your second question. You can use the `try' function to capture errors and keep proceeding through simulations without crashing out: ?try If `L-BFGS-B' does not work well, you could try the `spg' function in the "BB" package. Ravi. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Devred, Emmanuel Sent: Wednesday, August 19, 2009 9:41 AM To: r-help at r-project.org Subject: [R] Confidence interval on parameters from optim function Hi everyone, I have two questions: I would like to get confidence intervals on the coefficients derived from the optim() function. I apply optim() to a given function f> res <-optim(c(0.08,0.04,1.),f,NULL,method="L-BFGS-B",lower=c(0.,0.,0.)) And I would like to get the p-value and confidence intervals associated with> res$parMy second question deals with error message. I am doing a loop with the optim() function in it, when I get an error message like below, the loop is stopped, however I would like to change my initial values to avoid this error message and keep the loop going, so if it crashes when I am away the program can still run, any idea ? Error in optim(c(0.08, 0.04, 1), f, NULL, method = "L-BFGS-B", lower c(0, : L-BFGS-B needs finite values of 'fn' Thank you for any information on these two problems. Emmanuel ------------------------------------------- Dr. Emmanuel Devred Bedford Institute of Oceanography, 1 Challenger Drive, Dartmouth, Nova Scotia, B2Y 4A2, Canada Ph: (1) 902 426-4681 Fax: (1) 902 426-9388 devrede at mar.dfo-mpo.gc.ca http://myweb.dal.ca/edevred/ ------------------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Regrading your second question, I guess somehow you get undefined value like logarithm of zero of your target function for some unfortunate parameter values in the parameter space. Devred, Emmanuel wrote:> > Hi everyone, > > I have two questions: > > I would like to get confidence intervals on the coefficients derived > from the optim() function. > I apply optim() to a given function f >> res <- > optim(c(0.08,0.04,1.),f,NULL,method="L-BFGS-B",lower=c(0.,0.,0.)) > And I would like to get the p-value and confidence intervals associated > with >> res$par > > My second question deals with error message. I am doing a loop with the > optim() function in it, when I get an error message like below, the loop > is stopped, however I would like to change my initial values to avoid > this error message and keep the loop going, so if it crashes when I am > away the program can still run, any idea ? > > Error in optim(c(0.08, 0.04, 1), f, NULL, method = "L-BFGS-B", lower > c(0, : > L-BFGS-B needs finite values of 'fn' > > Thank you for any information on these two problems. > > Emmanuel > > ------------------------------------------- > Dr. Emmanuel Devred > Bedford Institute of Oceanography, > 1 Challenger Drive, > Dartmouth, Nova Scotia, B2Y 4A2, > Canada > > Ph: (1) 902 426-4681 > Fax: (1) 902 426-9388 > > devrede at mar.dfo-mpo.gc.ca > > http://myweb.dal.ca/edevred/ > ------------------------------------------- > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/Confidence-interval-on-parameters-from-optim-function-tp25044388p25046772.html Sent from the R help mailing list archive at Nabble.com.
If you f function is a likelihood (or better a log likelihood), then you can do profiling. Take the parameter of interest and change it a small amount from the optimal value, rerun optim with this value fixed and let it optimize over everything else. Repeat this for several values and see how the likelihood changes. You can use this information to construct the confidence interval. One way is by using the chi-square approximation to log likelihood differences. There are already some functions that use this procedure and you could either use those or look at the code to see how to do it yourself. The mle, confint, and profile functions in the stats4 package are one set. If your f function is not a likelihood or log likelihood, then we will need more information on what you are trying to accomplish. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Devred, Emmanuel > Sent: Wednesday, August 19, 2009 7:41 AM > To: r-help at r-project.org > Subject: [R] Confidence interval on parameters from optim function > > Hi everyone, > > I have two questions: > > I would like to get confidence intervals on the coefficients derived > from the optim() function. > I apply optim() to a given function f > > res <- > optim(c(0.08,0.04,1.),f,NULL,method="L-BFGS-B",lower=c(0.,0.,0.)) > And I would like to get the p-value and confidence intervals associated > with > > res$par > > My second question deals with error message. I am doing a loop with the > optim() function in it, when I get an error message like below, the > loop > is stopped, however I would like to change my initial values to avoid > this error message and keep the loop going, so if it crashes when I am > away the program can still run, any idea ? > > Error in optim(c(0.08, 0.04, 1), f, NULL, method = "L-BFGS-B", lower > c(0, : > L-BFGS-B needs finite values of 'fn' > > Thank you for any information on these two problems. > > Emmanuel > > ------------------------------------------- > Dr. Emmanuel Devred > Bedford Institute of Oceanography, > 1 Challenger Drive, > Dartmouth, Nova Scotia, B2Y 4A2, > Canada > > Ph: (1) 902 426-4681 > Fax: (1) 902 426-9388 > > devrede at mar.dfo-mpo.gc.ca > > http://myweb.dal.ca/edevred/ > ------------------------------------------- > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.