ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 27 Sep 2011 05:31:22 +0200fitting a curve to a straight linehttps://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/How can I use sage to fit some points like
x = [1, 3, 4...]
y = [1.2, 4.5, 3.6, ...]
to a straight line using method of least squares And make a plot of the points and the line?Sat, 24 Sep 2011 04:31:50 +0200https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/Answer by kcrisman for <p>How can I use sage to fit some points like </p>
<p>x = [1, 3, 4...]
y = [1.2, 4.5, 3.6, ...]</p>
<p>to a straight line using method of least squares And make a plot of the points and the line?</p>
https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?answer=12689#post-id-12689I have a feeling that the answer to [this ask.sagemath question](http://ask.sagemath.org/question/438/how-to-linear-regression-in-sage#784) may also be helpful. It mentions the built-in `find_fit` method.Sun, 25 Sep 2011 23:11:50 +0200https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?answer=12689#post-id-12689Answer by Dirk Danckaert for <p>How can I use sage to fit some points like </p>
<p>x = [1, 3, 4...]
y = [1.2, 4.5, 3.6, ...]</p>
<p>to a straight line using method of least squares And make a plot of the points and the line?</p>
https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?answer=12686#post-id-12686I didn't find any reference to linear regression in the SAGE reference manual. That means you have to use Maxima directly. Fortunately that's not so difficult.
It's easier if you present the data as a list of pairs (x,y). Try this in a SAGE notebook cell:
maxima.load("stats")
x=maxima.simple_linear_regression([[0,1],[1,2],[2,4]],conf_level=0.95)
x
You are presented a whole bunch of statistics about the regression. From there on I guess you can manage on your own. Good luck.Sun, 25 Sep 2011 06:32:15 +0200https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?answer=12686#post-id-12686Answer by Joaquim Puig for <p>How can I use sage to fit some points like </p>
<p>x = [1, 3, 4...]
y = [1.2, 4.5, 3.6, ...]</p>
<p>to a straight line using method of least squares And make a plot of the points and the line?</p>
https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?answer=12693#post-id-12693In my opinion the easiest way to do the fit is to use the find_fit function, which is standard in SAGE. For example:
sage: x = [1, 3, 4]
sage: y = [1.2, 4.5, 3.6]
sage: var('a,b,t')
sage: model(t)=a*t+b
sage: data=zip(x,y)
sage: fit=find_fit(data,model,solution_dict=True) # Dictionary is optional but handy
sage: model.subs(fit) # Show the model
t |--> 0.92142857142840007*t + 0.64285714285636364
sage: plot(model.subs(fit),(t,0,5))+points(data,size=20,color='red') # Nice plot
When you have a huge number of points and you want just a polynomial fit, I found that it is (numerically) better to use the polyfit function from numpy:
sage: import numpy as np
sage: a,b=np.polyfit(x,y,1)
sage: a,b
(0.92142857142857137, 0.64285714285714401)
Tue, 27 Sep 2011 05:31:22 +0200https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?answer=12693#post-id-12693Answer by Jason Grout for <p>How can I use sage to fit some points like </p>
<p>x = [1, 3, 4...]
y = [1.2, 4.5, 3.6, ...]</p>
<p>to a straight line using method of least squares And make a plot of the points and the line?</p>
https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?answer=12685#post-id-12685Searching in google for "sage least squares" gives three examples as the three top hits. Here is another example: http://sage.cs.drake.edu/home/pub/52/
You can also use scipy to do it: http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html#least-square-fitting-leastsq
or numpy: http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html#numpy.linalg.lstsq
Sat, 24 Sep 2011 12:42:48 +0200https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?answer=12685#post-id-12685Comment by kcrisman for <p>Searching in google for "sage least squares" gives three examples as the three top hits. Here is another example: <a href="http://sage.cs.drake.edu/home/pub/52/">http://sage.cs.drake.edu/home/pub/52/</a></p>
<p>You can also use scipy to do it: <a href="http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html#least-square-fitting-leastsq">http://docs.scipy.org/doc/scipy/refer...</a></p>
<p>or numpy: <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html#numpy.linalg.lstsq">http://docs.scipy.org/doc/numpy/refer...</a></p>
https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?comment=21200#post-id-21200Or R, naturally.Sun, 25 Sep 2011 23:09:35 +0200https://ask.sagemath.org/question/7566/fitting-a-curve-to-a-straight-line/?comment=21200#post-id-21200