Dear all,
I produced a part of code which does what needed but by estimations I would
need to wait for that for 300 hours!!!
Maybe someone can give a me a glance advice or something.
Thanks a lot!
I need to define maximum adjusted R-squares from regression model: returns
as dependent variable( here simulated to be normal) and 3 factors out of say
14 independent (simulated here). I need to run lm model for all possible
combinations of factors and this way find the max adj R-squared. This should
be done 100 times. Below I provide my code for that. It takes around 3
minutes to calculate, while I need to repeat the whole procedure for 6000
stocks!
library(Combinations)
# 14 factors with returns normally distributed
factors.r <- matrix(data = rnorm(200), nrow = 200, ncol = 14)
# simulated normal returns (nrows equals to #of simulations, ncol length of
history)
simul <- matrix(data = rnorm(200), nrow = 100, ncol = 200)
# possible combinations of 3 out of 14 factors
# 364 different combinations
fact <- combinations(14, 3)
# Elements of matrix are whole vectors of factors' returns each row
corresponds to the combination of factors from matrix "factors"
AAA <- matrix(as.list(factors.r[, t(fact)]), nrow = 364, ncol = 3)
# Function to calculate R-squared for the linear regression model
AdjRsqrFunc <- function(x, a){
summary(lm(a ~ x[[1]] + x[[2]] + x[[3]]))$adj.r.squared
}
max.seq <- NA
# I need to check all the combinations of factors and find the model
which brings the highest adjusted R-squared
# for every simulation out of 100
for (i in 1:100){
xyz <- apply(AAA, 1, AdjRsqrFunc, a = simul[i, ])
max.seq <- c(maxrsq.seq, max(xyz))
}
[[alternative HTML version deleted]]