0
I am trying to generate a general function, which runs several regression models. The problem is that when a model gives error, the main function stops. I would like to use a parole that in case of error, skip to the next step:
Example: when trying to run a model nls()
, presents this message described below, as this function is one of which I am testing within a general function, it locks the function at this point.
an example, when I want to load some package, I usually do so:
if(!require(nome do pacote)){install.packages("nome do pacote")}
in my intention is like this: if(there is no such package){ install the package} so here I inform that "!" would represent the "error".
assuming I create a function:
melhor.ajuste<-function(df,x,y){
n1<-nls(y~x+c/e*x^d,start=....,data=df)
n2<-nls(y~x-b*e^d/log(x),start=....,data=df)
n3<-nls(y~x+a*b/c^x,start=....,data=df)
list(modelo1=n1,modelo2=n2,modelo3=n3)}
if for example the function N2 shows any error, as an example:
stop(simpleError("Error in nlsModel(formula, mf, start, wts) :\n matriz gradiente singular com estimativas de parâmetros iniciais"))
my function does not present me any value, because there was a mistake!
what I wish was something like:
if(!nls(y~x+c/e*x^d,start=....,data=df)){n1=0}else{n1<-nls(y~x+c/e*x^d,start=....,data=df)}
if(!nls(y~x+a*b/c^x,start=....,data=df)){n2=0}else{n1<-nls(y~x+a*b/c^x,start=....,data=df)}
if(!nls(y~x+a*b/c^x,start=....,data=df)){n3=0}else{n1<-nls(y~x+a*b/c^x,start=....,data=df)}
someone knows somehow I do it! excel does, by the SEERRO() function, where I go (if I make a mistake; do this), in my case it would be, se(seerro(nls(y~x+a*b/c^x,start=....,data=df);0)==0;n1=0;n1=nls(y~x+a*b/c^x,start=....,data=df))
, or would be a parole that identifies error, within a conditional.
Making it clear, that at no time, my main objective is to find out what the mistake is and try to fix it, because I will apply the function in more than 50 models, knowing that some do not fit even, because it is a singular matrix, for bursting the minFactor, and something like that. my goal, is to find a way that if the function presents error it concatene 0 to the object, and pass to the next model.
and at the end provide me the list of all models.
Have you tried using the
try
?– lmonferrari