[R-es] problema de selección de valores iniciales en nlm

Juan Santos juan.santos en vi.ieo.es
Mie Ago 24 12:20:26 CEST 2011


Hola a todos,

Necesito estimar dos parametros utilizando la función nlm;

fit<-nlm(hood2par,c(x01[i],x02[j]),iterlim=300, catch=x[,c(3,4,5)],sp=.5)

donde hood2par es una logística modificada.

Pero en mi caso, la convergencia de nlm depende de los  valores 
iniciales de dichos parámetros. Para buscar dichos valores iniciales de 
manera automática, genero dos vectores de valores iniciales posibles,

x01=seq(-10,-20,-0.1)
x02=seq(0.1,0.9,0.01)

y construyo un doble bucle para buscar los valores que llevan a la 
convergencia de la función:

for (i in 1:length(x01)) { for (j in 1:length(x02)) {

fit<-NULL                      #genero un objeto nulo llamado fit

try(fit<-nlm(hood2par,c(x01[i],x02[j]),iterlim=300, 
catch=x[,c(3,4,5)],sp=.5),silent=TRUE) # Con try, evito la rotura de los 
#bucles cuando nlm no converge

stopifnot(is.null(fit)) # si finalmente converge ("fit" ya no es nulo) 
rompo los bucles con stopifnot
}
}

Cuando fit no es NULL, stopifnot rompe el bucle. El problema que tengo 
es que cuando trato de embeber la rutina anterior en una función:

FFF<-function(x01,x02,catch){

for (i in 1:length(x01)) { for (j in 1:length(x02)) {

fit<-NULL

try(fit<-nlm(hood2par,c(x01[i],x02[j]),iterlim=300, 
catch=x[,c(3,4,5)],sp=.5),silent=TRUE) # does not stop in the case of err

stopifnot(is.null(fit))}}

return(fit)}


no consigo que esta me retorne el valor de fit estimado por nlm

 > FFF(x01,x02,catch)
#Error: is.null(fit) is not TRUE

¿Alguien puede ayudarme? muchas gracias.

Juan Santos



Más información sobre la lista de distribución R-help-es