ts object not recognised in hybridModel of forecastHybrid package - r

Data is something like this:
df <- tribble(
~y,~timestamp
18.74682, 1500256800,
19.00424, 1500260400,
18.86993, 1500264000,
18.74960, 1500267600,
18.99854, 1500271200,
18.85443, 1500274800,
18.78031, 1500278400,
18.97948, 1500282000,
18.86576, 1500285600,
18.55633, 1500289200,
18.79052, 1500292800,
18.74790, 1500296400,
18.62743, 1500300000,
19.04696, 1500303600,
18.97851, 1500307200,
18.70956, 1500310800,
18.92302, 1500314400,
18.91465, 1500318000,
18.61556, 1500321600,
19.03535, 1500325200 )
I'm trying to apply hybridModel on timeseries data to perform ensemble.Below is my code:
library(tidyquant)
library(forecast)
library(timetk)
library(sweep)
library(forecastHybrid)
df <- mutate(df, timestamp = as_datetime(timestamp))
tk_ts_df <- tk_ts(df, start = 1, freq = 3600, silent = TRUE)
fit <- hybridModel(tk_ts_df)
On fitting timeseries object tk_ts_df (ts object) to hybridModel; it's giving error : "The time series must be numeric and may not be a matrix or dataframe object."
But on link: https://cran.r-project.org/web/packages/forecastHybrid/vignettes/forecastHybrid.html
It's clearly mentioned : The workhorse function of the package is hybridModel(), a function that combines several component models from the “forecast” package. At a minimum, the user must supply a ts or numeric vector for y
Please suggest what I'm doing wrong.

The "forecastHybrid" requires that the input timeseries is a numeric vector or ts type. While the "timekit" package does return a ts object, it also adds additional attributes that are not in regular ts objects so input checks failed.
See discussion here. and the fixing commit here.
The latest version from Github incorporating the fix can be downloaded with
devtools::install_github("ellisp/forecastHybrid/pkg")

Related

Graphing time series in R

I'm new to R and am having a lot of trouble with what's essentially my first assignment.
I'm trying to plot the adjusted closing prices of the NASDAQ over a time period 2014-2018 (time series analysis module).
I have been provided with the following code which I am told I should have to make minor adjustments to:
data <- read.csv('Nasdaq_2014_2018.csv')
t <- data[,1] #This is the date (yyyy-mm-dd), first column of the dataset
y <- data[,6] #This is the adjusted closing price, sixth column of the dataset
plot(t, y)
The error messages I am getting are:
error in data[,1]: object of type 'closure' is not subsettable
error in data[,6]: object of type 'closure' is not subsettable
and
error in plot.function(t,y): object 'y' not found
I find this last one strange as t seems to exist. I have tried other methods to plot the graph to no avail, i.e
plot(Nasdaq_2014_2018$Date, Nasdaq_2014_2018$Adj.Close)
I understand this may be a very basic question but I've been trying to fix the problem all day to no avail, and this is only the first part of the first question of the assignment :(
In your case I would have a look at the xts package that was developed for TS in the context of Trading and Stocks. dplyr whith lubridate for the date might be more complex in your case.
Regarding importing as you stuck have a look at the other import functions.
Easy way:
library(quantmod)
getSymbols("QQQ", auto.assign = T)
plot(QQQ$QQQ.Adjusted)
Using your data:
library(xts)
dataxts <- xts(data[, -1], order.by = as.Date(data[, 1], format = "%Y-%m-%d"))
plot.xts(dataxts[, 5])

how to get tsclean working on data frame with multiple time series

I'm in the process of creating a forecast based on the hts package but before getting this far I need to clean the data for outliers and missing values.
For this I thought of using the tsclean function in the forecast package. I got my data stored in data frame with multiple columns (time series) that I wish to get cleaned. I can get the function to work when only having one time serie, but since I do have quite a lot i'm looking for a smart way to do this.
When running the code:
SFA5 <- ts(SFA4, frequency=12, start=c(2012,1), end=c(2017,10))
ggt <- tsclean(SFA5[1:70, 1:94], replace.missing = TRUE)
I get this error message:
Error in na.interp(x, lambda = lambda) : The time series is not univariate.
The data is here:
https://www.dropbox.com/s/dow2jpuv5unmtgd/Data1850.xlsx?dl=0
My question is: what am i doing wrong or is the only solution to do a loop sequence
The error message suggests that the function takes univariate time series as its first argument only. So you need to apply tsclean to each column, as you might have guessed.
library(forecast)
ggt <- sapply(X = SFA5[1:70, 1:94], FUN = tsclean)

Moving from zoo to xts object

