首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制图表时不显示ARIMA预测

绘制图表时不显示ARIMA预测
EN

Stack Overflow用户
提问于 2021-06-04 14:06:59
回答 1查看 77关注 0票数 0

我是一个新手程序员,试图建立一个预测模型来预测苹果的股票价格。我使用这个脚本作为指导:https://github.com/inertia7/timeSeries_sp500_R/blob/master/src/script.R

我能够复制大多数部分,但当我们绘制预测数据的最后部分时,它只绘制我的训练数据集。我做错了什么?如果您有任何需要澄清的额外信息,请告诉我!

谢谢!!

代码语言:javascript
复制
# Import stock price data using existing R packages
getSymbols('AAPL', from='2018-01-01') 

# Use Augmented Dickey-Fuller Test for stationarity. To run time series models, we must adjust for non-stationarity.
adf.test(AAPL$AAPL.Adjusted)

#Augmented Dickey-Fuller Test
#data:  AAPL$AAPL.Adjusted
#Dickey-Fuller = -2.0226, Lag order = 9, p-value = 0.5687
#alternative hypothesis: stationary

#p-value is high, so we accept null hypothesis (non-stationarity). Let's visualize the data further.

# Create training set to which we'll compare values for 2021
aapl_adjusted_only = AAPL[,6] # Subset to only adjusted close price column
aapl_training = ts(aapl_adjusted_only, start=c(2018,1), end=c(2021,5), frequency = 253)

# 253 = number of trading days in a year

# Let's decompose this data set to check for any trends.
plot(decompose(aapl_training))

#From the decomposed time series of FB, we see that there is a component of seasonality here that we may need to adjust for.

#Take the difference in daily closing values to account for the non-stationary nature of the data set.
tsDiff <- diff(aapl_training)

plot(tsDiff,
     main = "Apple First Difference Time Series (2016-2021)",
     xlab = "Year",
     ylab = "Closing Values")

adf.test(tsDiff) #Run the ADF test again to test for stationary data

#Augmented Dickey-Fuller Test
#data:  tsDiff
#Dickey-Fuller = -8.6073, Lag order = 9, p-value = 0.01
#alternative hypothesis: stationary

#p-value < 0.05, so we reject the null hypothesis and accept the alternative hypothesis.


#DIAGNOSING ACF AND PACF PLOTS
ggAcf(aapl_training)
ggPacf(aapl_training)

#This means that our transformed data is stationary, and we can use the ARIMA model for forecasting.

auto.arima(aapl_training) #Select best fit ARIMA model

#Series: aapl_training 
#ARIMA(1,1,0) with drift 

#Coefficients:
#  ar1   drift
#-0.1538  0.1167
#s.e.   0.0358  0.0512

#sigma^2 estimated as 2.667:  log likelihood=-1455.84
#AIC=2917.68   AICc=2917.71   BIC=2931.59

# Now let's fit our model using the Arima method and training set
fit = Arima(aapl_training, order = c(1,1,0),
            include.drift = TRUE)

summary(fit)

#Training set error measures:
#                        ME     RMSE      MAE         MPE     MAPE       MASE        ACF1
#Training set -0.0001032566 1.629771 1.011449 -0.09118978 1.495376 0.03936189 0.003590521



# Test set that we will compare our forecast against
aapl_test = ts(aapl_adjusted_only, 
                    start = c(2021, 1), 
                    frequency = 253)

# FORECASTING
fit_arima = forecast(fit, h = 253)

if (is.null(here("models", 'arima.rds'))){
  saveRDS(fit_arima, file = here("models", 'arima.rds'))
}


forecastAAPL = autoplot(fit_arima,
                      holdout = aapl_test, 
                      forc_name = 'ARIMA', 
                      ts_object_name = 'Apple')

ggplotly(forecastAAPL)

这里是一些关于变量的str(),以便进一步澄清。

代码语言:javascript
复制
> str(aapl_adjusted_only)
 num [1:860] 41.3 41.3 41.5 42 41.8 ...
