I have a quite complicated model, which is written in several R scripts.The final function
model <- LSV_funct(s,e,h,matrix1,matrix2,vector1,A1,A2,B1,B2)
parameters s,e,h
are constants, matrix1,matrix2,vector1
are inputs values and A1,A2,B1,B2
need to be optimized, besed on the best correlation with empirical data. I therefore need to optimize these four parameters, and I am doing this simply with four nested loops. The code is below:
s = 524; e = 684; h = 40
results <- c()
A1s <- seq(1, 10, 0.5)
A2s <- seq(10, 20, 0.5)
B1s <- seq(0.01, 0.04, 0.001)
B2s <- seq(0.5, 0.9, 0.1)
for (i in 1:length(A1s)){
for (j in 1:length(A2s)){
for (y in 1:length(B1s)){
for (k in 1:length(B2s)){
A1 <- A1s[i]; A2 <- A2s[j]; B1 <- B1s[y]; B2 <- B2s[k]
matrix1 = as.matrix(read.csv("inputs1.csv",header=FALSE)) #read in matrix1
matrix2 = as.matrix(read.csv("inputs2.csv",header=FALSE)) #read in matrix2
vector1 <- c(0.3, 0.08, 0.045, 48.25, 9.32, 54, 85, 6, 15, 1250)
source(file="mypath/LSV_fun.R", chdir=T) #call the R script where the full model is written
model <- LSV_funct(s,e,h,matrix1,matrix2,vector1,A1,A2,B1,B2) #run the funtion
out_model <- (model[[1]]) #save one model output for comparing with an empirical dataset
r <- cor(out_model, empirical_dataset) #calculate correlation between modeled and observed dataset
comb <- cbind(r, A1, A2, B1, B2) #save correlation value and parameters combination
results <- rbind(results,comb)
}
}
}
}
combin <- as.data.frame(results) #save everything in a dataframe
names(combin) <- c("corr", "A1", "A2", "B1", "B2")
best <- subset(combin, combin$corr == max(combin$corr)) #and finally save the combination of parameters that give the best correlation
print(best)
The problem is that this system is not time efficient at all.It takes up to several hours to run the optimization and save the best set of parameters.
Is there a smarter function for doing the same operation but more efficiently? I had a look at the optim()
function, but I encountered difficulties to apply it for my purpose and my model (I do not have much experience in optimization algorythms...)
Thanks in advance for any suggestion!