Reputation: 605
I want to replicate a nested loop with sapply or other apply functions. I have a data set consisting of monthly returns of 100 stocks. I want to compute the sum of t-6 to t-2 monthly returns for each stock.Here t represent each observation. For this purpose I have created following nested loop. Now I want to do the same thing with apply family. I have tried but it does not work. I think I am doing mistake. Please check my code.
x <- matrix(rnorm(1e4), nrow=100, ncol=100)
s=6
k=1
XSMOM = x
XSMOM[1:nrow(XSMOM),1:ncol(XSMOM)] <- NA
# Using nested loops
for (i in 1:ncol(x)){
for (t in (s + 1):nrow(x)){
XSMOM[t,i] = sum(x[(t-s):(t-1-k),i])
}
}
## using sapply
sapply(1:ncol(x),function(m)
sapply(s+1:nrow(x),function(n)
sum(x[(n-s):(n-s-k),m])
Upvotes: 3
Views: 535
Reputation: 11255
There are a few errors in the code. One note is that you should provide a minimal example.
x <- matrix(rnorm(50), nrow=10, ncol=5)
XSMOM <- matrix(NA_real_, ncol = ncol(x), nrow = nrow(x))
s=6; k=1
sapply(1:ncol(x),
function(i) { # need curly bracket; changed var from m to i to match loop
sapply((s+1):nrow(x),function(t) { # need curly bracket; changed from n to t
sum(x[(t-s):(t-1-k),i]) # copied original loop function; you had n-s-k
})
})
For more speed you can look into rcpproll or data.table
library(data.table)
simplify2array(shift(frollsum(as.data.frame(x), n = s-1), 2))
library(RcppRoll)
XSMOM <- matrix(NA_real_, ncol = ncol(x), nrow = nrow(x))
XSMOM[-(1:s), ] <- roll_sumr(x, n = s-1)[(s-1):(nrow(x) - k - 1), ]
XSMOM
Performance of everything:
# for x <- matrix(rnorm(1E4), nrow=100, ncol=100)
# A tibble: 6 x 13
expression min median `itr/sec` mem_alloc
<bch:expr> <bch:t> <bch:t> <dbl> <bch:byt>
1 original_loop 19.8ms 20.5ms 48.2 140.71KB
2 double_sapply 27.2ms 27.7ms 35.1 624.49KB
3 apply_sapply 20.5ms 21.1ms 46.5 827.84KB
4 zoo_rollapply 120.6ms 122.1ms 8.19 11.04MB
5 rcpp_roll 243.6us 250.8us 3771. 400.53KB
6 dt_froll_shift 720.3us 806.9us 1186. 2.01MB
# code for reference
library(data.table)
library(zoo)
library(RcppRoll)
library(bench)
x <- matrix(rnorm(1E4), nrow=100, ncol=100)
# x <- matrix(rnorm(50), nrow=10, ncol=5)
s=6
k=1
XSMOM <- matrix(NA_real_, ncol = ncol(x), nrow = nrow(x))
bench::mark(
original_loop = {
XSMOM <- matrix(NA_real_, ncol = ncol(x), nrow = nrow(x))
for (i in 1:ncol(x)){
for (t in (s + 1):nrow(x)){
XSMOM[t,i] = sum(x[(t-s):(t-1-k),i])
}
}
XSMOM
}
,
double_sapply = {
XSMOM <- matrix(NA_real_, ncol = ncol(x), nrow = nrow(x))
XSMOM[-(1:s), ] <- sapply(1:ncol(x),
function(i) {
sapply((s+1):nrow(x),function(t) {
sum(x[(t-s):(t-1-k),i])
}
)
}
)
XSMOM
}
,
apply_sapply = {
XSMOM <- matrix(NA_real_, ncol = ncol(x), nrow = nrow(x))
XSMOM[-(1:s), ] <- apply(x, 2,
function(col) {
sapply((s+1):nrow(x), function(t) {
sum(col[(t-s):(t-1-k)])
})
})
XSMOM
}
,
zoo_rollapply = {
# XSMOM <- rollapplyr(x,
# by.column = T,
# width = list(-s:-(k + 1)),
# sum,
# fill = NA)
XSMOM <- matrix(NA_real_, ncol = ncol(x), nrow = nrow(x))
XSMOM[-(1:s), ] <-head(rollsumr(x, by.column = T, k = s-1), -(k+1))
XSMOM
}
,
rcpp_roll = {
XSMOM <- matrix(NA_real_, ncol = ncol(x), nrow = nrow(x))
XSMOM[-(1:s), ] <- roll_sumr(x, n = s-1)[(s-1):(nrow(x) - k - 1), ]
XSMOM
}
,
dt_froll_shift = {
simplify2array(shift(frollsum(as.data.frame(x), n = s-1), 2))
}
)
Upvotes: 2