> str(aapl_test)
 Time-Series [1:860] from 2021 to 2029: 41.3 41.3 41.5 42 41.8 ...
> str(aapl_training)
 Time-Series [1:764] from 2018 to 2021: 41.3 41.3 41.5 42 41.8 ...
> str(tsDiff)
 Time-Series [1:763] from 2018 to 2021: -0.00719 0.19186 0.47243 -0.15588 -0.00479 ...
> str(fit_arima)
List of 10
 $ method   : chr "ARIMA(1,1,0) with drift"
 $ model    :List of 19
  ..$ coef     : Named num [1:2] -0.154 0.117
  .. ..- attr(*, "names")= chr [1:2] "ar1" "drift"
  ..$ sigma2   : num 2.67
  ..$ var.coef : num [1:2, 1:2] 1.28e-03 2.58e-06 2.58e-06 2.62e-03
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "ar1" "drift"
  .. .. ..$ : chr [1:2] "ar1" "drift"
  ..$ mask     : logi [1:2] TRUE TRUE
  ..$ loglik   : num -1456
  ..$ aic      : num 2918
  ..$ arma     : int [1:7] 1 0 0 0 253 1 0
  ..$ residuals: Time-Series [1:764] from 2018 to 2021: 0.0412 -0.1224 0.0562 0.3673 -0.2178 ...
  ..$ call     : language Arima(y = aapl_training, order = c(1, 1, 0), include.drift = TRUE, xreg = 1:764)
  ..$ series   : chr "aapl_training"
  ..$ code     : int 0
  ..$ n.cond   : int 0
  ..$ nobs     : int 763
  ..$ model    :List of 10
  .. ..$ phi  : num -0.154
  .. ..$ theta: num(0) 
  .. ..$ Delta: num 1
  .. ..$ Z    : num [1:2] 1 1
  .. ..$ a    : num [1:2] 1.97 39.38
  .. ..$ P    : num [1:2, 1:2] 0.00 4.39e-22 4.39e-22 -4.39e-22
  .. ..$ T    : num [1:2, 1:2] -0.154 1 0 1
  .. ..$ V    : num [1:2, 1:2] 1 0 0 0
  .. ..$ h    : num 0
  .. ..$ Pn   : num [1:2, 1:2] 1.00 6.75e-23 6.75e-23 -4.39e-22
  ..$ aicc     : num 2918
  ..$ bic      : num 2932
  ..$ xreg     : int [1:764, 1] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr "drift"
  ..$ x        : Time-Series [1:764] from 2018 to 2021: 41.3 41.3 41.5 42 41.8 ...
  ..$ fitted   : Time-Series [1:764] from 2018 to 2021: 41.3 41.4 41.4 41.6 42 ...
  ..- attr(*, "class")= chr [1:3] "forecast_ARIMA" "ARIMA" "Arima"
 $ level    : num [1:2] 80 95
 $ mean     : Time-Series [1:253] from 2021 to 2022: 130 130 131 131 131 ...
 $ lower    : Time-Series [1:253, 1:2] from 2021 to 2022: 128 128 127 127 127 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "80%" "95%"
 $ upper    : Time-Series [1:253, 1:2] from 2021 to 2022: 132 133 134 134 135 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "80%" "95%"
 $ x        : Time-Series [1:764] from 2018 to 2021: 41.3 41.3 41.5 42 41.8 ...
 $ series   : chr "aapl_training"
 $ fitted   : Time-Series [1:764] from 2018 to 2021: 41.3 41.4 41.4 41.6 42 ...
 $ residuals: Time-Series [1:764] from 2018 to 2021: 0.0412 -0.1224 0.0562 0.3673 -0.2178 ...
 - attr(*, "class")= chr "forecast"
EN

回答 1

Stack Overflow用户

发布于 2021-06-07 09:57:07

autoplot返回的forecastAAPL是一个ggplot对象。

您可以简单地在控制台或print(forecastAAPL)中输入forecastAAPL,它将生成预测和训练数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67832222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档