I believe that the best way is to define extractor functions when generics are not available and methods when generics already exist.
The first two functions extract the values of interest from the fitted objects.
get_sigma_t <- function(x, ...){
x@sigma.t
}
get_z_t <- function(x, ...){
x@fit$series$z
}
Here a logLik
method for objects of class "fGARCH"
is defined.
logLik.fGARCH <- function(x, ...){
x@fit$value
}
Now use the functions, including the method. The data comes from the first example in help("garchFit")
.
N <- 200
r <- as.vector(garchSim(garchSpec(rseed = 1985), n = N)[,1])
model <- garchFit(~ garch(1, 1), data = r, trace = FALSE)
get_sigma_t(model) # output not shown
get_z_t(model) # output not shown
logLik(model)
#LogLikelihood
# -861.9494
Note also that methods coef
and fitted
exist, there is no need for model@fitted
or model@fit$coefs
, like is written in the question.
fitted(model) # much simpler
coef(model)
# mu omega alpha1 beta1
#3.541769e-05 1.081941e-06 8.885493e-02 8.120038e-01