0

I am new to R. Recently I've been trying to implement a multiquadric RBF interpolation process. I used "optimize" to try to minimize the squared error (SSE) to find the best fit. However, one problem I encountered was that the weights (w) shows NA values after the loop was completed. So I placed a break point and tried to debug. I looked at a few iterations, the w matrix was fine within the function. If I switch to the global environment, the w matrix shows NA values still. Any suggestions?

The following is the code I used:

SSE <- function(epsilon) {
 for (lin in 1:N) {
  for (col in 1:N) {    
   Phi[lin,col] <- 0
   for (RD in 1:C){
    Phi[lin,col] <- Phi[lin,col] + (CalibScen[lin,RD]-CalibScen[col,RD])^2 * 
                                        Scale_factor[RD,1]^2
   }

   Phi[lin,col] <- rbf.multi(sqrt(Phi[lin,col]),epsilon)
  }          
 }

w <- solve(Phi, Calibdata)`  #Solve for RBF weights


# determine sum squared error

for (j in 1:N) {
 for (k in 1:N){
  SumDiff[k] <-0
  for (RD in 1:C){
   SumDiff[k] <- SumDiff[k] + (CalibScen[j,RD]-CalibScen[k,RD])^2 * 
                               Scale_factor[RD,1]^2
  }
  rbf[k] <- rbf.multi(sqrt(SumDiff[k]),epsilon)

 }
 Est[j] <- t(w)%*%rbf
 SquaredError[j] <- ((Calibdata[j] - Est[j]))^2
}

SSE <- sum(SquaredError[j])
}

best_epsilon <- optimize(SSE,c(0.1,2.0),maximum = FALSE, tol = 0.001)
joran
  • 169,992
  • 32
  • 429
  • 468
Katherine
  • 111
  • 4
  • You can find out how to fix this by reading this help page ``?`<<-` `` – G5W Mar 27 '17 at 20:05
  • 2
    I think you would be much better served by reading about how variable scope works in functions and writing functions that explicitly return objects rather than using an ill-advised tool like `<<-`. – joran Mar 27 '17 at 20:10
  • Welcome to SO! How one can reproduce the issue? Please read [ask] and [mcve] ... then edit your question: http://stackoverflow.com/posts/43055384/edit – jogo Mar 27 '17 at 20:18
  • http://stackoverflow.com/questions/10904124/global-and-local-variables-in-r/10904810#10904810 – jogo Mar 27 '17 at 20:22

0 Answers0