Re: [R] newton-raphson

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Thu 01 Dec 2005 - 04:35:08 EST

>>>>> "dlavecchia" == dlavecchia <dlavecchia@tiscali.it>
>>>>> on Wed, 30 Nov 2005 17:08:47 +0100 writes:

    dlavecchia> I have to solve a score function by using
    dlavecchia> Newton-Raphson algorithm. Is there such a
    dlavecchia> fucntion in R? I have built this algoritm

    dlavecchia> newton<-function(tgt,drva,th0,err) {
    dlavecchia> iter=0
    dlavecchia> repeat  {iter = iter+1
    dlavecchia> th1=th0-tgt(th0)/drva(th0)
    dlavecchia> if (abs(th0-th1)<err||abs(tgt(th1))<.1e-10)
    dlavecchia> break
    dlavecchia> th0=th1}

    dlavecchia> th1
    dlavecchia> }
    dlavecchia> but it does not work for my function because the
    dlavecchia> ratio "tgt(th0)/drva(th0)" is very high and the
    dlavecchia> algoritm does not converge. On the contrary it
    dlavecchia> works very well for some simple functions (like
    dlavecchia> X^2, X^3..and so on) 

    dlavecchia> Please, can you help me?

yes, instead of Newton-Raphson which is fine for teaching the basics, do use a much smarter and more "robust" algorithm like the one built into

    uniroot(drva, ......) ## read ?uniroot and look at the 'Examples'

Martin



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Dec 01 05:33:42 2005

This archive was generated by hypermail 2.1.8 : Thu 01 Dec 2005 - 09:32:45 EST