Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

Error in R : object of type 'closure' is not subsettable

$
0
0

This is my first time posting here so I apologize if I make any mistakes when it comes to the format.

I've been working on this project where we have to use ARCH/GARCH models but whenever I tried to use the following code -giving to us by our teacher- I've been hit with a an error message. Here's the code:

'''
    objf.garch <- function(vartheta,
                   eps,n,sig2init,petit=sqrt(.Machine$double.eps),r0=10){     
     omega <- vartheta[1]
     alpha <- vartheta[2]
     beta <- vartheta[3]
     sig2<-rep(0,n)
     sig2[1]<-sig2init
     for(t in 2:n){
     sig2[t]<-omega+alpha*eps[t-1]^2+beta*sig2[t-1]
    }
     qml <- mean(eps[(r0+1):n]^2/sig2[(r0+1):n]+log(sig2[(r0+1):n]))
     qml }
    #
    VarAsymp<- function(omega,alpha,beta,eps,sig2init,petit,r0=10){
     n <- length(eps)
     dersigma2<-matrix(0,nrow=3,ncol=n)
     sig2<-rep(0,n)
     sig2[1]<-sig2init
     for(t in 2:n){
       vec<-c(1,eps[t-1]^2,sig2[t-1])
       sig2[t]<-omega+beta*sig2[t-1]+alpha*eps[t-1]^2
       dersigma2[1:3,t]<-vec/sig2[t]+beta*dersigma2[1:3,(t-1)]
     }
     eta <- eps[(r0+1):n]/sqrt(sig2)[(r0+1):n]
     eta <- eta/sd(eta)

     J<-dersigma2[1:3,(r0+1):n]%*%t(dersigma2[1:3,(r0+1):n])/(n-r0)
     kappa4<-mean(eta^4)

     {if(kappa(J)<1/petit) inv<-solve(J) else inv<-matrix(0,nrow=3,ncol=3)}
     var<-(kappa4-1)*inv
     list(var=var,residus=eta)
   }


   estimGARCH<-
     function(omega,alpha,beta,eps,petit=sqrt(.Machine$double.eps),r0=10)
     {
       valinit<-c(omega,alpha,beta)
       n <- length(eps)
       sig2init<-var(eps[1:min(n,5)])
       res <- nlminb(valinit,objf.garch,lower=c(petit,0,0),
                     upper=c(Inf,Inf,1), eps=eps,n=n,sig2init=sig2init)
       omega <- res$par[1]
       alpha<- res$par[2]
       beta <- res$par[3]

       var<-VarAsymp(omega,alpha,beta,eps,sig2init,petit=sqrt(.Machine$double.eps),r0=10)
       list(coef=c(omega,alpha,beta),residus=var$residus,var=var$var)
     }

Running the code defining these functions doesn't provoke any errors but when I try to apply them to estimate using this

'''       
   ### Estimation ###

   par(mfrow=c(1,1))

   omega.init<- 0.01
   alpha.init<-0.01
   beta.init<- 0.01
   petit=sqrt(.Machine$double.eps)


   fitgarch<-estimGARCH(omega.init,alpha.init,beta.init,eps,petit,r0=10)
   fitgarch
   par<-fitgarch$coef
   res<-fitgarch$residus
    '''

Then I get this message for both par and res :

'''
   Error in fitgarch$coef : object of type 'closure' is not subsettable
   Error in fitgarch$residus : object of type 'closure' is not subsettable
   '''

I tried to change the names of my functions thinking that it had something to do with names being already used for built-in functions in R but it didn't work. Any help or piece of advice is very much welcomed. Thank you.


Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>