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)