Juan Santos
2011-Aug-24  10:20 UTC
[R-es] problema de selección de valores iniciales en nlm
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
Carlos Ortega
2011-Aug-29  20:36 UTC
[R-es] problema de selección de valores iniciales en nlm
Hola,
Como alternativa a solucionar tu problema de utilizar una función para
definir diferentes parámetros de entrada, podrías utilizar la función
equivalente (también para minimizar) "nlminb" que permite justamente
esto,
el poder definir un intervalo sobre el que minimizar tu función.
nlminb(start, objective, gradient = NULL, hessian = NULL, ...,
       scale = 1, control = list(), lower = -Inf, upper = Inf)
lower, uppervectors of lower and upper bounds, replicated to be as long as
start. If unspecified, all parameters are assumed to be unconstrained.
Saludos,
Carlos Ortega
www.qualityexcellence.es
2011/8/24 Juan Santos <juan.santos@vi.ieo.es>
> 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
>
> ______________________________**_________________
> R-help-es mailing list
> R-help-es@r-project.org
>
https://stat.ethz.ch/mailman/**listinfo/r-help-es<https://stat.ethz.ch/mailman/listinfo/r-help-es>
>
	[[alternative HTML version deleted]]