我最近开始研究预测包,并尝试先将其应用于一些股票数据,然后再将其传输到我的工作中。
library(quantmod)
library(forecast)
library(data.table)
#Importing historical data of Microsoft stock
my_portfolio <- "MSFT"
StockData <- lapply(my_portfolio,getSymbols,auto.assign=FALSE)
names(StockData) <- my_portfolio
head(StockData[[my_portfolio[1]]])
StockData <-StockData$MSFT
StockData <-data.frame(StockData[,4])
setDT(StockData, keep.rownames = TRUE)[]
setnames(StockData, 1, "Date")
StockData$Date <- as.Date(StockData$Date)
#Creating the time-series object
TimeSeries <- ts(StockData, frequency=365)
#Fitting the Exponential smoothing
fit2 <- ets(TimeSeries, model="ZZZ")
TimeSeriesFC <- forecast(fit2,h=60,model="ZZZ")
plot(forecast(fit2,h=60,model="ZZZ"))
#Performing accuracy tests
accuracy(TimeSeriesFC)
#Running time-series cross validation
e <- tsCV(TimeSeries, function(x,h){forecast(ets(x),h)}, h=60)
mse <- colMeans(e^2, na.rm = TRUE)
#Union 2 data frames
Union <- ts.union(TimeSeries,TimeSeriesFC$mean, dframe=TRUE)
我遇到2个错误。第一个是应用tsCV时
Error in ets(TimeSeries, model = "ZZZ") : y should be a univariate time series
加入两个时间序列时的第二个。
Error in .cbind.ts(list(...), .makeNamesTs(...), dframe = dframe, union = TRUE) not all series have the same frequency
您能帮我解决问题,并可能解释我做错了什么吗?
预先谢谢你!