title: Best way to minimize residual sum of squares in R; is nlm function the right way?
body: <p>I have five points for which I will fit a 2nd order polynomial in R. I will do so by minimizing the  <strong>sum of squared errors of prediction</strong> (SSE). </p>
  
  <p>What's the best way to do so?  </p>
  
  <p>So far I have done this:</p>
  
  <pre><code>(1,5.8), (2,3.9), (3,4.2), (4,5.7), (5,10.2) ## my data
  </code></pre>
  
  <p>To this data I want to fit a 2nd order polonium with the intercept 10 and the coefficient before x^2 is set to 1. I do this:</p>
  
  <pre><code>p&lt;-c(5.8, 3.9, 4.2, 5.7, 10.2)
  
  f&lt;-function(x,a){x^2-ax+}
  ##sum of squared errors of prediction
  SSE&lt;-function(a){ (p[i]-f(i,a) )^2 }
  
  nlm(SSE,0.1) ## 0.1 is picked totally random
  </code></pre>
  
  <p>But it returns a "wrong" answer:</p>
  
  <pre><code>$minimum
  [1] 9.475923e-25
  
  $estimate
  [1] 4.96
  </code></pre>
  
  <p>When I manually calculate SSE for a=4.96 then is SSE=9.475923e-25. What do I do wrong? My biggest concern: does nlm function run through all my data points? </p>