read daily data with weekly seasonal in R - 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)))

Related

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.

ERROR in R: decompose(y) : time series has no or less than 2 periods

I have a time series data of daily transactions, starting from 2017-06-28 till 2018-11-26.
The data looks like this:
I am interested to use decompose() or stl() function in R. But I am getting
error:
decompose(y) : time series has no or less than 2 periods
when I am trying to use decompose()
and
Error in stl(y, "periodic") :
series is not periodic or has less than two periods
when I am trying to use stl().
I have understood that I have to specify the period, but I am not able to understand what should be the period in my case? I have tried with the following toy example:
dat <- cumsum(rnorm(51.7*10))
y <- ts(dat, frequency = 517)
plot.ts(y)
stl(y, "periodic")
But I couldn't succeed. Any help will be highly appreciated.
The frequency parameter reflects the number of observations before the seasonal pattern repeats. As your data is daily, you may want to set frequency equal to 7 or 365.25 (depending on your business seasonality).
Of course, the larger the business seasonality, the more data you need (i.e. more than 2 periods) in order to decompose your time series. In your case, you set the frequency to 517, but have data available for less than two periods. Thus, the seasonal decomposition cannot happen.
For more info, please see: Rob Hyndman's Forecasting Principles and Practice book

How are the intermediate values between observations in time series calculated in ts() in 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.

seasonal decomposition loess daily temperature time series

I have a 10 year daily time series of air temperatures:
x <- c(rep((seq(-3,5,by=0.85)),365),NA)
I converted it to a time series object like this:
x <- ts(x, frequency=10, start=1)
and ran stlm
stlm(x, s.window=365, robust=TRUE, allow.multiplicative.trend=TRUE, level=0.95)
which produced the error
error in na.fail.default(as.ts(x)) : missing values in object
This is very strange, because meteorological time series are highly seasonal. What could I do to fix that? Is there a problem with the zeros?
Any help appreciated.
UPDATE: There was one missing value in my time series, which produced the error. The partial code
robust=TRUE, allow.multiplicative.trend=TRUE, level=0.95
produced another error and the arguments obviously cannot be used.
How can I decompose my time series adequately into season and trend in order to identify the trend which eventually changed during the 10 years?
you could also try using the dsa package, which is explicitly designed to handle daily data. You have to convert your data first to xts, but then, you'll be fine,
library(dsa); library(xts)
x <- c(rep((seq(-3,5,by=0.85)),365),NA) # I didn't change these strange looking data ;-)
# Converting to xts
dates <- seq(as.Date("2010-01-01"),length=length(x),by="days")
x <- xts(x, order.by=dates)
result <- dsa(x, fourier_number=24) # if no monthly recurring cycle is visible
# fourier_number can be reduced or left empty.
sa <- result$output[,1] # This is the seasonally adjusted series
xtsplot(result$output[,c(2,1)], names=c("original series", "seasonally adjusted series"))

R - Trend estimation for short time series

I have very short time series of data from a climatic experiment that I did back in 2012. The data consists of daily water solution flux and daily CO2 flux data. The CO2 flux data comprises 52 days and the water solution flux data is only 7 days long. I have several measurements a day for the CO2 flux data but I calculated daily averages.
Now, I would like to know if there is a trend in these time series. I have figured out that I can use the Kendall trend test or a Theil-Sen trend estimator. I used the Kendall test before for a time series spanning several years. I don't know how to use the Theil-Sen trend estimator.
I put my data into an ts object in R, but when I tried doing a decompositon (using the function decompose) I get the error that the time series is spanning less than 2 periods. I would like to extract the trend data and then do a Mann-Kendall test on it.
Here is the code that I got so far:
myexample <- structure(c(624.27, 682.06, 672.77,
765.96, 759.52, 760.38, 742.81
), .Names = c("Day1", "Day2", "Day3", "Day4", "Day5", "Day6",
"Day7"))
ts.object <- ts(myexample, frequency = 365, start = 1)
decomp.ts.obj <- decompose(ts.obj, type = "mult", filter=NULL)
# Error in decompose(ts.obj, type = "mult", filter = NULL)
Can anyone help me on how to do a trend analysis with my very short time series? Any google-fu was to no avail. And, can someone tell me what the size of the Kendall tau means? It spans values from -1 to 1. Is a tau=0.5 a strong or a weak trend?
Thanks,
Stefan
I would be tempted to do something simple like
d <- data.frame(val=myexample,ind=seq(myexample))
summary(lm(val~ind,d))
or
library(lmPerm)
summary(lmp(val~ind,d))
or
cor.test(~val+ind,data=d,method="kendall")
Whether tau=0.5 is strong or weak depends a lot on context. In this case the p-value is 0.24, which says at least that this signal (based on rank alone) is not distinguishable from an appropriate null signal.

Resources