Question

I wrote a function that uses the loop below, intending to find starting values for a nonlinear regression. I wonder if there is a procedure to scan the whole list that results from the loop, and find the parameter set that produces the smallest RSS.

for (c1 in seq(0.95*min(trans), 0.95*max(trans), mean(trans)/10)){
for (gamma in seq(1,100,1))  {

  lin_eq     <- paste0("lag_0 ~",  paste(paste(colnames(linear_dat)[-1]), sep="",collapse='+'))      
  nonlin_eq  <- paste0("transition + ",paste("I(transition*",paste(colnames(nonlinear_dat),")"), sep="",collapse='+'))

  transition <- (1+exp(-(gamma/scale)*(trans-c1)))^-1

  grid.regre <-lm(paste0(lin_eq,"+",nonlin_eq),data=data.frame(linear_dat,nonlinear_dat,trans)) 

  coef <- grid.regre$coefficients
  RSS  <- sum(grid.regre$residuals^2)
  grid[[length(grid)+1]] <- c(RSS,gamma,c1,coef)  
} 

}

Thanks a million.

Was it helpful?

Solution

num <- which.min( sapply(grid, "[[", 1) )  # the item number with min RSS
grid[[num]] # the whole list entry
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top