[R] How to speed up the simulation when processing time and date
Faranak Golestaneh
faranak.golestaneh at gmail.com
Thu Jan 15 11:38:09 CET 2015
Dear Friends,
I am trying to program a forecasting method in R. The predictors are
weather variables in addition to lag measured Power values. The accuracy of
data is one minute and their corresponding time and date are available.
To add lag values of power to the predictors list, I am aiming to consider
last ten minutes values. If I was sure that the database is perfect and the
values for all minutes throughout the year are available I could simply
shift the Power columns but as it may not be always the case, I have used
the following codes for each time t to check if all its corresponding ten
minutes lag values are available and extract them and store in a matrix.
The problem is that, the process is highly time consuming and it takes a
long time to be simulated. Here I ve given reproducible example. I was
wondering any of you can suggest a better approach. Thank you.
rm(list = ls())
cat("\014")
st="2012/01/01"
et="2012/02/27"
st <- as.POSIXlt(as.Date(st))
et <- as.POSIXlt(as.Date(et))
time= seq(from=st, to=et,by=60)
time<as.POSIXlt(time)
#Window is the number of lag values
#leadTime is look-ahead time (forecast horizon)
leadTime=10;
Window=15;
zzzz=time[1:8000]
Total_Zone1=abind(matrix(rnorm(4000*2),4000*2,1),
matrix(rnorm(4000*2),4000*2,1), matrix(rnorm(4000*2),4000*2,1),time[1:8000])
N_Train=nrow(Total_Zone1);
lag_Power=matrix(0,N_Train,Window)
colnames(Total_Zone1) <- c( "airtemp","humidity", "Power", "time")
Total_Zone1<- as.data.frame(Total_Zone1)
for (tt in 4000:N_Train){
Statlag=Total_Zone1$time[tt]-(leadTime+Window)*60
EndLag=Total_Zone1$time[tt]-(leadTime)*60
Index_lags=which((Total_Zone1$time>Statlag)&(Total_Zone1$time<=EndLag))
if (size(Index_lags)[2]<Window) {
Statlag2=Total_Zone1$time[tt]-24*60*60
Index_lags2=which(Total_Zone1$time==Statlag2)
tem1=rep(Total_Zone1[Index_lags2,c("Power")],Window-size(Index_lags)[2])
lag_Power[tt,]=t(c(Total_Zone1[Index_lags,c("Power")],tem1))
}else{
lag_Power[tt,]=t(Total_Zone1[Index_lags,c("Power")])
}
}
[[alternative HTML version deleted]]
More information about the R-help
mailing list