Skip to content
Prev 172184 / 398506 Next

nls does not accept start values

Thank you

It was simplified version of my problem. I want to elaborate a function 
which can take predefined list of formulas, some data and evaluate which 
formulas can fit the data. I was inspired by some article in Chemical 
engineering in which some guy used excel solver for such task. I was 
curious if I can do it in R too. I am not sure if nls is appropriate tool 
for such task but I had to start somewhere.

Here is a function which takes list of formulas and data and gives a 
result for each formula.

modely <- function(formula, data, ...){
ll <- length(formula)   #no of items in formula list
result2 <- vector("list", ll) #prepare results
result1 <- rep(NA, ll)
for(i in 1:ll) {
fit<-try(nls(formula[[i]], data))
if( class(fit)=="try-error") result1[i] <- NA  else result1[i] <- 
sum(resid(fit)^2)
if( class(fit)=="try-error") result2[[i]] <- NA  else result2[[i]] <- 
coef(fit)
}

ooo<-order(result1) #order results according to residual sum

#combine results into one list together with functions used

result <- mapply(c, "sq.resid" = result1, result2) 
names(result) <- as.character(formula)
# output
result[ooo]
}

# data
x <-1:10
y <-1/(.5-x)+rnorm(10)/100

# list of formulas
fol <- structure(list(a = y ~ 1/(a - x), b = y ~ a * x^2 + b * log(x), 
    c = y ~ x^a), .Names = c("a", "b", "c"))

modely(fol, data.frame(x=x, y=y)

does not use "correct" model because when using default start values it 
results in
Error in numericDeriv(form[[3]], names(ind), env) : 
  Missing value or an infinity produced when evaluating the model

however

 nls(fol[[1]], data.frame(x=x, y=y), start=list(a=mean(y)))

gives correct result. Therefore I started think about how to add a 
"better" starting value for some fits as a second part of my formula list 
to define structure like>

list(a= formula1, start.formula1, b=formula2, start.formula2, ....)

I wonder If you can push me to better direction.

Thanks again
Best regards
Petr




Uwe Ligges <ligges at statistik.tu-dortmund.de> napsal dne 02.03.2009 
09:41:45:
"list(a=mean(y))")),
start=list(a=mean(y)))
[[1]]
eval,
it
http://www.R-project.org/posting-guide.html