Maximum Likelihood estimation Given a sample $ X_1,X_2 \dots X_{100}$ and the density function   $ f(x;\theta) = \frac{1}{\pi \cdot \left(1+\left(x-\theta \right)^2\right)}$ , find an approximate solution for  $\hat{\theta}_{MLE.}$
My attempt:
I have found the joint likelihood $L(\theta;x_1,x_2\dots x_{100}) = \prod _{i=1}^{100}\left(\frac{1}{\pi \cdot \left(1+\left(x_i-\theta \right)^2\right)}\right)\:$
$l$ = $\log(L) = -100*\ln(\pi)-\sum^{100}_{i=1}(\ln(1+(x-\theta)^2)$.
I'm not sure of this step
$\frac{\partial }{\partial \theta}\left(\log(L)\right) = \sum_{i=1}^{100}(\frac{2(x_i-\theta)}{1+(x_i-\theta)^2}$
then I used Newton's method to find the maxima.
this is the script I used to calculate the maxima
#deravitive of log(L).
fun1 &lt;- function(theta){
  y1 &lt;- 0
  for(i in 1:length(x)){
    y1 &lt;- y1 + (2*(theta-x[i]))/(1+(x[i]-theta)^2)
  }
  return(y1)
}


#derivative of fun1.
fun1.tag &lt;- function(theta){
  y &lt;- 0
  for(i in 1:length(x)){
    y &lt;- 2*(theta^2+(x[i]^2)-20*x[i]-1)/((1+(x[i]-theta)^2)^2)
  }
  return(y)
}


# The Newton's method.


guess &lt;- function(theta_guess){
  theta2 &lt;- theta_guess - fun1(theta_guess)/fun1.tag(theta_guess)
  return(theta2)
}
theta1 &lt;- median(data$x)
epsilon &lt;- 1

theta_before &lt;- 0

while(epsilon &gt;0.0001){
  theta1 &lt;- guess(theta1)
  epsilon &lt;- (theta_before- theta1)^2
  theta_before &lt;- theta1
}


What I got was $\hat{\theta}_{MLE} = 5.166$
I'm now trying to plot the data(in my case x) and check if $\hat{\theta}_{MLE} = 5.166$ is actually a maxima.
You have a typo in your formula
#derivative of fun1.
fun1.tag &lt;- function(theta){
  y &lt;- 0
  for(i in 1:length(x)){
    y &lt;- y + 2*(theta^2+(x[i]^2)-20*x[i]-1)/((1+(x[i]-theta)^2)^2)
  }
  return(y)
}

There is y +  missing inside the loop.