## source('tools.R')
library(R2admb)  ## requires 0.7.2
sessionInfo()
file.copy('../DATA/OrangeTree.dat', './orangetree.dat')
file.copy('../DATA/OrangeTree.pin', './orangetree.pin')
system('admb orangetree')
time<-system.time(system('./orangetree'))

fit<-read_pars('orangetree')

idx<-1:4
coef=fit$coefficients[idx]
sd=fit$se[idx]
ci.q=coef+sd%o%c(-2,2)
obj=-fit$loglik
maxgrad=fit$maxgrad
ev<-eigen(solve(fit$vcov[idx,idx]))$value
eratio <- min(ev)/max(ev)

results <- list(obj=obj,                     ## negative log L
                coef=coef,                   ## coefficients (MLE/mode)

                coef.median=NULL,
                sd=sd,                       ## standard deviations 
                confint.quad=ci.q,           ## Nx2 - matrix 95% ci 
                confint.profile=NULL,        
                confint.mcmc=NULL,           ## 0.025 0.975 quantiles
                time=c(fit=time,profile=NULL),  
                terminfo=c(maxgrad=maxgrad,eratio=eratio), ## MLE termination info
                convinfo=NULL
                )

save("results",file="fit.RData")

rmlist<-c("admodel.cov","admodel.hes","admodel.dep","fmin.log",
          "eigv.rpt","variance",paste('orangetree',c(".bar",".cpp",
          ".eva",".log", ".htp",".o","",".cor",".par",".std",
          ".dat"),sep=""))
file.remove(rmlist)


