Learn R Programming

comphy (version 1.0.5)

polysolveLS: Polynomial Least Squares

Description

Find the parameters, \(a_1,\dots,a_{m+1}\), of the polynomial model of degree \(m\) (1D function), using the least squares technique on a group of \(n\) data points.

Usage

polysolveLS(pts, m, tol = NULL)

Value

A named list with two elements:

a

A vector of length \(m\) containing the \(m\) numeric values of the estimated polynomial's coefficients.If more than one solution is possible, (infinite-solutions case) the function returns a NULL and prints out a related message.

SSE

A real number. The numerical value of the sum of squared residuals.

Arguments

pts

A \(n \times 2\) matrix or data frame where each row contains the coordinates of a data point used for regression.

m

An integer. The degree of the polynomial to be used as model for the regression.

tol

A real number. The solution of a linear system can be compromised when the condition number of the matrix of coefficients is particularly high (ill-conditioned matrices). tol is the reciprocal of the condition number. For values of tol smaller than 1e-17, ill-conditioning is deemed to be sever enough not to guarantee an accurate solution. For such values the function stops execution, returning an error message. In fact, the solution can still be accurate, notwithstanding ill-conditioning, and the user can force the calculation of a solution using a value of tol smaller than 1e-17. Default is NULL, corresponding to a tol=1e-17.

Details

The polynomial model has the following analytic form: $$ y = a_1x^m + a_2x^{m-1} + \dots + a_mx + a_{m+1} $$ The \(n\) data points are contained in a matrix or data frame with \(2\) columns, containing the coordinates of each data point, and \(n\) rows. The least squares procedure is carried out as solution of a matrix equation, via the solveLS function.

Examples

Run this code
# 21 points close to the quadratic x^2 - 5*x + 6
x <- seq(-2,5,length=21)
set.seed(7766)
eps <- rnorm(21,mean=0,sd=0.5)
y <- x^2-5*x+6+eps

# Data frame
pts <- data.frame(x=x,y=y)

# Regression
ltmp <- polysolveLS(pts,m=2)
print(names(ltmp))
print(ltmp$a)
print(ltmp$SSE)

Run the code above in your browser using DataLab