简体   繁体   English

使用R中的for循环将时间序列函数的值存储在数组中

[英]Store values from a time series function in an array using a for loop in R

I am working with Bank of America time series data for stock prices. 我正在使用美国银行时间序列数据获取股票价格。 I am trying to store the forecasted value for a specific step ahead (in this case 1:20 steps) in an array. 我正在尝试在数组中存储前面特定步骤(在这种情况下为1:20步骤)的预测值。 I then need to subtract each value of the array from each value of the test array. 然后,我需要从测试数组的每个值中减去数组的每个值。 Then I have to square each value of the array, sum all the squared values of the array, then divide by N (N = number of steps forecasted ahead). 然后,我必须对数组的每个值求平方,对数组的所有平方值求和,然后除以N(N =预先预测的步数)。

I have the following so far. 到目前为止,我有以下内容。 Also, the quantmod and fpp libraries are needed for this. 另外,还需要quantmod和fpp库。

---------Bank of America---------- - - - - -美国银行 - - - - -

library(quantmod)
library(fpp)

BAC = getSymbols('BAC',from='2009-01-02',to='2014-10-15',auto.assign=FALSE)
BAC.adj = BAC$BAC.Adjusted
BAC.daily=dailyReturn(BAC.adj,type='log')

test = tail(BAC.daily, n = 20)
train = head(BAC.daily, n = 1437)

Trying to write a function to forecast, extract requisite value (point forecast for time i), then store it in an array where I can perform operations on that array (ie - add, multiply, exponentiate, sum the values of the array) 尝试编写要预测的函数,提取必要的值(时间i的点预测),然后将其存储在一个数组中,我可以对该数组执行操作(即-对数组的值进行加,乘,乘,求和)

MSE = function(N){
    for(i in 1:(N)){
        x = forecast(model1, h = i)
        y = x$mean
        w = as.matrix(as.double(as.matrix(unclass(y))))
        p = array(test[i,]-w[i,])
    }
}

and we also have: 而且我们还有:

model1 = Arima(train, order = c(0,2,0))
MSE = function(N){
    result = vector("list", length = (N))
    for(i in 1:(N)){
        x = forecast(model1, h = i)
        point_forecast = as.double(as.matrix(unclass(x$mean)))
        result[i] = point_forecast
    }
   result = as.matrix(do.call(cbind, result))
}

Neither of these functions have worked so far. 到目前为止,这些功能都没有起作用。 When I run the MSE function, I get the following errors: 当我运行MSE函数时,出现以下错误:

> MSE(20)
There were 19 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In result[i] = point_forecast :
number of items to replace is not a multiple of replacement length
2: In result[i] = point_forecast :
number of items to replace is not a multiple of replacement length
3: In result[i] = point_forecast :
number of items to replace is not a multiple of replacement length
4: In result[i] = point_forecast :

When I run MSE2 function, I get the following ouput: 当我运行MSE2函数时,得到以下输出:

MSE2(20) [1] -0.15824 MSE2(20)[1] -0.15824

When putting a print statement inside, it printed out 'p' as a singular number, just like above (even though that had been run for i = 20). 像上面一样,在内部放入打印语句时,它会以单数形式打印出“ p”(即使已将i = 20运行)。 The x,y, and w variable in the MSE2 function act as vectors as far as storing the output, so I do not understand why p does not as well. 就存储输出而言,MSE2函数中的x,y和w变量充当矢量,因此我不明白为什么p也是如此。

I appreciate any help in this matter, thank you. 感谢您在此问题上的帮助。

Sincerely, 此致

Mitchell Healy 米切尔·希利(Mitchell Healy)

Your question has two MSE functions: one in the first code block and one in the second code block. 您的问题具有两个MSE功能:第一个代码块中的一个,第二个代码块中的一个。

Also, library(forecast) is needed to run Arima and forecast . 另外,需要使用library(forecast)来运行Arimaforecast

My understanding of what you are trying to do in the first paragraph is to compute the 20-step ahead forecast error. 在第一段中,我对您要执行的操作的理解是计算20步提前的预测误差。 That is, what is the error in forecasts from model1 20 days ahead, based on your test data. 也就是说,根据您的测试数据,未来20天来自model1预测中的错误是什么。 This can be done in the code below: 这可以在下面的代码中完成:

model1 <- Arima(train, order = c(0,2,0))
y_fcst<-forecast(model1,h=20)$mean
errors<-as.vector(y_fcst)-as.vector(test)
MSE.fcst<-mean(errors^2)

However, I'm not sure what you're trying to do here: an ARIMA(0,2,0) model is simply modelling the differences in returns as a random walk. 但是,我不确定您要在这里做什么:ARIMA(0,2,0)模型只是将收益差异建模为随机游走。 That is, this model just differences the returns twice and assumes this twice-differenced data is white noise. 也就是说,该模型仅将两次收益差相乘,并假定两次差数据是白噪声。 There's no parameters other than $\\sigma^2$ being estimated. 除了估计$ \\ sigma ^ 2 $之外,没有其他参数。

Rob Hyndman has a blog post covering computing errors from rolling forecasts . 罗伯·海德曼(Rob Hyndman)的博客文章涵盖了滚动预测中的计算错误

My solution to finding the MSE is below. 我找到MSE的解决方案如下。 I used log adjusted daily return data from Bank of America gathered through quantmod. 我使用从美国银行通过quantmod收集的经过日志调整的每日收益数据。 Then I subsetted the data (which had length 1457) into training[1:1437] and testing[1438:1457]. 然后,我将数据(长度为1457)分为训练[1:1437]和测试[1438:1457]。

The solution is: 解决方案是:

forc = function(N){
   forecast = matrix(data = NA, nrow = (N) )
   for(i in 1:N){
      fit = Arima(BAC.adj[(1+(i-1)):(1437+(i-1))], order = c(0,0,4))
      x = forecast(fit, h = 1)
      forecast[i,] = as.numeric(x$mean)
   }
   error = test - forecast
   error_squared = error^2
   sum_error_squared = sum(error_squared)
   MSE = sum_error_squared/N
   MSE
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM