# [Rd] Bug in package stats function ar() (PR#10459)

From: <smckinney_at_bccrc.ca>
Date: Sat, 24 Nov 2007 02:05:21 +0100 (CET)

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
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 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
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:

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.

```###-----------------------------------------------------------------

```

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
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 :-(

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))

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 24 Nov 2007 - 01:13:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 24 Nov 2007 - 18:30:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.