# [R] finding roots (Max Like Est)

Berend Hasselman bhh at xs4all.nl
Sat Dec 1 17:26:49 CET 2007

```Why exactly the same question?

You were told what to do.
I am new to R. I did what the previous poster said.
I found it. Write your function in terms of vector operations. Avoid
loops if you can.

Sample input for R follows
-------------------------

# par is the thing that has to be found
# x are the observations

f  <- function(par,x) sum(2*(x-par)/(1+(x-par)^2))

# trial stuff

x  <- 1:10

# use uniroot to find a value for par such that f(par,x) == 0
# interval for par is obvious (lower=1 and upper=10)

paropt  <- uniroot(f,c(1,10),tol=1e-8,x)

paropt
<END OF SAMPLEINPUT>

It works.

Berend Hasselman

On 30 Nov 2007, at 17:59, stathelp wrote:

>
> I have this maximum liklihood estimate problem
>
> i need to find the roots of the following:
>
> [sum (from i=1 to n) ] ((2(x[i]-parameter)/(1+(x[i]-parameter)^2))=0
>
> given to me is the x vector which has length 100
>
> how would I find the roots using R?
>
> I have 2 thoughts...... 1 is using a grid search ... eg. brute
> force, just
> choosing a whole bunch of different values for my parameter ....
> such as
> parameter=seq(0,100,.1) .... and this is what I have so far,
>
>
> 	john=rep(0,length(x))
> 	for(i in 1:length(x)) {
> 	john[i]=((x[i]-0)/(1+(x[i]-0)^2))
>             }
>             sum(john)
>
> then
>
> 	john=rep(0,length(x))
> 	for(i in 1:length(x)) {
> 	john[i]=((x[i]-.1)/(1+(x[i]-.1)^2))
>             }
>             sum(john)
>
> then
>
> 	john=rep(0,length(x))
> 	for(i in 1:length(x)) {
> 	john[i]=((x[i]-.2)/(1+(x[i]-.2)^2))
>             }
>             sum(john)
>
> something like this ...
>
>             theta=seq(0,100,.1)
> 	john=rep(0,length(x))
> 	for(i in 1:length(x)) {
> 	john[i]=((x[i]-theta[j])/(1+(x[i]-theta[j])^2))
>             }
>             sum(john)
>
> but something is wrong with my code because its not working. Anyone
> have any
> ideas? (I am very new to R and statistical software in general)
>
> The 2nd thought was to use the Newton Raphson Method, but, I dont
> even know
>
> Any thoughts help.
>
> Thanks
>
>
> --
> View this message in context: http://www.nabble.com/finding-roots-%28Max-Like-Est%29-tf4901659.html#a14040895
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help