From smckinney at bccrc.ca  Sat Nov 24 02:05:21 2007
From: smckinney at bccrc.ca (smckinney at bccrc.ca)
Date: Sat, 24 Nov 2007 02:05:21 +0100 (CET)
Subject: [Rd] Bug in package stats function ar() (PR#10459)
Message-ID: <20071124010521.AEE0B282EFF7@mail.pubhealth.ku.dk>

Full_Name: Steven McKinney
Version: 2.6.0
OS: OS X
Submission from: (NULL) (142.103.207.10)



Function ar() in package "stats" is showing
a quirky bug.  Some calls to ar() run to
completion, others throw an error.

The bug is reproducible by several people on different
machines, however, the ar() function itself ends
up throwing the error sporadically.  Several calls to
ar() may be necessary to trip the error condition.

Code to reproduce:

x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1980,3))
# ar function
res.ar<-ar(x,aic=TRUE,demean=F)
# call "ar" again and ............
res.ar<-ar(x,aic=TRUE,demean=F)


Example output:
(Note that on this attempt the first call to ar()
 tripped the error.)

> x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-
+ 1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1980,3))
> 
> # ar function
> res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In log(var.pred) : NaNs produced
2: In if (order > 0) coefs[order, 1:order] else numeric(0) :
  the condition has length > 1 and only the first element will be used
> 
> # call "ar" again and ............
> res.ar<-ar(x,aic=TRUE,demean=F)
> x
           Qtr1       Qtr2       Qtr3       Qtr4
1978                       -0.2052083 -0.3764986
1979 -0.3762448  0.3740089  0.2737568  2.8235722
1980 -1.7783313  0.2728676 -0.3273164           
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar$resid
           Qtr1       Qtr2       Qtr3       Qtr4
1978                       -0.2052083 -0.3764986
1979 -0.3762448  0.3740089  0.2737568  2.8235722
1980 -1.7783313  0.2728676 -0.3273164           
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar$resid
           Qtr1       Qtr2       Qtr3       Qtr4
1978                       -0.2052083 -0.3764986
1979 -0.3762448  0.3740089  0.2737568  2.8235722
1980 -1.7783313  0.2728676 -0.3273164           
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In if (order > 0) coefs[order, 1:order] else numeric(0) :
  the condition has length > 1 and only the first element will be used
> res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In log(var.pred) : NaNs produced
2: In if (order > 0) coefs[order, 1:order] else numeric(0) :
  the condition has length > 1 and only the first element will be used
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> sessionInfo()
R version 2.6.0 (2007-10-03) 
powerpc-apple-darwin8.10.1 

locale:
C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
> 


###-----------------------------------------------------------------

Also seen in R 2.5.0:

> x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1980,3))
> # ar function
> res.ar<-ar(x,aic=TRUE,demean=F)
> # call "ar" again and ............
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) : 
        missing value where TRUE/FALSE needed
In addition: Warning message:
the condition has length > 1 and only the first element will be used in: if
(order > 0) coefs[order, 1:order] else numeric(0) 
> sessionInfo()
R version 2.5.0 (2007-04-23) 
x86_64-redhat-linux-gnu 

locale:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_P
APER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
[7] "base"     
> 

###-----------------------------------------------------------------

However, if I lengthen the input vector 

x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164,
  -0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1982,3))

I can not trip the error, so perhaps it is due to the short length of
the user's input data.



###-----------------------------------------------------------------

Nov 23, 2007 original email from R-help

[R] help pleaseeeeeeeee


Dears Sirs

During my computational work I encountered unexpected behavior when calling
"ar" function, namely
# time series
x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-
1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1980,3))

# ar function
res.ar<-ar(x,aic=TRUE,demean=F)

# call "ar" again and ............
res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) :
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In log(var.pred) : NaNs produced
2: In if (order > 0) coefs[order, 1:order] else numeric(0) :
  the condition has length > 1 and only the first element will be used

For me it is mysterious why sometimes it works and others it does not,
perhaps I am doing something wrong and stupid :-(

If anyone had already had this problem could you please tell me how you have
solved it?

Thank you for your time.

Best Regards,

Clara Cordeiro



x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164,
  -0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1982,3))



[Rd] Bug in package stats function ar() (PR#10459)
smckinney at bccrc.ca