[R] newton-raphson

Martin Maechler maechler at stat.math.ethz.ch
Wed Nov 30 18:35:08 CET 2005


>>>>> "dlavecchia" == dlavecchia  <dlavecchia at 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




More information about the R-help mailing list