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").
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])
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 a problem with the package clogitLasso where I continually get the error "(list) object cannot be coerced to type 'double'"
I've done plenty of searching on this, and there are plenty of ways to pre-convert the data to solve this problem, but no matter what I do it keeps coming up.
I'm not sure what I'm doing wrong here - I can generate data structured exactly like this within R and it runs with the same syntax without any problems, but when I read it in like this it doesn't work.
Using the data (trimmed, but gives the same error): https://pastebin.com/WfB1LJQ2
And the code:
library(clogitLasso)
#Read in data
data <- read.csv('data.txt',sep="\t")
#Data must be sorted so that the
#binary=1 option comes FIRST within the strata
datasorted <- data[order(data$groupid,-data$binary),]
#Convert from a data frame to numericals
X <- as.matrix(datasorted[,1:4])
y <- as.numeric(datasorted[,5])
group <- as.numeric(datasorted[,6])
results <- clogitLasso(X,y,group)
This gives the same error every time. Any tips would be greatly appreciated!
The object y must be of class matrix. Here is the modified code:
library(clogitLasso)
data <- read.csv('WfB1LJQ2.txt',sep="\t", header=T)
datasorted <- data[order(data$groupid,-data$binary),]
X <- as.matrix(datasorted[,1:4])
y <- as.matrix(datasorted[,5])
group <- as.numeric(datasorted[,6])
results <- clogitLasso(X,y,group)
plot(results)
I am new to R and quantmod. I am trying to get daily data for a user defined ticker symbol, like this:
check_symbol<-"GOOG"
check_symbol2<-paste0(check_symbol,".Adjusted")
getSymbols(check_symbol)
temp<-as.vector(GOOG[,check_symbol2])
How do I keep GOOG as a variable in the as.vector(GOOG[,check_symbol2]) part of the above code?
Also, any more elegant way of doing this is much appreciated!
It seems like you'd benefit from using auto.assign=FALSE in the call to getSymbols:
check_symbol <- "GOOG"
check_symbol_data <- getSymbols(check_symbol, auto.assign=FALSE)
temp <- as.vector(Ad(check_symbol_data))
Ok. I´ve tried several foruns and threads, but I couldn't find this. I imported my database to R using this:
teste <- read.zoo("bitcoin2.csv", header=TRUE, sep=",", format = "%m/%d/%Y")
Which worked fine. My xyplot gave me the right plot. So I tried to convert it to ts in order to use strucchange and other outlier/breakpoints packages.
aba <- as.ts(zoo(z$Weighted_Price))
When I did it, it seems to have been lost the index time. The plot still has the same shape, but the X-axis doesn't look as a regular time series plot.
Anyway, I´ve tried the strucchange. After loading it, I made this simple test:
test<-breakpoints(teste$Weighted_Price~1)
But R returned me:
Error in my.RSS.table[as.character(i), 3:4] <- c(pot.index[opt], break.RSS[opt]) :
replacement has length zero
I presume my mistake is that the coercion from zoo to ts was not correct. Any help would be great.