@@ -138,8 +138,9 @@ garma<-function(x,
138138 stop(' QML method does not support k>1. It is suggested you try either the CSS or Whittle methods.\n ' )
139139
140140if (is.null(opt_method )) {
141- if (k > = 2 )opt_method <- ' solnp'
142- else opt_method <- c(' directL' ,' solnp' )
141+ opt_method <- c(' directL' ,' solnp' )
142+ # if (k>=2) opt_method <- 'solnp'
143+ # else opt_method <- c('directL','solnp')
143144 }
144145
145146for (om in opt_method ) {
@@ -177,18 +178,14 @@ garma<-function(x,
177178pars <- numeric (0 )
178179lb <- numeric (0 )
179180ub <- numeric (0 )
180- lb_finite <- numeric (0 )
181- ub_finite <- numeric (0 )
182181
183182mean_methods <- c(' QML' ,' CSS' )
184183if (include.mean & method %in% mean_methods ) {
185184n_pars <- n_pars + 1
186185mean_y <- mean(y )
187186pars <- c(pars ,mean_y )
188- lb_finite <- c(lb_finite ,ifelse(mean_y < 0 ,2 * mean_y ,- 2 * mean_y ))
189- ub_finite <- c(ub_finite ,ifelse(mean_y < 0 ,- 2 * mean_y ,2 * mean_y ))
190- lb <- c(lb ,- Inf )
191- ub <- c(ub ,Inf )
187+ lb <- c(lb ,ifelse(mean_y < 0 ,2 * mean_y ,- 2 * mean_y ))
188+ ub <- c(ub ,ifelse(mean_y < 0 ,- 2 * mean_y ,2 * mean_y ))
192189 }
193190
194191if (k > 0 ) {# initial parameter estimates for Gegenbauer factors
@@ -213,8 +210,6 @@ garma<-function(x,
213210pars <- c(pars ,start_u ,start_d )
214211lb <- c(lb ,min_u ,ifelse(allow_neg_d ,- 1 ,0 ))
215212ub <- c(ub ,max_u ,0.5 )
216- lb_finite <- c(lb_finite ,min_u ,ifelse(allow_neg_d ,- 1 ,0 ))
217- ub_finite <- c(ub_finite ,max_u ,0.5 )
218213 }
219214 }
220215
@@ -233,21 +228,13 @@ garma<-function(x,
233228if (p == 1 & q == 0 ) {# special limits for AR(1)
234229lb <- c(lb ,- 1 )
235230if (method %in% methods_to_estimate_var )lb <- c(lb ,1e-10 )
236- lb_finite <- c(lb_finite ,- 1 )
237- if (method %in% methods_to_estimate_var )lb_finite <- c(lb_finite ,1e-10 )
238231ub <- c(ub ,1 )
239- if (method %in% methods_to_estimate_var )ub <- c(ub ,Inf )
240- ub_finite <- c(ub_finite ,1 )
241- if (method %in% methods_to_estimate_var )ub_finite <- c(ub_finite ,2 * stats :: var(y ))
232+ if (method %in% methods_to_estimate_var )ub <- c(ub ,2 * stats :: var(y ))
242233 }else {
243- lb <- c(lb ,rep(- Inf ,p + q ))
234+ lb <- c(lb ,rep(- 10 ,p + q ))
244235if (method %in% methods_to_estimate_var )lb <- c(lb ,1e-10 )
245- ub <- c(ub ,rep(Inf ,p + q ))
246- if (method %in% methods_to_estimate_var )ub <- c(ub ,Inf )
247- lb_finite <- c(lb_finite ,rep(- 10 ,p + q ))
248- if (method %in% methods_to_estimate_var )lb_finite <- c(lb_finite ,1e-10 )
249- ub_finite <- c(ub_finite ,rep(10 ,p + q ))
250- if (method %in% methods_to_estimate_var )ub_finite <- c(ub_finite ,2 * stats :: var(y ))
236+ ub <- c(ub ,rep(10 ,p + q ))
237+ if (method %in% methods_to_estimate_var )ub <- c(ub ,2 * stats :: var(y ))
251238 }
252239 }
253240
@@ -260,15 +247,17 @@ garma<-function(x,
260247message <- c()
261248
262249# Optimisation functions for each method
263- fcns <- list (' CSS' = .css.ggbr.obj ,' Whittle' = .whittle.ggbr.obj ,' QML' = .qml.ggbr.obj ,' WLL' = .wll.ggbr.obj )
250+ fcns <- list (' CSS' = .css.ggbr.obj ,' Whittle' = .whittle.ggbr.obj ,' QML' = .qml.ggbr.obj ,' WLL' = .wll.ggbr.obj )
251+ grads <- list (' CSS' = NULL ,' Whittle' = .whittle.ggbr.grad ,' QML' = NULL ,' WLL' = NULL )
264252
265253if (opt_method [[1 ]]== ' best' ) {
266- fit <- .best_optim(initial_pars = pars ,fcn = fcns [[method ]],lb = lb ,ub = ub ,lb_finite = lb_finite ,ub_finite = ub_finite ,params = params ,control = control )
254+ fit <- .best_optim(initial_pars = pars ,fcn = fcns [[method ]],grad = grads [[method ]],
255+ lb = lb ,ub = ub ,params = params ,control = control )
267256 }else {
268- fit <- .generic_optim_list(opt_method_list = opt_method ,initial_pars = pars ,fcn = fcns [[method ]],
269- lb = lb ,ub = ub , lb_finite = lb_finite , ub_finite = ub_finite ,params = params ,control = control )
257+ fit <- .generic_optim_list(opt_method_list = opt_method ,initial_pars = pars ,fcn = fcns [[method ]],grad = grads [[ method ]],
258+ lb = lb ,ub = ub ,params = params ,control = control )
270259 }
271- if (fit $ convergence == - 999 ) stop(' Failed to converge.' )
260+ if (fit $ convergence == - 999 ) stop(' ERROR: Failed to converge.' )
272261
273262hh <- fit $ hessian
274263
@@ -297,33 +286,33 @@ garma<-function(x,
297286if (opt_method [[1 ]]== ' solnp' )conv_ok <- (fit $ convergence == 0 )
298287else conv_ok <- (fit $ convergence > = 0 )
299288
289+ vcov1 <- matrix (nrow = length(fit $ par ),ncol = length(fit $ par ))# default
300290if (conv_ok & method != ' WLL' &! is.null(hh )) {
301291# Next, find the se's for coefficients
302292start <- 1
303293se <- c()# default to set this up in the right environment
304294# next check the hessian
305295h_inv_diag <- diag(inv_hessian <- pracma :: pinv(hh ))
306- if (! any(h_inv_diag < 0 )) {# if hessian doesn't look valid then... generate another one
296+ if (any(h_inv_diag < 0 )) {# if hessian doesn't look valid then... generate another one
307297hh <- pracma :: hessian(fcns [[method ]],fit $ par ,params = params )
308298h_inv_diag <- diag(inv_hessian <- pracma :: pinv(hh ))
309299 }
310300if (method == ' Whittle' ) {
311301se <- suppressWarnings(sqrt(2 * pi * h_inv_diag ))# var(y)/length(y) *
312- vcov <- 2 * pi * inv_hessian
302+ vcov1 <- 2 * pi * inv_hessian
313303 }
314304if (method == ' CSS' ) {
315305se <- suppressWarnings(sqrt(h_inv_diag * sigma2 * 2 ))
316- vcov <- inv_hessian * 2 * sigma2
306+ vcov1 <- inv_hessian * 2 * sigma2
317307 }
318308if (method == ' QML' ) {
319309se <- suppressWarnings(sqrt(h_inv_diag * length(y )))
320- vcov <- inv_hessian * length(y )
310+ vcov1 <- inv_hessian * length(y )
321311 }
322312 }
323313if (method == ' WLL' ) {
324314se <- rep(NA ,length(par ))
325315if (k == 1 )se [2 ]<- .wll_d_se(fit $ par [1 ],ss )# result only holds for k=1
326- vcov <- matrix (nrow = length(fit $ par ),ncol = length(fit $ par ))
327316 }
328317nm <- list ()
329318if (include.mean )nm <- c(nm ,' intercept' )
@@ -346,7 +335,7 @@ garma<-function(x,
346335coef <- t(matrix (c(temp_coef ,temp_se ),nrow = n_coef ))
347336 colnames(coef )<- nm
348337 rownames(coef )<- c(' ' ,' s.e.' )
349- colnames(vcov )<- rownames(vcov )<- tail(nm ,nrow(vcov ))
338+ colnames(vcov1 )<- rownames(vcov1 )<- tail(nm ,nrow(vcov1 ))
350339
351340# build a ggbr_factors object
352341gf <- list ()
@@ -383,7 +372,7 @@ garma<-function(x,
383372res <- list (' call' = match.call(),
384373' series' = deparse(match.call()$ x ),
385374' coef' = coef ,
386- ' var.coef' = vcov ,
375+ ' var.coef' = vcov1 ,
387376' sigma2' = sigma2 ,
388377' obj_value' = fit $ value ,
389378' loglik' = loglik ,