How are the intermediate values between observations in time series calculated in ts() in R? - r

I need help regarding how frequency affects my time series. I fit a daily time series data with frequency = 7 When I view the time series, I get intermediate values between days. I have data for 60 days. I created a time series for the same
ts.v1<- ts(V1, start = as.Date("2017-08-01"), end = as.Date("2017-09-30"), frequency = 7)
which gives me 421 values. I kind of understood that it has to do with the frequency as the value is a product of 7 and 60. What I need to know is- how are these calculated? And why? Isn't frequency used only to tell your time series whether the data is daily/weekly/annual etc.? (I referred to this)
Similarly in my ACF and PACF plots, the lag values are < 1 meaning there are seven values to make 1 'lag'. In that scenario, when I estimate arima(p,d,q) using these plots would the values be taken as lag x frequency?

Normally one does not use Date class with ts. With ts, the frequency is the number of points in a unit interval. Just use:
ts(V1, frequency = 7)
The times will be 1, 1 + 1/7, 1 + 2/7, ... You can later match them to the proper dates if need be.

Related

How to create and analyze a time series with variable test frequency in R

Here is a short description of the problem I am trying to solve: I have test data for multiple variables (weight, thickness, absorption, etc.) that are taken at varying intervals over time - no set schedule, sometimes a test a day, sometimes days might go between tests. I want to detect trends in each of these and alert stake holders when any parameter is trending up/down more than a certain amount. I first did a linear model between each variable's raw data and test time (I converted the test time to days or weeks since a fixed date) and create a table with slopes for each variable - so the stake holders can view one table for all variables and quickly see if any of them is raising concern. The issue was that the data for most variables is very noisy. Someone suggested using time series functions, separating noise and seasonality from the trends, and studying the trend component for a cleaner analysis. I started to look into this and see a couple concerns/questions already:
Time series analysis seems to require specifying a frequency - how do you handle this if your test data is not taken at regular intervals
If one gets over the issue in #1 above, decomposes the data, and gets the trend separated out (ie. take out particularly the random variation/noise), how would you then get a slope metric from that? Namely, if I wanted to then fit a linear model to the trend component of the raw data (after decomposing), what would be the x (independent) variable? Is there a way to connect the trend component of the ts-decompose function with the original data's x-axis data (in this case the actual test date/times, say converted to weeks or days from a fixed date)?
Finally, is there a better way of accomplishing what I explained above? I am only looking for general trends over time - say over 3 months of data, not day to day trends.
Time series are generally used to see if previous observations of a variable have influence on future observations. You would model under the assumption that the previous observations are able to predict the future observations. That is the reason for that most (not all) time series models require evenly spaced instances of training data. If your data is not only very noisy, but also not collected on a regular basis, then you should seriously consider if time series is the appropriate choice of modelling.
Time series analysis seems to require specifying a frequency - how do you handle this if your test data is not taken at regular intervals.
What you can do, is creating an aggregate by increasing the time bucket (shift from daily data to a weekly average for instance) such that every unit of time has an instance of training data. Following your final comment, you could create the average of the observations of the last 3 months of data instead from the observations.
If one gets over the issue in #1 above, decomposes the data, and gets the trend separated out (ie. take out particularly the random variation/noise), how would you then get a slope metric from that? Namely, if I wanted to then fit a linear model to the trend component of the raw data (after decomposing), what would be the x (independent) variable?
In the simplest case of a linear model, the independent variable is the unit of time corresponding to the prediction you are trying to make. However this is not always regarded a time series model.
In the case of an autoregressive model, this would be the previous observation of what you are trying to predict, something similar to y(t) = x(t-1), for instance multiplied by a smoothing factor. I encourage you to read Forecasting: principles and practice which is an excellent book on the matter.
Is there a way to connect the trend component of the ts-decompose function with the original data's x-axis data (in this case the actual test date/times, say converted to weeks or days from a fixed date)?
The function decompose.ts returns a list which includes trend. Trend is a vector of the estimated trend components corresponding to it's respective time value.
Let's create an example time series with linear trend
df <- data.frame(
date = seq(from = as.Date("2021-01-01"), to = as.Date("2021-01-10"), by=1)
)
df$value <- jitter(seq(from = 1, to = nrow(df), by=1))
time_series <- ts(df$value, frequency = 5)
df$trend <- decompose(time_series)$trend
> df
date value trend
1 2021-01-01 0.9170296 NA
2 2021-01-02 1.8899565 NA
3 2021-01-03 3.0816892 2.992256
4 2021-01-04 4.0075589 4.042486
5 2021-01-05 5.0650478 5.046874
6 2021-01-06 6.1681775 6.051641
7 2021-01-07 6.9118942 7.074260
8 2021-01-08 8.1055282 8.041628
9 2021-01-09 9.1206522 NA
10 2021-01-10 9.9018900 NA
As you see, the trend component already is an estimate of the dependent variable at the corresponding time. In decompose the estimate of trend is based on a moving average.

setting the parameter "frequency" for the multi-year average of hourly ozone data

