# In R, given an output from optim with a hessian matrix, how to calculate parameter confidence intervals using the hessian matrix?

Given an output from optim with a hessian matrix, how to calculate parameter confidence intervals using the hessian matrix?

fit<-optim(..., hessian=T)

hessian<-fit$hessian  I am mostly interested in the context of maximum likelihood analysis, but am curious to know if the method can be expanded beyond. • This question is too vague. What kind of confidence intervals? What sort of models are you interested on? Consider taking a look at the literature before posting 3 questions in a row. – user10525 Commented Apr 24, 2012 at 14:35 • The method should be independent of the interval and model. Commented Apr 24, 2012 at 15:25 • What function do you optimize ? Commented Apr 24, 2012 at 17:36 • I was told I should be able to do this independent of the model I use. Commented Apr 25, 2012 at 20:38 • Corey Chivers provided the answer. Commented Apr 25, 2012 at 21:12 ## 1 Answer If you are maximising a likelihood then the covariance matrix of the estimates is (asymptotically) the inverse of the negative of the Hessian. The standard errors are the square roots of the diagonal elements of the covariance (from elsewhere on the web!, from Prof. Thomas Lumley and Spencer Graves, Eng.). For a 95% confidence interval fit<-optim(pars,li_func,control=list("fnscale"=-1),hessian=TRUE,...) fisher_info<-solve(-fit$hessian)
prop_sigma<-sqrt(diag(fisher_info))
prop_sigma<-diag(prop_sigma)
upper<-fit$par+1.96*prop_sigma lower<-fit$par-1.96*prop_sigma
interval<-data.frame(value=fit$par, upper=upper, lower=lower)  Note that: • If you are maximizing the log(likelihood), then the NEGATIVE of the hessian is the "observed information" (such as here). • If you MINIMIZE a "deviance" = (-2)*log(likelihood), then the HALF of the hessian is the observed information. • In the unlikely event that you are maximizing the likelihood itself, you need to divide the negative of the hessian by the likelihood to get the observed information. See this for further limitations due to optimization routine used. • (+1) The inverse of the negative hessian is an estimator of the asymptotic covariance matrix - I know this appears in your code but I think it's important to point out. Commented Apr 26, 2012 at 12:20 • Excellent answer, should the upper and lower bounds read upper<-fit$par+1.96*(prop_sigma/sqrt(n)) lower<-fit\$par-1.96*(prop_sigma/sqrt(n)) ? Thanks Commented Jun 16, 2015 at 0:03
• Why not delete line 4? Commented Sep 12, 2016 at 19:34
• Is including the line prop_sigma<-diag(prop_sigma) an error? Commented Jan 21, 2017 at 2:58
• FWIW numDeriv::hessian() might be worth a look. stat.ethz.ch/pipermail/r-help/2009-August/401566.html Commented Apr 1, 2020 at 14:50