我正在开展一个使用特定计量经济学测试的项目,并且应用了越来越多的时间范围窗口。
我已经为线性计算编写了一段代码,在安装了16M RAM的核心i5 PC上获得结果大约需要20秒。为了加快计算速度,我尝试使用并行计算,但之前从未使用过它。 Windows环境有一些限制,我得到的最好结果是14秒。与20秒相比,4核计算的速度太慢了。对于单核心。有没有办法进一步提高计算速度?或许,还有另一种解决方案可以获得相同的结果吗?
# Proxy data (imitation of the real data in the project)
library (xts)
library (egcm)
library (urca)
library(foreach)
library(doSNOW)
#Generating source matrices
adf.data<-as.data.frame(matrix(ncol=7,nrow=250))
names(adf.data)[1:7]<-c("Hours","Adf.Process.A","Adf.Process.B","Adf.Process.C","Coeff.A","Coeff.B","Coeff.C")
source.data<-as.data.frame(matrix(ncol=4,nrow=10000))
names(source.data)[1:4]<-c("Time","Process.A","Process.B","Process.C")
start.time<-Sys.time()
source.data$Time<-start.time
for (i in seq (1,10000, by=1)) {source.data$Time[i]<-as.POSIXct(start.time-i*60*5,format="%Y.%m.%d %H:%M")}
source.data$Process.A<-runif(10000, 14000, 15000)
source.data$Process.B<-runif(10000, 10040, 12000)
source.data$Process.C<-runif(10000, 11000, 14000)
source.data<-as.xts(source.data,as.POSIXct(source.data$Time))
source.data$Time<-NULL
# Core calculations
comb<-3 # number of combinations of processes
cl<-makeCluster(4)
registerDoSNOW(cl)
start.timer.loop<-Sys.time()
r<- foreach(k =1:250, .packages = c("xts", "tseries","egcm","urca")) %dopar%
{
chunk.set<-last(source.data, paste(k, " hour", sep=""))
# adf-test for all processes (with an increasing window by 1 hour each time)
adf.data[k,1]<-k
adf.data[k,2]<-round(adf.test(chunk.set[,1], k=2)$p.value,3)
adf.data[k,3]<-round(adf.test(chunk.set[,2], k=2)$p.value,3)
adf.data[k,4]<-round(adf.test(chunk.set[,3], k=2)$p.value,3)
# Johansen cointegration procedure for all processes
mode(chunk.set)<-"numeric"
coint<-ca.jo(x=chunk.set, type="trace", K=2, ecdet="const")
coeffs<-vector()
coeffs<-coint@V[1:comb]
scale<-min(abs(coeffs[1:comb]))
adf.data[k,5:7]<-coeffs[1:comb]/scale # scaled coefficients of the Johansen procedure are put in the same output matrix.
}
stopCluster(cl)
end.timer.loop<-Sys.time()
calc.time<-round(end.timer.loop-start.timer.loop,1)
print(calc.time)