I used quantmod to download stock data from yahoo finance. Here msft is a xts object.
library(quantmod)
library(forecast)
library(xts)
library(zoo)
start <- as.Date('2018-01-01')
end <- as.Date('2018-08-14')
getSymbols('MSFT', src='yahoo', from=start, to=end)
msft <- MSFT[, 'MSFT.Adjusted']
I'm trying to convert xts object to ts object. Below is what I did. My result is kind of weird. What frequency should I put in this case? The stock data are daily data (weekdays only). Thanks a lot for help.
ts(msft, start=c(2018,1,1), frequency = 365)
You can use as.timeSeries.xts from the xts library.
msft <- as.timeSeries.xts(MSFT)
str(as.timeSeries.xts(msft))
plot(msft)
I hope this will help
msft1<- as.data.frame(msft) #converting it to Data frame
rownames(msft1) <- NULL #Nullify all the rownames
timeseries<-ts(msft1, start=c(2018,1,1), frequency = 365) #convert it into a ts object
plot(timeseries) ## Plot to verify the time series
you can select weekdays from the below code
# install.packages('timeDate')
require(timeDate)
# A ’timeDate’ Sequence
tS <- timeSequence(as.Date("1991/1/4"), as.Date("2010/3/1"))
tS
# Subset weekdays
tW <- tS[isWeekday(tS)]; tW
dayOfWeek(tW)
Related
Good day,
I am building an auto.arima forecast in R. I was able to complete the forecast successfully, however the results is not displaying the date.
Forecast result:
The Plot
Data
So if you look at the x-axis, you see here it displays the years in periods.I would like to be able to export this data with actual dates
I use
library("tseries")
library("forecast")
library("xts")
The code:
Pulsedata$date <- as.Date(Pulsedata$date,format = "%d-%b-%y")
PD_ts <- msts(Pulsedata$Call_volume, start = c(2016, 01), end = c(2018,
365), seasonal.periods=c(365))
DPD_ts <- decompose(PD_ts, "multiplicative")
AA <- auto.arima(ts(PD_ts,frequency=365),D=1)
Myforecast <- forecast(AA,h=365)
plot(Myforecast)
I have tried:
Anydate
sweep
as.date
lubridate
setDT
I have a CSV file with the format
ref_date;wings;airfoil;turbines
2015-03-31;123,22;22,77;99,0
2015-04-30;123,22;28,77;99,0
2015-05-31;123,22;22,177;02,0
2015-06-30;56,288;22,77;99,0
and I want to use the forecast package to predict the next values of this time series. The forecast package only accepts a ts object, but so far all my attempts to create one failed. I tried to
1) Use zoo package
df = read.zoo(data_file, sep=';', dec=',', format="%Y-%m-%d", header=T)
but the data is truncated at the decimal point.
2) Use the zoo package with xts
df = read.zoo(datafile, sep=';', dec=',', format="%Y-%m-%d", header=T)
df_ts = ts(df)
The dates are nowhere to be seen, the index is just a sequence of numbers, like
1 123.22 22.77 99
3) Use read.csv and ts
df = read.zoo(datafile, sep=';', dec=',', format="%Y-%m-%d", header=T)
df_ts = ts(df)
4) Try using xts
df = read.csv(data_file, sep=';', header=T, dec=',')
tt = as.xts(df[,-1],order.by = as.Date(as.character(df[,1]), format = "%Y-%m-%d"))
forecast(tt)
Error in `tsp<-`(`*tmp*`, value = tsp.y) :
invalid time series parameters specified
the result looses all information about the date, including the ref_date column, and now the forecast package gives nonsense as result.
What is the correct approach to create the object that the forecast library is waiting and can generate a forecast, maintaining the dates, including in the plots?
I have been wrestling CSV data into ZOO/XTS objects and sympathize -- painful.
Suggest using as_xts() in the tidyquant package
as_xts(read_csv(file),ref_date)
You may need to coerce the resulting coredata() in the XTS object back to numeric.
For annual data (such as Annual Income Statements), I would like to keep xts format but I need to convert the index of the table to "only year". There are yearmon and yearqtr classes but I did not find "year" only class to work with xts.
# IS is annual reports of incomes. time(IS) is POSIXct.
library(quantmod)
IS <- viewFin(get(getFin("IBM")), "IS", "A") # Download data
IS <- as.xts(t(IS)) # Convert to xts
time(IS) <- as.yearqtr(time(IS)) ## works to have quarterly index
time(IS) <- as.yearmon(time(IS)) ## works to have monthly index
time(IS) <- ????(time(IS)) ## To have yearly index with xts class
What is the best solution? Thank you.
It would be helpful if you explained why you need to have the index as "year" only. Xts has an indexFormat command that allows you to control how dates are displayed, and while I've never used it I assume it will allow you to display only the year of any given index entry.
A more extreme solution would be to convert every date to the first of the year in that year. Here's some code to help do this:
first.of.year <- function(x) # Given a date, returns the first day of that year
return(as.Date(paste(year(as.Date(x)),"-01-01", sep="")))
index(x) <- first.of.year(index(x))
I'm doing a forecast in R using the forecast package.
I have a time series with daily data (download the .CSV here):
library(forecast)
data <- read.csv('daily_electricity.csv')
time_series <- ts(data$value, start=c(2007,1,1), frequency=365.25)
fit <- stlf(time_series) # uses STL decomposition
plot(fit)
forecast(fit, h=365)
But when I issue the last forecast command to get predictions for the next 365 days, the output not only skips days - but the values aren't in regular date format:
2012.687 2480489
2012.689 2411931
2012.692 2582997
2012.695 2190245
2012.697 2603242
2012.700 2413211
How can I get forecasts for the next 365 days, with each value formatted with the correct date, with no missing days?
The first thing you have to do is convert the dates (which are brought in as row numbers) to an actual column.
setDT(fit, keep.rownames = TRUE)[]
Then you can use decimal_date() to convert to proper format:
fit$dates <- as.Date(date_decimal(as.numeric(fit$rn)))
library(quantmod)
getSymbols('AAPL')
n <- nrow(AAPL)
a <- runif(n)
I would like to convert a to an xts object with dates equal to the dates of AAPL.
So far I wasn't able to do it by any way.
This is very simple:
a <- xts(runif(nrow(AAPL)), index(AAPL))