2015-10-26 36 views
2

我想測試不同長度的移動平均值與因變量之間的相關性。我已經寫了一個for循環來完成工作,但顯然對於循環不是理想的解決方案。我想知道是否有人可以給我一些關於如何替換這個for循環的功能的指針,作爲一個更優雅的解決方案?我提供了代碼和測試數據。替換適用於R的滾動平均值

library(zoo) 

# a function that calculates the correlation between moving averages for 
different lengths of window 
# the input functions are "independent": the variable over which to apply the 
moving function 
# "dependent": the output column, "startLength": the shortest window length, 
"endLength" the longest window length 
# "functionType": the function to apply (mean, sd, etc.) 

MovingAverageCorrelation <- function(indepedent, depedent, startLength, endLength, functionType) { 
# declare an matrix for the different rolling functions and a correlation vector 
avgMat <- matrix(nrow = length(depedent), ncol = (endLength-startLength+1)) 
corVector <- rep(NA, ncol(avgMat)) 
# run the rollapply function over the data and calculate the corresponding correlations 
for (i in startLength:endLength) { 
    avgMat[, i] <- rollapply(indepedent, width = i, FUN = functionType, 
         na.rm = T, fill = NA, align = "right") 
    corVector[i] <- cor(avgMat[, i], depedent, use = "complete.obs") 
    } 
return(corVector) 
} 

# set test data 

set.seed(100) 
indVector <- runif(1000) 
depVector <- runif(1000) 

# run the function over the data 

cor <- MovingAverageCorrelation(indVector, depVector, 1, 100, "mean") 

謝謝!

回答

2

嘗試sapply

sapply(1:100, function(i) cor(rollapplyr(indVector, i, mean, na.rm = TRUE, fill = NA), 
     depVector, use = "complete.obs")) 

如果在你的投入沒有來港,這將工作,基本上是速度快:

sapply(1:100, function(i) cor(rollmeanr(indVector, i, fill = NA), depVector, use = "comp")) 
+0

真棒。謝謝。不幸的是我有NA,所以我必須使用前者。 – TSW