I have various financial data that I am trying to merge into an xts object so I can perform multiple statistical analyses. I am having difficulty, however, with dates when moving from the original data to a zoo object to an xts object.
For instance, I read in some hedge fund return data, change the report date variable using the ymd function from the lubridate package, create a zoo object, then just as a check create a timeSeries object. All seems to be OK, but I continue to get an error when I attempt to create the xts object, as shown below:
hfIndexes$ReportDt <- ymd(hfIndexes$ReportDt)
hfIndexesZoo <- zoo(hfIndexes,order.by="ReportDt")
hfIndexesTimeSeries <- as.timeSeries(hfIndexesZoo)
hfIndexesXTS <- as.xts(hfIndexesZoo)
Error in xts(coredata(x), order.by = order.by, frequency = frequency, :
order.by requires an appropriate time-based object
What do I need to do to ensure that I have the correct time-based object to create the desired xts object?
Consider this answer: https://stackoverflow.com/a/4297342/3253015
order.by is an argument needed in xts objects. As we are dealing with timeseries, you can consider it to be one, that creates a frame of sorts, into which the data is put. So you tell as.xts that the data you want inside is spaced out by the time-based object given in order.by.

In R cannot use AdjustedSharpeRatio() from 'Performance Analytics'

I have some troubles using the function AdjustedSharpeRatio() from the package PerformanceAnalytics, the following code sample in R 3.0.0:
library(PerformanceAnalytics)
logrets = array(dim=c(3,2),c(1,2,3,4,5,6))
weights = c(0.4,0.6)
AdjustedSharpeRatio(rowSums(weights*logrets),0.01)
gives the following error:
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to pass in
names from a data object with one column, you should use the form 'data[rows,
columns, drop = FALSE]'. Rownames should have standard date formats, such as
'1985-03-15'.
Replacing the last line with zoo gives the same error:
AdjustedSharpeRatio(zoo(rowSums(weights*logrets)),0.01)
Am I missing something obvious ?
Hmm...not too sure what you are trying to achieve with the logrets and weights objects there....but if logrets are already in percentages. then maybe something like this...
AdjustedSharpeRatio(xts(rowSums(weights*logrets)/100,Sys.Date()-(c(3:1)*365)), Rf=0.01)
This might work:
a <- rowSums(weights*logrets)
names(a) <- c('1985-03-15', '1985-03-16', '1985-03-17')
AdjustedSharpeRatio(a,0.01)

Adding a variable to a ts object in R

I have an object that I have created using the as.ts function in R, and now I would like a simple way to transform one of the variables and add it to the same ts object. So, for example
tsMloa <- ts(read.dta("http://www.stata-press.com/data/r12/mloa.dta"), frequency=12, start=1959)
tsMloa[, "meanLog"] <- tsMloa[,"log"] - mean(tsMloa[,"log"])
gives me a subscript out of bounds error. How can I get around this?
Firstly, you ought to consider adding require(foreign) to your example code, as it's necessary to run your code.
I don't know anything about *.dta files or their formatting, but i can tell you that if you'd like to work with time series in R, you'd do well to look into the zoo and xts family of functions.
With that in mind, try the following:
require(xts)
require(foreign)
tsMloa <- ts(read.dta("http://www.stata-press.com/data/r12/mloa.dta"), frequency=12, start=1959)
tt <- seq(as.Date("1959-01-01"), as.Date("1990-12-01"), by='mon')
tsMloa_x <- xts(unclass(tsMloa)[,1:3], order.by=tt)
tsMloa_x$meanLog <- tsMloa_x$log - mean(tsMloa_x$log)
That should do what you are looking for -- and it gives you a reason to look into the very good packages.
Doing it with zoo -- plus i've created a function to turn your integers into months.
require(foreign)
require(zoo)
Mloa <- read.dta("http://www.stata-press.com/data/r12/mloa.dta"), frequency=12, start=1959)
intToMonth <- function(intMonth, origin = "1960-01-01"){
dd <- as.POSIXlt(origin)
ddVec <- rep(dd, length(intMonth))
ddVec$mon <- ddVec$mon + intMonth%%12
ddVec$year <- ddVec$year + intMonth%/%12
ddRet <- as.Date(ddVec)
return(ddRet)
}
dateString <- intToMonth(Mloa[, 'tm'])
zMloa <- zoo(Mloa[, -2], dateString)
zMloa$meanLog <- zMloa$log - mean(zMloa$log)
As i see it, your problem is with converting the timestamps in the source file to something R understands and can work with. I found this part of adapting to R especially tricky.
The above function will take your month-integers, and turn them into a Date object. The resultant output will work with both zoo and xts as the order.by argument.
If you need to change the origin date, just supply the second argument to the function -- i.e. otherDateString <- intToMonth(timeInts, "2011-01-01").

Resources