[R] Fix for nls bug??? [already fixed in R-patched]

Keith Jewell k.jewell at campden.co.uk
Fri Aug 8 15:35:01 CEST 2008


Dear Prof. Ripley,

Thanks for that.

Just to wrap up the thread, I confirm that my problem is fully fixed in 
R-patched.

Best regards,

Keith Jewell
-----------
"Prof Brian Ripley" <ripley at stats.ox.ac.uk> wrote in message 
news:alpine.LFD.1.10.0808081231170.6025 at gannet.stats.ox.ac.uk...
> On Fri, 8 Aug 2008, Keith Jewell wrote:
>
>> Dear Prof. Ripley,
>>
>> Thank you for your helpful reply. I will download and try R-patched ASAP.
>>
>> I take your point, I should have tried the latest version (R-patched) 
>> before
>> posting.
>>
>> With respect to R-patched, would you recommend its use routinely, or only 
>> in
>> investigation of "unexpected behaviour"?
>
> I use it routinely.
>
> In this case I was not expecting this to be fixed, but just tried your 
> example in my usual R-patched (when it worked) and then in 2.7.1 (to 
> confirm I could reproduce it) -- I then had to figure out what fix also 
> fixed this one -- as it was
>
>     o   nls() was only finding its 'weights' argument in the case when
>         all the variables in the formula were of the same length and
>         hence that model.frame() could be used.
>
> it would have been obvious to no one.  Hence the advice to try R-patched 
> (rather than just read about the changes) can pay off.
>
>>
>> Thanks again,
>>
>> Keith Jewell
>> -------------
>> "Prof Brian Ripley" <ripley at stats.ox.ac.uk> wrote in message
>> news:alpine.LFD.1.10.0808070651030.14363 at gannet.stats.ox.ac.uk...
>>> Your example works in R-patched, as a consequence of investigations of a
>>> different problem.  (See the comments in the posting guide about 
>>> updating
>>> your R and trying the very latest versions.)
>>>
>>> Windows binaries for R-patched are available on CRAN.
>>>
>>> On Tue, 5 Aug 2008, Keith Jewell wrote:
>>>
>>>> Hi All,
>>>>
>>>> I've hit a problem using nls. I think it may be a restriction in the
>>>> applicability of nls and I may have found a fix, but I've been wrong
>>>> before.
>>>>
>>>> This example is simplified to the essentials. My real application is 
>>>> much
>>>> more complicated.
>>>>
>>>> Take a function of matrix 'x' with additional arguments:
>>>> matrix 'aMat' whose values are _not_ to be determined by nls
>>>> vector 'Coeffs' whose vales _are_ to be determined.
>>>> For simplicity, this isn't a selfStart function with an 'initial'
>>>> attribute,
>>>> but that doesn't change things.
>>>>
>>>> Myfunc<-function(x, aMat, Coeffs)
>>>> {
>>>> #
>>>> # result = quadratic response in x with
>>>> # terms selected by aMat
>>>> #
>>>> aMat[aMat!=0] <- Coeffs
>>>> rowSums((x%*%aMat)%*%t(x))
>>>> }
>>>>
>>>> If aMat is passed in by name (e.g. aMat = bMat) nls fails.
>>>> e.g.
>>>> #
>>>> # data frame with some noise
>>>> DF <- data.frame(x1 = runif(20, 1, 20), x2=runif(20, 1, 20))
>>>> DF$y <- 1 +DF$x1 +DF$x2 +DF$x1*DF$x2 +DF$x1^2 + DF$x2^2 + rnorm(20)
>>>> #
>>>> # matrix to pass in as aMat
>>>> bMat <- matrix(c(1,1,0,0), 2, 2)
>>>> #
>>>> # and nls fails
>>>> nls(y ~ Myfunc(cbind(x1, x2), bMat, aVec), DF, start=list(aVec=c(1,2)))
>>>> #
>>>> # pass in the same matrix other than by name and it works
>>>> nls(y ~ Myfunc(cbind(x1, x2), matrix(c(1,1,0,0), 2, 2), aVec), DF,
>>>> start=list(aVec=c(1,2)))
>>>>
>>>> I think the problem lies in this line in nls
>>>>
>>>>  for (var in varNames[!varIndex]) mf[[var]] <- eval(as.name(var), data,
>>>> env)
>>>>
>>>> This adds values for some named arguments (bMat) as columns of the data
>>>> frame. The problem is that generally they don't have the same number of
>>>> rows. I've made it work for my example by replacing that line with this
>>>> line, which adds values for those arguments to the data frame as
>>>> parameters
>>>> rather than as a column
>>>>
>>>>  attributes(mf)[["parameters"]] <-
>>>> c(attributes(mf)[["parameters"]],lapply(varNames[!varIndex],
>>>> function(var)
>>>> eval(as.name(var), data, env)) )
>>>>
>>>> Problem is, I really don't know nls internals enough to be sure I 
>>>> haven't
>>>> broken something.
>>>> And anyway, if this is really an improvement I ought to share it, but
>>>> don't
>>>> know how.
>>>>
>>>> Or I could have totally the wrong end of the stick...
>>>>
>>>> Comments, corrections and advice are welcome.
>>>>
>>>> Thanks in advance,
>>>>
>>>> Keith Jewell
>>>> -----------------------
>>>>> sessionInfo()
>>>> R version 2.7.0 (2008-04-22)
>>>> i386-pc-mingw32
>>>>
>>>> locale:
>>>> LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
>>>> Kingdom.1252;LC_MONETARY=English_United
>>>> Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252
>>>>
>>>> attached base packages:
>>>> [1] stats     graphics  grDevices datasets  tcltk     utils     methods
>>>> base
>>>>
>>>> other attached packages:
>>>> [1] xlsReadWrite_1.3.2 svSocket_0.9-5     svIO_0.9-5 
>>>> R2HTML_1.58
>>>> [5] svMisc_0.9-5       svIDE_0.9-5
>>>>
>>>> loaded via a namespace (and not attached):
>>>> [1] tools_2.7.0 VGAM_0.7-7
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>> --
>>> 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
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> -- 
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list