I have experimental data points which I want to fit with my model which has 3 parameters (p, q, and R). Therefore I have three nested for loops to go through all possible combinations of the three parameters and to determine then the best fit (least squares of residuals). This is how I do that (with weighting = 1/exp(y) ) :
tgreparpq <- function(x, p, q, R){exp(-(p + q)*x)*(1 + x*(q + R*p) + x^2*((R*q^2)/2 + R*q*p) + x^3*R^2*q^2*p/2)}
pvalues <- seq(1e-10,1.01,0.01)
qvalues <- seq(1e-10,1.01,0.01)
repair <- seq(1e-10,1.01,0.01)
bestl <- array(dim = c(length(pvalues), length(qvalues)))
sigmapq <- array(dim = c(length(pvalues), length(qvalues)))
bestp <- vector(length = klength)
bestq <- vector(length = klength)
bestr <- array(dim = c(length(pvalues), length(qvalues)))
bestrk <- vector(length = klength)
#number of dose rates = klength
klength = 2
for (k in 1:klength){
  x <- c(0, dat$Dose[dat$Rate%in%rle(dat$Rate)$values[k]])
  SR <- vector(length = length(repair))
  for (i in 1:length(pvalues)){
    for (j in 1:length(qvalues)){
      for (l in 1:length(repair)){
    y <- tgreparpq(x,pvalues[i], qvalues[j], repair[l])
    SR[l] <- sum(((y-c(1,dat$Survival[dat$Rate%in%rle(dat$Rate)$values[k]]))*1/exp(y))^2)
      }
     bestl[i,j] <- which(SR %in% min(SR))
     sigmapq[i,j] <- SR[bestl[i,j]]
     bestr[i,j] <- repair[bestl[i,j]]
    }
  }
  besti <- which(sigmapq == min(sigmapq), arr.ind = TRUE)[1]
  bestp[k] <- pvalues[besti]
  bestj <- which(sigmapq == min(sigmapq), arr.ind = TRUE)[2]
  bestq[k] <- qvalues[bestj]
  bestrk[k] <- bestr[besti,bestj]
}
This is a rather slow process and I know you're not supposed to use that many for loops in r. Hence my question: Is there a better way to determine the fit paramters (i.e. is there a way to replace the for loops)?
Edit: Here is some example data:
dat =
Dose        Survival     Rate
1.9163E+00  6.42870E-01  3.0000E+01    
3.9713E+00  3.68150E-01  3.0000E+01    
5.9857E+00  1.76050E-01  3.0000E+01    
7.9572E+00  8.27670E-02  3.0000E+01    
1.0013E+01  2.01370E-02  3.0000E+01    
1.2015E+01  1.09200E-02  3.0000E+01
1.9683E+00  6.42530E-01  7.6800E+01    
2.9740E+00  4.86220E-01  7.6800E+01    
4.0354E+00  3.09730E-01  7.6800E+01    
5.0276E+00  2.13930E-01  7.6800E+01    
6.0851E+00  1.67200E-01  7.6800E+01    
7.0223E+00  1.04640E-01  7.6800E+01    
8.0531E+00  6.79020E-02  7.6800E+01    
9.0841E+00  3.14080E-02  7.6800E+01    
1.0135E+01  2.12510E-02  7.6800E+01    
1.1176E+01  8.39810E-03  7.6800E+01    
1.2168E+01  4.92070E-03  7.6800E+01    
1.4169E+01  1.53690E-03  7.6800E+01
 
     
    