How do I convert ts based on yday() back to proper dates - r

I have a periodic ts with frequency of 365 in R using yday to use in forecasting. How do I convert the ts back to a day of the year ?
myTs = ts(rnorm(1:1000), start=c(year(as.Date("2018-05-05")),yday("2018-05-05")),frequency = 365)
head(index(myTs))
I get the following:
[1] 2018.340 2018.342 2018.345 2018.348 2018.351 2018.353
I use this ts for a forecast. But I want to be able to convert back the index of the forecast output to the actual day of the year in my post processing, and I could not find such a function in the documentation. I want to avoid writing such a function if possible.
Is there a function that converts the elements of the ts index back to the yday, i.e. 2018.340 back to 125.

Clemsang's answer:
> format(date_decimal(2016.425),"%Y-%m-%d")
[1] "2016-06-04"

Related

How to convert a weekly dataset into a time series in R

i have a WEEKLY dataset that start on 1986.01.03 and end on 2022-10-07.
The problem is when I forecast the time series with Arima +garch, because the date in T0 is wrong, i.e. 1975 enter image description here.
The function that I used to convert the dataset into time series is here, but I think that the problem is here, since it doesn't take on the right date.
FutureWeekly= ts(WeeklyFuture$FutureWeekly, start= c(1986,1), end = c(2022,10), frequency = 52)
does anyone know how to convert a weekly dataset to time series other than this?
There are the first rows of my dataset and then I have to transform that into returns (diff(log(FutureWeekly) to do the ARMA+GARCH
enter image description here
Try this:
futures<-c(WeeklyFuture$FutureWeekly) #convert to vector
FutureWeekly= ts(futures, start= c(1986,1,10), end = c(1986,3,7), frequency = 52) #add day of week ending on
One of the things ts() demands is a vector of values. I think it might also be easier for ts() to convert the data if it was able to see the 7-day increments.
Assuming you have full un-broken weekly data for the entire period, I think these two things will solve the problem.

Reading Time series data in R

I am trying to import time series data in R with the below code. The data is from 1-7-2014 to 30-4-2017 making it 1035 data point. But when I use the below code it gives 1093 observation.
series <- ts(data1, start=c(2014,7,1), end=c(2017,4,30), frequency = 365)
Can someone help me in understanding where am I going wrong?
ts doesn't allow input for start and end in this form. Either a single number or a vector of two integers is allowed. In second case it's year and day number, starting from 1st January.
With the help of lubridate you can use the following. decimal_date will convert the date to proper integer, suitable for ts.
library(lubridate)
series <- ts(data1, start=decimal_date(as.Date("2014-07-01")), end=decimal_date(as.Date("2017-04-30") + 1), frequency = 365)
> length(series)
[1] 1035

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.

Converting numeric values to yyyymm dates in r

I am trying to covert the following date formats. I have run into trouble using parse_date_time in lubridate package and the strfttime function since it either converts the entire column to the same date or because it keeps on returning the day value. I don't want to see any dates in my solution.
mydata=data.frame(dates=c(200102,200102,200111,200202),desired=c('2001-02','2001-02', '2001-11','2002-02'))
I want to return just the YYYY-mm format in my column. I am having trouble doing this. I have tried using
Try this:
transform(mydata, desired = sub("(....)(..)", "\\1-\\2", dates))
This form is not that convenient for manipulation (plotting, etc.). You might prefer to use the "yearmon" class from the zoo package which stores internally the year/months as year + fraction where fraction is 0 for Jan, 1/12 for Feb, ..., 11/12 for Dec. On output the default rendering is, for example, Jan 2000:
library(zoo)
transform(mydata, ym = as.yearmon(as.character(dates), "%Y%m"))

How to convert ordinal date day-month-year format using R

I have log files where the date is mentioned in the ordinal date format.
wikipedia page for ordinal date
i.e 14273 implies 273'rd day of 2014 so 14273 is 30-Sep-2014.
is there a function in R to convert ordinal date (14273) to (30-Sep-2014).
Tried the date package but didn come across a function that would do this.
Try as.Date with the indicated format:
as.Date(sprintf("%05d", 14273), format = "%y%j")
## [1] "2014-09-30"
Notes
For more information see ?strptime [link]
The 273 part is sometimes referred to as the day of the year (as opposed to the day of the month) or the day number or the julian day relative to the beginning of the year.
If the input were a character string of the form yyjjj (rather than numeric) then as.Date(x, format = "%y%j") will do.
Update Have updated to also handle years with one digit as per comments.
Data example
x<-as.character(c("14273", "09001", "07031", "01033"))
Data conversion
x1<-substr(x, start=0, stop=2)
x2<-substr(x, start=3, stop=5)
x3<-format(strptime(x2, format="%j"), format="%m-%d")
date<-as.Date(paste(x3, x1, sep="-"), format="%m-%d-%y")
You can use lubridate package as follows:
>library(lubridate)
# Create a template date object
>date <- as.POSIXlt("2009-02-10")
# Update the date using
> update(date, year=2014, yday=273)
[1] "2014-09-30 JST"

Resources