下标超出范围

时间:2014-10-17 11:27:47

标签: r subscript

我一直收到错误subscript out of bounds,但我似乎无法解决问题。我在这里搜索并尝试了所有的建议,但没有设法让它工作。

这是我的数据(需要将其粘贴到csv文档中才能使其正常工作)

Quarter CEM NEM SEM EM
08Q1    23285.19478 33851.5049  16612.74897 73831.11865
08Q2    28746.65234 36133.13926 19650.86779 84750.47149
08Q3    31306.72424 34726.6257  18931.68308 85034.80061
08Q4    35534.98764 36786.73358 26812.4811  99137.19637
09Q1    18889.66318 27347.66828 16173.98474 62626.50672
09Q2    24981.56469 27186.82582 19944.4445  72144.77761
09Q3    26648.05562 30284.12372 17786.01916 74726.1774
09Q4    45733.05621 43851.78196 32518.24748 122203.5416
10Q1    26103.14739 37187.99184 21580.88431 84872.02354
10Q2    33326.57431 35790.52714 26937.67392 96054.77537
10Q3    34553.94579 35046.35961 24186.5165  93849.93456
10Q4    50752.13937 47939.07868 37689.16814 136380.3862
11Q1    30401.16845 38433.89076 25417.26815 94252.32737
11Q2    35396.77208 37045.54789 28602.19806 101044.518
11Q3    43245.86706 37149.49302 32057.4195  112453.256
11Q4    54705.70636 45084.67986 39003.08001 138807.2089
12Q1    33865.62645 40807.17445 27391.7468  102091.1436
12Q2    36675.59209 37814.89296 28043.09073 102568.8303
12Q3    38210.15718 34849.98231 25624.06249 98839.36528
12Q4    51115.28249 41412.00172 36676.70064 129249.4421
13Q1    31155.85191 35508.49135 24455.95786 91207.28917
13Q2    34861.70704 31634.15772 26425.44029 93060.79801
13Q3    35171.02026 29256.66262 23245.385   87776.30512
13Q4    49264.24215 38255.2727  36770.79968 124342.2055
14Q1    31018.62745 33163.87212 22750.14068 87128.55797
14Q2    32429.40978 29592.38391 28238.7409  90261.46195
14Q3    33695.31508 27805.36967 24868.95593 86371.5614

然后这是我的代码(需要将第三行更改为保存文档的位置)

library(forecast)
library(tseries)
M<-read.csv("~/R/mass.csv")
N<-data.frame(M)
deltaT<-1/4
horiz<-4
startY<-c(8,1)
arima.force.seasonality<-"y"
max.sdiff<-3
N<-N[,-1]
cnames<-c(colnames(N))
transform<-"log"
if(transform=="log")
  N<-log(N) else
    NULL
fcastmat<-matrix(0,nrow=horiz,ncol=ncol(N))
colnames(fcastmat)<-cnames
rownames(fcastmat)<-c(1:horiz)
N<-ts(N,start=startY,deltat=deltaT)
for(i in 1:ncol(N)){
  G<-N[,i]
  fweight<-function(x){
    PatX<-0.5+x
    return(PatX)}
  integ1<-integrate(fweight,lower=0.00,upper=1)
  valinteg<-2*integ1$value
  integvals<-rep(0,length.out=length(G))
  for(i in 1:length(G)){
    integi<-integrate(fweight,lower=(i-1)/length(G),upper=i/length(G))
    integvals[i]<-2*integi$value}
  suppressWarnings(kpssW<-kpss.test(G,null="Level"))
  suppressWarnings(ppW<-tryCatch({ppW<-pp.test(G,alternative="stationary")},error=function(ppW){ppW<-list(error="TRUE",p.value=0.99)}))
  suppressWarnings(adfW<-adf.test(G,alternative="stationary",k=trunc((length(N)-1)^(1/3))))
  if(kpssW$p.value<0.05|ppW$p.value>0.05|adfW$p.value>0.05)
    ndiffsW=1 else
      ndiffsW=0
  suppressWarnings(aaw<-auto.arima(G,max.D=max.sdiff,d=ndiffsW,seasonal="TRUE",allowdrift=FALSE,stepwise=FALSE,trace=TRUE,seasonal.test="ch"))
  orderWA<-c(aaw$arma[1],aaw$arma[6],aaw$arma[2])
  orderWS<-c(aaw$arma[3],aaw$arma[7],aaw$arma[4])
  if(sum(aaw$arma[1:2])==0)
    orderWA[1]<-1 else
      NULL
  if(arima.force.seasonality=="y")
    if(sum(aaw$arma[3:4])==0)
      orderWS[1]<-1 else
        NULL else
          NULL
  stAW<-Arima(G,order=orderWA,seasonal=list(order=orderWS),method="ML")
  parSW<-stAW$coef
  WMAEOPT<-function(parSW){
    ArimaW<-Arima(G,order=orderWA,seasonal=list(order=orderWS),include.drift=FALSE,method="ML",fixed=c(parSW))
    errAR<-c(abs(resid(ArimaW)))
    WMAE<-t(errAR)%*%integvals
    return(WMAE)}
  OPTWMAE<-optim(parSW,WMAEOPT,method="SANN",control=list(fnscale=1,maxit=5000))
  parS3<-OPTWMAE$par
  ArimaW1<-Arima(G,order=orderWA,seasonal=list(order=orderWS),include.drift=FALSE,method="ML",fixed=c(parS3))
  fArimaW1<-forecast(ArimaW1,h=8,simulate=TRUE,fan=TRUE)
  if(transform=="log")
    fArimaWF<-exp(fArimaW1$mean[1:horiz]) else
      fArimaWF<-fArimaW1$mean[1:horiz]
  fcastmat[,i]<-fArimaWF
  plot(fArimaW1,sub=cnames[i])
  lines(G,col="red",lwd=2)
  lines(ts(append(fitted(Arimafit),fArimaW1$mean[1]),deltat=deltaT,start=startY),col="blue",lwd=2)}

谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

我没有运行你的代码,只是注意到你在另一个for循环中有一个for循环,但两者都有相同的变量名(i)。 也许您可以从第二个循环中的i更改为j开始?