I am trying to decompose a time series which is the monthly multi-year average of hourly ozone data. There are 288 data points (24 hours * 12 months). STL needs ts object to extract the components of time series. And ts has the parameter "frequency". As far as I know, it is the number of observations in one period. For example, it is 12 for monthly averaged temperature data.
What is the frequency for my case since If I use 288
data_ts=stl(ts(data,frequency = 288),s.window = "per"))
As expected, it throws the error "series is not periodic or has less than two periods".
BTW, I am aware of other methods to extract seasonality, but I also need to check the results with STL.
Best
Assuming you have hourly data, there are 24 periods per day, and 24*365.25 periods per year on average. Months would appear to be irrelevant for a natural phenomenon such as ozone. Similarly, weeks are irrelevant. So you just need seasonal periods of 24 and 24*265.35.
The mstl() function from the forecast package can handle multiple seasonal periods.
library(forecast)
data_ts <- mstl(msts(data, seasonal.periods = c(24, 24*365.25)))
However, if you actually have monthly data, then the frequency is 12.
data_ts <- mstl(ts(data, frequency = 12))
As you can see in the picture ACF, the ACF of your data clearly shows an annual seasonal trend.
it peaks at yearly lag at about 12, 24, etc.
If I am behalf on you, I will use freq=12 to decompose my time series data.

read daily data with weekly seasonal in R

I have a daily data with a weekly seasonal component ranging from 2017-03-01 to 2017-05-29. I want to do a seasonal decomposition in R. My code was as follows.
ser = ts(series[,2], frequency=7, start=c(2017,1,1 ))
plot(decompose(ser))
I got a graph as follows.
But the X axis is wrong in the graph. How can I correct it..?
it isn't correct because you have not correctly expressed the arguments frequency.
Reading the help of the function ts() you can see that:
frequency the number of observations per unit of time.
So you can try use this code:
ser = ts(series[,2], frequency=365, start=c(2017,1))
plot(decompose(ser))
Because being daily data, every year you have 365 observations.
Verify that it is the correct solution
I think your frequency is wrong. Also, if your data start in the third day of 2017 you put the wrong start. Try this :
ser = ts(series[,2], frequency = 365.25, start = c(2017,3)) #Third day of 2017
Frequency = 7 isn't really interpretable. For instance, frequency = 12 means that you've got data for each month. In this case you've got daily data so, frequency = 365.25
Default ts object in R seems to be very limited. If you want to create time series with weekly seasonality, i'd recommend the mats object from the forecast library. Because it allows multiple periods, you can define week as well as year as seasonal influence:
library(forecast)
daily_onboardings.msts <- msts(daily_onboardings$count, seasonal.periods = c(7, 365.25),start = decimal_date(min(members$onboarded_at)))

Can we easily avoid the "maximum supported lag is 350 error" when using the ARIMA function in R?

I am currently fitting a SARIMAX model to big data sets. Information is retrieved every 10 minutes, so I have a vector of size 52560 for a year of data. Considering it is representing electricity load in a device throughout said year, we can observe a daily pattern, a weekly one and a yearly one.
There is also a trend, so I need to differentiate my series 4 times. Since the set is for 1 year, I can let aside the yearly seasonality and focus on the other 3. Let's say I get something like this:
dauch = diff(diff(diff(auch2), 144), 1008)
With 144 being the daily seasonality (6×24 10-minute points per day) and 1008 the weekly one.
I would like to fit a SARIMAX model on which I worked with my predecessor. He found that SARIMAX(2,1,5)(1,2,8)144 was the best one for this series. However, I get an error whenever I try this:
themodel = arima(auch[1000:4024,1], order = c(2,1,5), seasonal = list(order = c(1,2,8),
period = 144), xreg=tmpf[988:4012])
tmpf being the temperature used as an exogenous variable. The error is the following:
Error in makeARIMA(trarma[[1L]], trarma[[2L]], Delta, kappa, SSinit) :
maximum supported lag is 350
I don't really understand what it means in my case, because the period I chose is 144 which is inferior to 350. I feel like I need to keep the D = 2 in the model because of the dual differencing for daily and weekly pattern, but I don't know what to do to solve this. Thanks.

[R+zoo]: Operations on time series with different temporal resolutions

I have two time series (sensor data) with different temporal resolutions. A time series from the class "xts / zoo" (TS1) includes hourly values and the other time series (TS2) has a better temporal resolution (one observation every 10 minutes). I.e. for TS1 I have 24 data points (observations) per day and for TS2 I have 144 data points per day.
When I calculate TS1-TS2 for one day I get a result with 24 data points (low temporal resolution). What I would like to achieve is to obtain a result with 144 data points (as TS2, better temporal resolution).
Is it possible to achieve this in R?
P.S.:
That's no a trivial problem because in an hourly interval I just have one observation from TS1 and 6 observations from TS2, so I could imagine this problem can be solved if one draws a fit line between every two points of TS1 and calculate the difference between the line and the data points from TS2. But I know no R Function to do this.
You can approximate missing values using na.approx for linear/constant approx or na.spline for polynomial one.
## new index to be used
new.index <-
seq(min(index(TS1)),max(index(TS1)), by=as.difftime(10,units='mins'))
## linear approx
TS1.new <- na.approx(merge(TS1 ,xts(NULL,new.index)))
Now you can susbtract your ts, (even if maybe you should check that they have same start dates)
TS2-TS1.new

Resources