[R] nls weights warning message

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jul 31 14:49:39 CEST 2008


On Thu, 31 Jul 2008, Lisa Readdy wrote:

> The following warning message occurs when running the nls on some data:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Warning message:
> In is.na(wts) : is.na() applied to non-(list or vector) of type 'NULL'
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> and I would like to know what is causing it and how I can fix it.
>
> As an example, the following, from Venables and Ripley, which does not
> produce the warning message and then on the data which does procude the
> warning message.
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> ##Venables and rippley
>
> library(MASS)
> data(wtloss)
> wtloss.st = c(b0=90, b1=95, th=120)
> n1<-length(wtloss)
> Weight=c(1,rep(0.1,n1-1))
>
> wtloss.fm = nls(Weight ~ b0 + b1*2^(-Days/th), data=wtloss, start=wtloss.st,
> trace=T, weights=Weight )

I am not sure what you think that does.  It uses the Weight column of the 
data frame as the weights, which is not what you seem to have intended.

> wtloss.fm
>
> ##My data
> y<-c(73,73,70,74,75,115,105,107,124,107,116,125,102,144,178,149,177,124,157,128,
> 169,165,186,152,181,139,173,151,138,181,152,188,173,196,180,171,188,174,198,172,
> 176,162,188,182,182,141,191,190,159,170,163,197)
>
> x<-c(1,1,1,1,1,2,2,3,3,3,3,3,3,4,4,4,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,8,8,8,
> 8,8,8,8,8,8,8,9,9,9,9,9,11,12)
>
> Data<-data.frame(cbind(y,x))
>
> n2<-length(x)
> weight<-c(1,rep(0.1,n2-1))
>
> Ta<-min(Data$x)
> Tb<-max(Data$x)
> G.st<-c(k=0.005, g1=50,g2=550)
>
>
> #no weights
> growthFit<-nls(y~((g1)*exp((log(g2/g1))*(1-exp(-k*(x-Ta)))
>  /(1-exp(-k*(Tb-Ta))))), data=Data, start=G.st, trace=T)
>
>
> #with weights
> growthFit.wts<-nls(y~((g1)*exp((log(g2/g1))*(1-exp(-k*(x-Ta)))
>  /(1-exp(-k*(Tb-Ta))))), data=Data, start=G.st, trace=T, weights=weight)

The difference here is that you don't have all your variables of the same 
length:

Browse[1]> print(n)
  y  x Ta Tb
52 52  1  1

and the variable-finding mechanism is not set up to find weights in that 
case.  That's a bug, but you can avoid it by substituting the scalars 
(e.g. using substitute()).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list