# 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 Apr 24 '12 at 14:35 • The method should be independent of the interval and model. – Etienne Low-Décarie Apr 24 '12 at 15:25 • What function do you optimize ? – Stéphane Laurent Apr 24 '12 at 17:36 • I was told I should be able to do this independent of the model I use. – Etienne Low-Décarie Apr 25 '12 at 20:38 • Corey Chivers provided the answer. – Etienne Low-Décarie Apr 25 '12 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. • Corey Chivers provided the answer. – Etienne Low-Décarie Apr 25 '12 at 21:12 • (+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. – Macro Apr 26 '12 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 – forecaster Jun 16 '15 at 0:03
• Why not delete line 4? – Jason Sep 12 '16 at 19:34
• Is including the line prop_sigma<-diag(prop_sigma) an error? – Mark Miller Jan 21 '17 at 2:58