object of type 'closure' is not subsettable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

object of type 'closure' is not subsettable

Lemma
Hi.
I tried to run this R-code but still completely no idea why it still gives error message: Error in forecast[[d + 1]] = paste(index(lEJReturnsOffset[windowLength]),  : object of type 'closure' is not subsettable
Here is the R-code:
library(rugarch); library(sos); library(forecast);library(lattice)library(quantmod); require(stochvol); require(fBasics);data = read.table("EURJPY.m1440.csv", header=F)names(data)data=ts(data)lEJ=log(data)lret.EJ = 100*diff(lEJ)lret.EJ = ts(lret.EJ)lret.EJ[as.character(head(index(lret.EJ)))]=0windowLength=500foreLength=length(lret.EJ)-windowLengthforecasts<-vector(mode="character", length=foreLength)for (d in 0:foreLength) {  lEJReturnsOffset=lret.EJ[(1+d):(windowLength+d)]  final.aic<-Inf  final.order<-c(0,0,0)  for (p in 0:5) for (q in 0:5) {    if(p == 0 && q == 0) {      next    }        arimaFit=tryCatch(arima(lEJReturnsOffset, order=c(p,0,q)),                      error=function(err)FALSE,                      warning=function(err)FALSE)    if(!is.logical(arimaFit)) {      current.aic<-AIC(arimaFit)      if(current.aic<final.aic) {        final.aic<-current.aic        final.order<-c(p,0,q)        final.arima<-arima(lEJReturnsOffset, order=final.order)      }    } else {      next    }  }
spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)),                     mean.model = list(armaOrder = c(final.order[1], final.order[3]), arfima = FALSE, include.mean = TRUE),                     distribution.model = "sged")fit <- tryCatch(ugarchfit(spec, lEJReturnsOffset, solver='gosolnp'),  error=function(e) e, warning=function(w) w)if(is(fit, "warning")) {  forecast[d+1]=paste(index(lEJReturnsOffset[windowLength]), 1, sep=",")  print(paste(index(lEJReturnsOffset[windowLength]), 1, sep=","))} else {  fore = ugarchforecast(fit, n.ahead=1)  ind = fore@forecast$seriesFor  forecasts[d+1] = paste(colnames(ind), ifelse(ind[1] < 0, -1, 1), sep=",")  print(paste(colnames(ind), ifelse(ind[1] < 0, -1, 1), sep=",")) }}write.csv(forecasts, file="forecasts.csv", row.names=FALSE)
  
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: object of type 'closure' is not subsettable

Jeff Newmiller
By failing to send your email in plain text format on this mailing list, we see a damaged version of what you saw when you sent it.

Also, we would need some some data to test the code with. Google "r reproducible example" to find discussions of how to ask questions online.

From the error message alone I suspect forecast is the function from the forecast package, and you are trying to create and modify a data object with that same name. At the very least re-using names is unwise, but I think your whole concept of how to create forecasts is deviating from the normal way this is done. But the scrambling of the code isn't helping.

--
Sent from my phone. Please excuse my brevity.

On February 12, 2017 4:34:20 AM PST, Allan Tanaka <[hidden email]> wrote:

>Hi.
>I tried to run this R-code but still completely no idea why it still
>gives error message: Error in forecast[[d + 1]] =
>paste(index(lEJReturnsOffset[windowLength]),  : object of type
>'closure' is not subsettable
>Here is the R-code:
>library(rugarch); library(sos);
>library(forecast);library(lattice)library(quantmod); require(stochvol);
>require(fBasics);data = read.table("EURJPY.m1440.csv",
>header=F)names(data)data=ts(data)lEJ=log(data)lret.EJ =
>100*diff(lEJ)lret.EJ =
>ts(lret.EJ)lret.EJ[as.character(head(index(lret.EJ)))]=0windowLength=500foreLength=length(lret.EJ)-windowLengthforecasts<-vector(mode="character",
>length=foreLength)for (d in 0:foreLength) { 
>lEJReturnsOffset=lret.EJ[(1+d):(windowLength+d)]  final.aic<-Inf 
>final.order<-c(0,0,0)  for (p in 0:5) for (q in 0:5) {    if(p == 0 &&
>q == 0) {      next    }       
>arimaFit=tryCatch(arima(lEJReturnsOffset, order=c(p,0,q)),             
>        error=function(err)FALSE,                     
>warning=function(err)FALSE)    if(!is.logical(arimaFit)) {     
>current.aic<-AIC(arimaFit)      if(current.aic<final.aic) {       
>final.aic<-current.aic        final.order<-c(p,0,q)       
>final.arima<-arima(lEJReturnsOffset, order=final.order)      }    }
>else {      next    }  }
>spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder =
>c(1,1)),                     mean.model = list(armaOrder =
>c(final.order[1], final.order[3]), arfima = FALSE, include.mean =
>TRUE),                     distribution.model = "sged")fit <-
>tryCatch(ugarchfit(spec, lEJReturnsOffset, solver='gosolnp'), 
>error=function(e) e, warning=function(w) w)if(is(fit, "warning")) { 
>forecast[d+1]=paste(index(lEJReturnsOffset[windowLength]), 1, sep=",") 
>print(paste(index(lEJReturnsOffset[windowLength]), 1, sep=","))} else
>{  fore = ugarchforecast(fit, n.ahead=1)  ind =
>fore@forecast$seriesFor  forecasts[d+1] = paste(colnames(ind),
>ifelse(ind[1] < 0, -1, 1), sep=",")  print(paste(colnames(ind),
>ifelse(ind[1] < 0, -1, 1), sep=",")) }}write.csv(forecasts,
>file="forecasts.csv", row.names=FALSE)
>  
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: object of type 'closure' is not subsettable

R help mailing list-2
In reply to this post by Lemma
> Error in forecast[[d + 1]] = paste(index(lEJReturnsOffset[windowLength]),  : object of type 'closure' is not subsettable

A 'closure' is a function and you cannot use '[' or '[[' to make a
subset of a function.

You used
   forecast[d+1] <- ...
in one branch of the 'if' statement and
   forecasts[d+1] <- ...
in the other.  Do you see the problem now?

By the way, the code snippet in the error message says '[[d+1]]' but
the code you supplied has '[d+1]'.  Does the html mangling selectively
double brackets or did you not show us the code that generated that
message?

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sun, Feb 12, 2017 at 4:34 AM, Allan Tanaka <[hidden email]> wrote:
> Hi.
> I tried to run this R-code but still completely no idea why it still gives error message: Error in forecast[[d + 1]] = paste(index(lEJReturnsOffset[windowLength]),  : object of type 'closure' is not subsettable
> Here is the R-code:
> library(rugarch); library(sos); library(forecast);library(lattice)library(quantmod); require(stochvol); require(fBasics);data = read.table("EURJPY.m1440.csv", header=F)names(data)data=ts(data)lEJ=log(data)lret.EJ = 100*diff(lEJ)lret.EJ = ts(lret.EJ)lret.EJ[as.character(head(index(lret.EJ)))]=0windowLength=500foreLength=length(lret.EJ)-windowLengthforecasts<-vector(mode="character", length=foreLength)for (d in 0:foreLength) {  lEJReturnsOffset=lret.EJ[(1+d):(windowLength+d)]  final.aic<-Inf  final.order<-c(0,0,0)  for (p in 0:5) for (q in 0:5) {    if(p == 0 && q == 0) {      next    }        arimaFit=tryCatch(arima(lEJReturnsOffset, order=c(p,0,q)),                      error=function(err)FALSE,                      warning=function(err)FALSE)    if(!is.logical(arimaFit)) {      current.aic<-AIC(arimaFit)      if(current.aic<final.aic) {        final.aic<-current.aic        final.order<-c(p,0,q)        final.arima<-arima(lEJReturnsOffset, order=final.order)      }    } els!
 e {      next    }  }
> spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)),                     mean.model = list(armaOrder = c(final.order[1], final.order[3]), arfima = FALSE, include.mean = TRUE),                     distribution.model = "sged")fit <- tryCatch(ugarchfit(spec, lEJReturnsOffset, solver='gosolnp'),  error=function(e) e, warning=function(w) w)if(is(fit, "warning")) {  forecast[d+1]=paste(index(lEJReturnsOffset[windowLength]), 1, sep=",")  print(paste(index(lEJReturnsOffset[windowLength]), 1, sep=","))} else {  fore = ugarchforecast(fit, n.ahead=1)  ind = fore@forecast$seriesFor  forecasts[d+1] = paste(colnames(ind), ifelse(ind[1] < 0, -1, 1), sep=",")  print(paste(colnames(ind), ifelse(ind[1] < 0, -1, 1), sep=",")) }}write.csv(forecasts, file="forecasts.csv", row.names=FALSE)
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: object of type 'closure' is not subsettable

