Polynomial regression: Difference between revisions

(added Ursala)
Line 429:
</lang>
Thus we confirm once more that for already sorted sequences the considered quick sort implementation has quadratic dependence on sequence length (see [[Query Performance|'''Example''' section for Python language on ''Query Performance'' page]]).
 
=={{header|R}}==
R has several tools for fitting. Here we use a generalized nonlinear least squares method with the polynomial as model:
 
<lang R>library(nlme)
x <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321)
fitted <- gnls(y ~ c0*x^2 + c1*x + c2, start=list(c0=1, c1=1, c2=0))
print(paste(fitted$coeff[[1]], "*x^2 + ",
fitted$coeff[[2]], "*x + ",
fitted$coeff[[3]]))
 
# get several info about the fitting process
print(summary(fitted))</lang>
 
The "base" <tt>nls</tt> could be used with algorithm "port", since the default Gauss-Newton has problems recognizing the convergence:
 
<lang R>nls(y ~ c0*x^2 + c1*x + c2, start=list(c0=1, c1=1, c2=0), trace=TRUE)</lang>
 
gives
 
5.364254e-29 : 3 2 1
Error in nls(y ~ c0 * x^2 + c1 * x + c2, start = list(c0 = 1, c1 = 1, :
number of iterations exceeded maximum of 50
 
And even increasing the maximum possible iterations, it does not finish properly (even if the result, as we can see with the trace option, is reached!). Instead, the
 
<lang R>nls(y ~ c0*x^2 + c1*x + c2, start=list(c0=1, c1=1, c2=0), algorithm="port")</lang>
 
works fine.
 
=={{header|Tcl}}==