[R] nlrob problem

Pascal A. Niklaus pascal.niklaus at ieu.uzh.ch
Mon Dec 19 20:46:57 CET 2011


Dear all,

I am not sure if this mail is for R-help or should be sent to R-devel 
instead, and therefore post to both.

While using nlrob from package 'robustbase', I ran into the following 
problem:

For psi-functions that can become zero (e.g. psi.bisquare), weights in 
the internal call to nls can become zero. Example:

   d <- data.frame(x=1:5,y=c(2,3,5,10,9))
   d.nlrob <- nlrob(y ~ k*x,start=list(k=1),data=d,psi=psi.bisquare)

I think the problem is as follows: After the call to nls, a weighted 
residual vector is calculated by dividing by sqrt(w). This generates 
NaN's for weights of zero, which leads to problems in the subsequent 
call to nls during the next iteration:

     for (iiter in 1:maxit) {
       ...
             w <- psi(resid/Scale, ...)
       ...
             data$w <- sqrt(w)
       ...
             out <- nls( ....., data=data ....... )
       ...
             resid <- -residuals(out)/sqrt(w)   # NaN for w=0
       ...
     }

I wonder whether this problem shouldn't be dealt with by setting 'w' to 
0 for the NaN cases.

I can get a fit by calling nlrob with na.action=na.exclude, but I'd have 
intuitively assumed that na.action applies to the NAs in the data set 
passed to nlrob, not to the one internally generated by nlrob and passed 
to nls.

The second 'issue' is that the weights are passed as 'w', whereas the 
documentation of 'nls' says weights should be given as 'weights' :

    data: an optional data frame in which to evaluate the variables in
           ‘formula’ and ‘weights’.  Can also be a list or an
           environment, but not a matrix.

I think it would be good to mention in the documentation of 'nls' that 
weights can be passed as 'w' as well.

Pascal Niklaus



More information about the R-help mailing list