I am using two loops. In the second loop, I am incrementing value by 1 and then applying filter based on that and converting it to data.matrix so that matrix multiplication can be done in later steps. Is there any way to make it efficient using lapply, expand.grid or any other method?
xx <- structure(list(Ars_0 = c(1308.56, 5728.84, 2177.82), Ars_1 = c(0, 0, 0),
Ars_2 = c(0, 0, 0), age = c(13, 31, 43), region = c('A','A','B')),
row.names = c(NA, -3L),
class = "data.frame")
mx_long2 = read.table(header = T, text = '
Arrears Ars_0 Ars_1 Ars_2 Seasoning Region
Ars_0 0.985 0.0148 0.0002 mths:36-47 A
Ars_1 0.3816 0.286 0.3317 mths:36-47 A
Ars_2 0.2959 0.0057 0.2524 mths:36-47 A
Ars_0 0.9822 0.0176 0.0002 mths:24-35 A
Ars_1 0.389 0.2753 0.3347 mths:24-35 A
Ars_2 0.3026 0.0334 0.2399 mths:24-35 A
Ars_0 0.9753 0.0243 0.0004 mths:12-23 A
Ars_1 0.4002 0.2592 0.3394 mths:12-23 A
Ars_2 0.3032 0.0208 0.2387 mths:12-23 A
Ars_0 0.8865 0.01332 0.00018 mths:36-47 B
Ars_1 0.34344 0.2574 0.29853 mths:36-47 B
Ars_2 0.26631 0.00513 0.22716 mths:36-47 B
Ars_0 0.88398 0.01584 0.00018 mths:24-35 B
Ars_1 0.3501 0.24777 0.30123 mths:24-35 B
Ars_2 0.27234 0.03006 0.21591 mths:24-35 B
Ars_0 0.87777 0.02187 0.00036 mths:12-23 B
Ars_1 0.36018 0.23328 0.30546 mths:12-23 B
Ars_2 0.27288 0.01872 0.21483 mths:12-23 B
')
mx_long2 = mx_long2 %>% mutate(minage = as.numeric(substr(as.character(Seasoning), 6,7)),
maxage = as.numeric(substr(as.character(Seasoning), 9,10)))
x <- xx %>% select(starts_with('Ars')) %>% data.matrix()
l <- list()
p <- 1
for (i in 1:nrow(x)) {
for (j in 1:3) {
Bx = filter(mx_long2, (j + xx[i, 'age']) >= minage, (j + xx[i, 'age']) <= maxage,
Region == xx[i, 'region']) %>%
select(starts_with('Ars_')) %>% data.matrix()
# Matrix Multiplication
x <- x %*% Bx
l[[p]] <- x
p = p + 1
}
}
l