Lemma
Dang, i should notice that forecastS and forecast thingy. Now it works like a charm. ThANKS

    On Monday, 13 February 2017, 3:56, William Dunlap <[hidden email]> wrote:
 

 > Error in forecast[[d + 1]] = paste(index(lEJReturnsOffset[windowLength]),  : object of type 'closure' is not subsettable

A 'closure' is a function and you cannot use '[' or '[[' to make a
subset of a function.

You used
  forecast[d+1] <- ...
in one branch of the 'if' statement and
  forecasts[d+1] <- ...
in the other.  Do you see the problem now?

By the way, the code snippet in the error message says '[[d+1]]' but
the code you supplied has '[d+1]'.  Does the html mangling selectively
double brackets or did you not show us the code that generated that
message?

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sun, Feb 12, 2017 at 4:34 AM, Allan Tanaka <[hidden email]> wrote:

> Hi.
> I tried to run this R-code but still completely no idea why it still gives error message: Error in forecast[[d + 1]] = paste(index(lEJReturnsOffset[windowLength]),  : object of type 'closure' is not subsettable
> Here is the R-code:
> library(rugarch); library(sos); library(forecast);library(lattice)library(quantmod); require(stochvol); require(fBasics);data = read.table("EURJPY.m1440.csv", header=F)names(data)data=ts(data)lEJ=log(data)lret.EJ = 100*diff(lEJ)lret.EJ = ts(lret.EJ)lret.EJ[as.character(head(index(lret.EJ)))]=0windowLength=500foreLength=length(lret.EJ)-windowLengthforecasts<-vector(mode="character", length=foreLength)for (d in 0:foreLength) {  lEJReturnsOffset=lret.EJ[(1+d):(windowLength+d)]  final.aic<-Inf  final.order<-c(0,0,0)  for (p in 0:5) for (q in 0:5) {    if(p == 0 && q == 0) {      next    }        arimaFit=tryCatch(arima(lEJReturnsOffset, order=c(p,0,q)),                      error=function(err)FALSE,                      warning=function(err)FALSE)    if(!is.logical(arimaFit)) {      current.aic<-AIC(arimaFit)      if(current.aic<final.aic) {        final.aic<-current.aic        final.order<-c(p,0,q)        final.arima<-arima(lEJReturnsOffset, order=final.order)      }    } else {      next    }  }
> spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)),                    mean.model = list(armaOrder = c(final.order[1], final.order[3]), arfima = FALSE, include.mean = TRUE),                    distribution.model = "sged")fit <- tryCatch(ugarchfit(spec, lEJReturnsOffset, solver='gosolnp'),  error=function(e) e, warning=function(w) w)if(is(fit, "warning")) {  forecast[d+1]=paste(index(lEJReturnsOffset[windowLength]), 1, sep=",")  print(paste(index(lEJReturnsOffset[windowLength]), 1, sep=","))} else {  fore = ugarchforecast(fit, n.ahead=1)  ind = fore@forecast$seriesFor  forecasts[d+1] = paste(colnames(ind), ifelse(ind[1] < 0, -1, 1), sep=",")  print(paste(colnames(ind), ifelse(ind[1] < 0, -1, 1), sep=",")) }}write.csv(forecasts, file="forecasts.csv", row.names=FALSE)
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

   
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.