# [Rd] Maybe bug? Using non-integer frequencies in stats::ts

Johann R. Kleinbub joh@nn@k|e|nbub @end|ng |rom gm@||@com
Mon Sep 16 15:53:12 CEST 2019

```I am developing a package to analyse physiological time-series and I
thought that the most reliable and robust solution was to base it on the
native stats::ts class. In my domain it is common to express series
frequencies as samples-per-second. So ts(..., frequency=10) would mean a
signal sampled 10 times every second, and ts(..., frequency = 1) a signal
sampled every second. Following this logic, a few slower signals are
sampled every 5 seconds (or more), resulting in a frequency of e.g. 0.2
Nowhere in the documentation is stated that the frequency must be an
integer value, but using fractional values gives inconsistent results.
For instance, in this example, foo and bar are identical, just with
start-end values shifted by 1. Yet when extracting an arbitrary window, the
'bar' series gives error.

x = 1:22
foo = ts(x, start = 1.5, end = 106.5, frequency = 0.2)
bar = ts(x, start = 2.5, end = 107.5, frequency = 0.2)

window(foo, start = 20, end = 30, extend=TRUE)

# Time Series:# Start = 20 # End = 25 # Frequency = 0.2 # [1] 5 6

window(bar, start = 20, end = 30, extend=TRUE)

# Error in attr(y, "tsp") <- c(ystart, yend, xfreq) : #   invalid time
series parameters specified

The reason is in the rounding procedures for ystart and yend at the end of
the stats::window function. For the 'foo' series the ystart and yend values
are calculated as: c(20, 25), whereas for the 'bar' series, they
become c(20, 30) although the window should be of the very same size in
both cases. (A further discussion on the example is at:
https://stackoverflow.com/questions/57928054 )
Should I report a bug or am I misunderstanding something?

--
Johann R. Kleinbub, PhD