Decompose coerced time series object R - r

I am trying to decompose a time series object. I have coerced a xts object into time series
To know the structure of my time series object
sapply(ts_group1_ts, class)
store item yearmonth total_sales
"ts" "ts" "ts" "ts"
>decompose(ts_group1_ts[,'total_sales'])
Error in `-.default`(x, trend) : non-numeric argument to binary operator
I want to decompose the total sales part of my time series . Please tell how to decompose. I realise that the error is because the function is not identifying total_sales as integer. How to achieve that in time series object
On doing forced conversion to numeric I get below output:
>decompose(as.numeric(ts_group1_ts[,'total_sales']))
Error in decompose(as.numeric(ts_group1_ts[, "total_sales"])) :
time series has no or less than 2 periods
But the frequency of my time series is 12
>frequency(ts_group1_ts)
12

Related

Time series and how I can construct a ts object

I have a dataset with 3 variables:
the first is date (example"01/01/2019" )
the second is hour (example:"01:00"), and
the third is a numeric.
I want to construct an object ts, but I don't know how I can do this.The first and second variables are characters.
I want an hour time series

Understand frequency parameter while converting xts to ts object in R

What is the meaning of frequency below; when I have converted my xts object to ts object and tried printing ts object I got below information.
My data is hourly data. But I could not understand how this below frequency is calculated. I want to make sure my ts object is treating my data as hourly data.
Time Series:
Start = 1
End = 15548401
Frequency = 0.000277777777777778 (how this is equivalent to hourly frequency?)
So, My dataframe looks like below intitally:
y
1484337600 19.22819
1484341200 19.28906
1484344800 19.28228
1484348400 19.21669
1484352000 19.32759
1484355600 19.21833
1484359200 19.20626
1484362800 19.28737
1484366400 19.20651
1484370000 19.18424
It has epoch times and values. Epoch times are row.names in this dataframe.
Now, I converted into xts object using --
xts_dataframe <- xts(x = dataframe$y,
order.by = as.POSIXct(as.numeric(row.names(dataframe)), origin="1970-01-01"))
ts_dataframe <- as.ts(xts_dataframe)
Please suggest what I'm doing wrong? Basically I want to convert my initial dataframe to ts() object as I need to apply ARIMA on it. This data is per hour data. I'm really facing hard time to work with it.
The frequency is equivalent to 1/deltat, where deltat is the fraction of the sampling period between successive observations. ?frequency gives the example that deltat would be "1/12 for monthly data".
In the case of hourly data, deltat is 3600, since there are 3600 seconds in an hour. Since frequency = 1 / deltat, that means frequency = 1 / 3600, or 0.0002777778.

R: Rank method changes my index format from Date to POSIXct

I have the following problem. I have a XTS containing a date column and several valuations, which should be ranked (biggest = best rank). So my original XTS is test:
> str(index(Test))
Date[1:235], format: "1995-01-31" "1995-02-28" "1995-03-31" "1995-04-28" "1995-05-31" "1995-06-30" "1995-07-31" ...
Now, my rankValuations function:
rankValuations<-function(ValuationXTS){
#Ranks the xts object asset valuations
#ValuationXTS is a xts time series with asset valuations
#Returns an xts object with ranks (asset with the greatest valuation receives 1)
#The ties parameter results in an ascending ranking. If two share the same rank, the first in the matrix gets the first rank
ranked<-as.xts(t(apply(-ValuationXTS,1,rank, ties.method="first",na.last=TRUE)))
}
After running this my index format has changed to POSIX:
> Test<-rankValuations(Test)
> str(index(Test))
POSIXct[1:235], format: "1995-01-31" "1995-02-28" "1995-03-31" "1995-04-28" "1995-05-31" "1995-06-30" "1995-07-31" ...
And this is a big problem because in the POSIX I have now a timezone. If using later on merge.xts it never matches since the POSIX dates are 1 day prior than in the to be merged with XTS which has a Date index. So how can I stop the rank method of changing Date to POSIX?

Time Series Decomposition of weekly data

I am totally new to R and have just started using it. I have three years of weekly data. I want to decompose this time series data into trend, seasonal and other components. I have following doubts:
Which function I should use - ts()or decompose()
How to deal with leap year situation.
Please correct me if I am wrong, the frequency is 52.
Thanks in Advance. I would really appreciate any kind of help.
Welcome to R!
Yes, the frequency is 52.
If the data is not already classed as time-series, you will need both ts() and decompose(). To find the class of the dataset, use class(data). And if it returns "ts", your data is already a time-series as far as R is concerned. If it returns something else, like "data.frame", then you will need to change it to time-series. Assign a variable to ts(data) and check the class again to make sure.
There is a monthly time-series dataset sunspot.month already loaded into R that you can practice on. Here's an example. You can also read the help file for decompose by writing ?decompose
class(sunspot.month)
[1] "ts"
> decomp <- decompose(sunspot.month)
> summary(decomp)
Length Class Mode
x 2988 ts numeric
seasonal 2988 ts numeric
trend 2988 ts numeric
random 2988 ts numeric
figure 12 -none- numeric
type 1 -none- character
> names(decomp)
[1] "x" "seasonal" "trend" "random" "figure" "type"
> plot(decomp) # to see the plot of the decomposed time-series
The call to names indicates that you can also access the individual component data. This can be done with the $ operator. For example, if you want to look at the seasonal component only, use decomp$seasonal.
r time-series

make an intra-month sum of xts object

i have an xts object
It's a daily time series.
With endpoints(prices, 'months') i can have the position of the start and the end of every months
What i want is an xts object where every data is the sum of the daily data in every month
Which is the most efficient way to do this?

Resources