3
votes

I downloaded MSFT historic daily stock data using quantmod package. What I got is xts/zoo object. I want to convert it to ts object, so that I can do daily price forecasting with forecast package.

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 is xts/zoo object
msft <- MSFT[, 'MSFT.Close']

#convert msft to ts object
msft.ts <-ts(as.numeric(msft), 
            start=c(2018, yday(start(msft))), 
            frequency = 365)

index of msft (xts object) look like below. They are weekly data with weekend missing. Obviously stock only trade on weekdays.

[1] "2018-01-02" "2018-01-03" "2018-01-04" "2018-01-05" "2018-01-08"
  [6] "2018-01-09" "2018-01-10" "2018-01-11" "2018-01-12" "2018-01-16"
 [11] "2018-01-17" "2018-01-18" "2018-01-19" "2018-01-22" "2018-01-23"

index of msft.ts (ts object) look like this:

[1] 2018.003 2018.005 2018.008 2018.011 2018.014 2018.016 2018.019 2018.022
  [9] 2018.025 2018.027 2018.030 2018.033 2018.036 2018.038 2018.041 2018.044
 [17] 2018.047 2018.049 2018.052 2018.055 2018.058 2018.060 2018.063 2018.066

I'm confused about what those index mean. Are the digits after 2018. the day number? Those does not seem to be right. My guess is it might not be because I set the frequency to be 365, but actually there is not data on weekends. In this case, what should I do? I googled and find that ts only work for evenly spaced data. But in order to use forecasting package, I need to supply ts object, although it looks like I lose all the date info after conversion from xts to ts object. I really appreciate it if anyone could kindly clarify me on this. What is the right way to do? I'm really confused. I want to make a forecast model using forecast package. Thanks a lot in advance.

1
@李哲源 The time index of the ts object does not know about weekends, and will put the monday exactly one day after friday. Depending on your model this might be fine, or you might want to add NA values on those days. ts objects only allow for a regular index by design (that's why getSymbols returns xts instead).Chris Haug
@Chris Haug why day numbers in the ts are 2 or 3 apart ? why, for example 2018-01-03 is 2018-005 (second entry)? I'm realy confused.zesla
You are right! diff(index(ms)) are all 1's. How come it print out differently?zesla
I remember R forecast package need ts object as its input. Here I have daily stock price as xts object, I want to try arima model, how should I deal with it?zesla
diff(index(msft.ts)) can't not be the same value because, as I said, ts only allows for a regular index: it doesn't store an explicit index at all, just a start, stop and frequency in tsp. Have a look at the function zoo::index.ts to see how the index is inferred.Chris Haug

1 Answers

0
votes

What you want to do is keep the dates from your MSFT timeseries and add to them. You can use the package timetk for this. Or if you want, the sweep package, until the tidy forecasting package fable is available. timetk works very well with tidyquant. You can use tk_tbl to turn a timeseries into a tibble.

library(quantmod)
library(forecast)

start <- as.Date('2018-01-01')
end <- as.Date('2018-08-14')
getSymbols('MSFT', src='yahoo', from=start, to=end)

# forecast    
my_aa <- auto.arima(Cl(MSFT))
my_forecast = forecast(my_aa, h = 10, level = 95)

library(timetk)
time_index <- tk_index(MSFT)
# future days need to be the same as used in the forecast, but because we don't want weekends we
# need to make sure we have enough records so 30 should cover it.
time_index_future <- tk_make_future_timeseries(time_index, n_future = 30, inspect_weekdays = T)

my_fc_future <- cbind(forecast = my_forecast$mean, forecast_low = my_forecast$lower, forecast_high = my_forecast$upper)
# select the needed number of records from the index
my_xts_future <- xts(my_fc_future , time_index_future[1:nrow(my_fc_future)])
my_xts_future

           forecast forecast_low forecast_high
2018-08-14 108.6679     105.8490      111.4868
2018-08-15 108.8136     105.3287      112.2985
2018-08-16 108.9593     104.9167      113.0019
2018-08-17 109.1050     104.5728      113.6372
2018-08-20 109.2507     104.2768      114.2246
2018-08-21 109.3964     104.0170      114.7758
2018-08-22 109.5421     103.7857      115.2985
2018-08-23 109.6878     103.5776      115.7980
2018-08-24 109.8335     103.3889      116.2781
2018-08-27 109.9792     103.2168      116.7417

# merge forecast data with stock data
MSFT2 <- merge(MSFT, my_xts_future)

tail(MSFT2, 12)

           MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted forecast forecast_low forecast_high
2018-08-10    109.42    109.69   108.38     109.00    18183700      108.5821       NA           NA            NA
2018-08-13    109.24    109.58   108.10     108.21    18472500      107.7952       NA           NA            NA
2018-08-14        NA        NA       NA         NA          NA            NA 108.6679     105.8490      111.4868
2018-08-15        NA        NA       NA         NA          NA            NA 108.8136     105.3287      112.2985
2018-08-16        NA        NA       NA         NA          NA            NA 108.9593     104.9167      113.0019
2018-08-17        NA        NA       NA         NA          NA            NA 109.1050     104.5728      113.6372
2018-08-20        NA        NA       NA         NA          NA            NA 109.2507     104.2768      114.2246
2018-08-21        NA        NA       NA         NA          NA            NA 109.3964     104.0170      114.7758
2018-08-22        NA        NA       NA         NA          NA            NA 109.5421     103.7857      115.2985
2018-08-23        NA        NA       NA         NA          NA            NA 109.6878     103.5776      115.7980
2018-08-24        NA        NA       NA         NA          NA            NA 109.8335     103.3889      116.2781
2018-08-27        NA        NA       NA         NA          NA            NA 109.9792     103.2168      116.7417