在自创的r函数中以矩阵形式获得结果 [英] Obtaining results as a matrix in a self created r function
问题描述
下面的代码运行,但是不是返回(950数字值)我期望(想要)。在以下代码中, result
是72行3列的矩阵。 作为一个输出,我想要一个有72列和 长度(点)
行的矩阵。我尝试初始化 fcasts< - matrix(0,nrow = length(spots),ncol = 72)
,但代码不运行。
$ $ p $ y <-ts(rnorm(1000),start = 1850,frequency = 1)
库(预测)
#寻找最佳模型
order.matrix< -matrix(0,nrow = 3,ncol = 6 * 2 * 6)
aic.vec< ;(0:5中的(p为0:5))中的数字(6 * 2 * 6)
k <-1
(a,b,a,a,a,a,a,a,a,a,a,a, q)))
k <-k + 1
}
ind < - order(aic.vec,decrease = F)
aic.vec < - aic.vec [ind ]
order.matrix< - order.matrix [,ind]
order.matrix< -t(order.matrix)
result< - cbind(order.matrix,aic.vec)
SlideFunction< - 函数(数据,窗口,步长){
总计< - 长度(数据)
个点< - seq(from = 1 ,到=(总窗口),by = step)
fit < - vector(mode =list,length = 72)
#fcasts < - matrix(0,nrow = length(spots) ,ncol = 72)
fcasts< - 数字(长度(点))
for(in 1:length(spots)){
fo r(j in 1:72){
p < - result [j,1]
d < - result [j,2]
q < - result [j, 3]
注意我使用Arima(在预测包中)不是arima
fit [[j]]< - Arima(data [spots [i] :( spots [i] +窗口)],order = c(p,d,q),method =CSS)
fcasts [i] < - forecast(fit [[j]],h = step) $ mean
#I预期(想要)每个fcasts提前一步预测,即每个arima模型的商店价值。
$ return(fcasts)
SlideFunction(y,50,1)
有人可以帮我解决这个问题吗?
pre $
SlideFunction< - function(data,window,step){$ (总数窗口),由=步骤)
fcasts < - 矩阵(0,nrow =长度(数据)
个点<-seq(从= 1到= ($)
for(in 1:length(spots)){
for(j in 1:72){
p < - result [j,1] $结果[j,3]
fit< - Arima(data [spots [i] :( spots [i] + window) ],order = c(p,d,q),method =CSS)
fcasts [i,j] < - 预测(fit,h = step)$ mean
}
}
return(fcasts)
}
SlideFunction(y,50,1)
但完成需要相当长的时间,我没有耐心等待。我尝试使用较小的数据集:
$ $ p $ y <-ts(rnorm(100),start = 1850,frequency = 1)
和5而不是72,知道结果是正确的。
The following code runs, but does not return(950 numeric values) what I expected(want). In the following code result
is a matrix of 72 rows and 3 columns. As an output, I would like to have a matrix with 72 columns and length(spots)
rows. I tried initialising fcasts<- matrix(0, nrow=length(spots), ncol=72)
, but then code does not run.
y<- ts(rnorm(1000),start = 1850, frequency=1)
library(forecast)
#Searching for the best models
order.matrix<-matrix(0,nrow = 3, ncol = 6 * 2 * 6)
aic.vec<- numeric(6 * 2 * 6)
k<-1
for(p in 0:5) for(d in 0:1) for(q in 0:5){
order.matrix[,k]<-c(p,d,q)
aic.vec[k]<- AIC(Arima(y, order=c(p,d,q)))
k<-k+1
}
ind<- order(aic.vec,decreasing=F)
aic.vec<- aic.vec[ind]
order.matrix<- order.matrix[,ind]
order.matrix<- t(order.matrix)
result<- cbind(order.matrix,aic.vec)
SlideFunction<- function(data, window, step){
total<- length(data)
spots<- seq(from=1, to=(total-window), by=step)
fit<- vector(mode="list",length=72)
#fcasts<- matrix(0,nrow=length(spots),ncol=72)
fcasts<- numeric(length(spots))
for(i in 1:length(spots)){
for(j in 1:72){
p<- result[j,1]
d<- result[j,2]
q<- result[j,3]
#Note I am using Arima(in forecast package) not arima
fit[[j]]<- Arima(data[spots[i]:(spots[i]+window)], order=c(p,d,q), method="CSS")
fcasts[i]<- forecast(fit[[j]], h=step)$mean
#I expected(would like to have) 72 one-step ahead prediction for each fcasts i.e, store values for each arima model.
}
}
return(fcasts)
}
SlideFunction(y,50,1)
Can someone please help me with this?
I think this is what you want:
SlideFunction<- function(data, window, step) {
total<- length(data)
spots<- seq(from=1, to=(total-window), by=step)
fcasts<- matrix(0,nrow=length(spots),ncol=72)
for(i in 1:length(spots)) {
for(j in 1:72){
p<- result[j,1]
d<- result[j,2]
q<- result[j,3]
fit <- Arima(data[spots[i]:(spots[i]+window)], order=c(p,d,q), method="CSS")
fcasts[i, j] <- forecast(fit, h=step)$mean
}
}
return(fcasts)
}
SlideFunction(y,50,1)
But it takes quite a long time to finish and I don't have patience to wait. I tried using smaller data set:
y<- ts(rnorm(100),start = 1850, frequency=1)
and 5 instead of 72, knowing that the result is correct.
这篇关于在自创的r函数中以矩阵形式获得结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!