Difference between revisions of "Software tutorial/Least squares modelling (linear regression)"

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
m
m
Line 6: Line 6:
However, here is a tutorial on how you can use MATLAB or Python to fit a least squares model.
However, here is a tutorial on how you can use MATLAB or Python to fit a least squares model.


<tt>linreg.m</tt> and <tt>regstats.m</tt>
* MATLAB: use the <tt>regress.m</tt> and <tt>regstats.m</tt> function
* Python: use the <tt>numpy.linalg.lstsq</tt>
 
http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html
 
 
x = np.array([0, 1, 2, 3])
    >>> y = np.array([-1, 0.2, 0.9, 2.1])
   
    By examining the coefficients, we see that the line should have a
    gradient of roughly 1 and cut the y-axis at, more or less, -1.
   
    We can rewrite the line equation as ``y = Ap``, where ``A = [[x 1]]``
    and ``p = [[m], [c]]``.  Now use `lstsq` to solve for `p`:
   
    >>> A = np.vstack([x, np.ones(len(x))]).T
    >>> A
    array([[ 0.,  1.],
          [ 1.,  1.],
          [ 2.,  1.],
          [ 3.,  1.]])
   
    >>> m, c = np.linalg.lstsq(A, y)[0]
    >>> print m, c
    1.0 -0.95
   
    Plot the data along with the fitted line:
   
    >>> import matplotlib.pyplot as plt
    >>> plt.plot(x, y, 'o', label='Original data', markersize=10)
    >>> plt.plot(x, m*x + c, 'r', label='Fitted line')
    >>> plt.legend()
    >>> plt.show()

Revision as of 19:21, 1 December 2010

One of the best packages for fitting least squares models, in addition to all sorts of other statistical manipulation of data is the R language. The following pages from the 4C3 (Statistics for Engineering) website will help you:

However, here is a tutorial on how you can use MATLAB or Python to fit a least squares model.

  • MATLAB: use the regress.m and regstats.m function
  • Python: use the numpy.linalg.lstsq

http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html


x = np.array([0, 1, 2, 3])

   >>> y = np.array([-1, 0.2, 0.9, 2.1])
   
   By examining the coefficients, we see that the line should have a
   gradient of roughly 1 and cut the y-axis at, more or less, -1.
   
   We can rewrite the line equation as ``y = Ap``, where ``A = x 1``
   and ``p = [[m], [c]]``.  Now use `lstsq` to solve for `p`:
   
   >>> A = np.vstack([x, np.ones(len(x))]).T
   >>> A
   array([[ 0.,  1.],
          [ 1.,  1.],
          [ 2.,  1.],
          [ 3.,  1.]])
   
   >>> m, c = np.linalg.lstsq(A, y)[0]
   >>> print m, c
   1.0 -0.95
   
   Plot the data along with the fitted line:
   
   >>> import matplotlib.pyplot as plt
   >>> plt.plot(x, y, 'o', label='Original data', markersize=10)
   >>> plt.plot(x, m*x + c, 'r', label='Fitted line')
   >>> plt.legend()
   >>> plt.show()