Frequency
parameter in a ts
object determines how many samples your series
has among units. Therefore, when you choose a frequency, it assumes a unit.
This unit is the one that will be used to set the start
and end
parameters.
For instance, if you set frequency = 365
, you are assuming that the unit is year
and that there are 365 points sampled between units. Let us identify the first point
of you series at 2016-07-05 in this unit. Year is clearly 2016 and within that year
I take as.Date("2016-07-05") - as.Date("2016-01-01") + 1
, that is 187. Note that
I'm assuming the first sampled day has index 1 instead of 0 as used in the solution
of @Helloworld. Therefore, start = c(2016, 187)
.
data <- c(101,99,97,95,93,91,89,87,85,83,81)
start_dt <- "2016-07-05"
end_dt <- "2016-07-15"
ts_365 <- ts(data, start = c(216, 187), frequency = 365)
ts_365
#> Time Series:
#> Start = c(216, 187)
#> End = c(216, 197)
#> Frequency = 365
#> [1] 101 99 97 95 93 91 89 87 85 83 81
On the other hand, if we want to use frequency = 7
, we need to use week as unit
and use it to specify start
. Indeed, we can obtain the week
(isoweek, but other criteria will work also) and the day of the week, assuming
Monday is the first day (again, you can change this criteria)
strftime(start_dt, "isoweek: %V weekday: %u (%A)")
#> [1] "isoweek: 27 weekday: 2 (martes)"
With frequency = 7
, we will define the time series as
ts_7 <- ts(data, start = c(27, 2), frequency = 7)
ts_7
#> Time Series:
#> Start = c(27, 2)
#> End = c(28, 5)
#> Frequency = 7
#> [1] 101 99 97 95 93 91 89 87 85 83 81
If you plot any of the ts
objects above, you will get a numeric axis based on the unit chosen.
My recommendation is that you set x axis to represent actual dates
x_dates <- seq.Date(from = as.Date(start_dt), to = as.Date(end_dt), by = "day")
plot(x_dates, data)
Created on 2023-03-05 with reprex v2.0.2
The frequency you chose is relevant when you apply some functions to ts
objects
because frequency can be taken as the period to look for seasonality
(decompose()
function, for instance)
xts
, and it keeps the data in the format I expected (like the timestamp and the value for that timestamp). But the output, that I got from callingforecast
using the xts object, is ats
object which does no longer contain those timestamps. I just see the values. – Kester