I've recently learn that there is a package call dyn which can perform regressions on xts object, however I have trouble reading the manual.
If there is a datum like below:
data(sample_matrix)
#sample_matrix is a built-in datum in xts package
xtsObject=as.xts(sample_matrix)[,"Close"]
#Extract daily close price to xtsObject
I tried the code below,but it gives me some error message.
dyn$lm(xtsObject~index(xtsObject))
Is this code correct? If not, how to do it?(I want to set xtsObject as dependent variable, time or date of datum as independent variables)
This looks like a bug but here is a workaround:
tt <- xts(time(xtsObject), time(xtsObject))
dyn$lm(xtsObject ~ tt)
Note that you can ask dyn questions on https://groups.google.com/forum/#!forum/sqldf
Related
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])
I recently started reading about the SuperLearner and I am trying to run SuperLearner for survival outcome in R. I found an example code in the Targeted Learning book by Mark J. van der Laan and Sherri Rose, which require the data to be converted to long format to run.
The function that converts the data to the long format is no longer available. Here is the code:
library(survival)
data(lung)
subLung <- subset(lung, select = c(time, status, age,ph.ecog, ph.karno, pat.karno))
subLung$female <- (lung$sex - 1)
subLung <- subLung[complete.cases(subLung), ]
## Expand subLung to Long Format
longData <- SuperLearner:::createDiscrete(time =subLung$time,
event = (subLung$status == 2),dataX = subset(subLung,
select =-c(time, status)), n.delta = 30)
The createDiscrete function is no longer available in the SuperLearner package. Is there any other function that will convert the data to long format? If not, then a toy example of how to convert the data into appropriate long format would be very helpful. Or a sample R code to run SuperLearner for survival outcome would be also helpful.
I found the answer. To run SuperLearner for survival outcome, the data structure has to be converted to counting process format, meaning that, the time variable should be split in such a way that at most 1 event can happen given a time interval. The survsplit function in survival package does that! Thanks to Dr. Eric C. Polley.
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)
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")
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.