Ask Your Question
6

How-to: (Linear ...) regression in Sage

asked 2011-03-15 14:42:53 +0100

tmaxara gravatar image

Suppose I have the following set of points r = [(1,2),(3.45,4),(6,5),(4,3)]. How do I implement in Sage a (linear) regression with "bord tools"?

edit retag flag offensive close merge delete

Comments

Can you clarify 'bord tools'? I'm not familiar with this phrase.

kcrisman gravatar imagekcrisman ( 2011-03-15 15:45:49 +0100 )edit

I don't know what "bord tools" means, but you can do all kinds of regression analysis, including linear regression, using the Sage interface to R. There are experts lurking here that I'm sure can give you an example.

benjaminfjones gravatar imagebenjaminfjones ( 2011-03-15 15:53:46 +0100 )edit

I bet it is a translation from French: "avec les outils du bord". It means : using only what already exists in Sage.

Laurent Claessens gravatar imageLaurent Claessens ( 2011-03-16 13:51:16 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
7

answered 2011-03-16 16:18:35 +0100

kcrisman gravatar image

It turns out that this has even showed up on Stack Overflow.

Luckily, that page refers to the function I had completely forgotten about - find_fit.

sage: find_fit?

String Form:    <function find_fit at 0x10bee5cf8>
Namespace:      Interactive
File:           /Applications/MathApps/sage/local/lib/python2.6/site-packages/sage/numerical/optimize.py
Definition:     find_fit(data, model, initial_guess=None, parameters=None, variables=None, solution_dict=False)
Docstring:
       Finds numerical estimates for the parameters of the function model
       to give a best fit to data.

So this might work, and looks decent.

sage: R = [[1,2],[3.45,4],[6,5],[4,3]]
sage: var('a,b')
(a, b)
sage: model(x) = a*x+b
sage: find_fit(R,model)
[a == 0.56881365890949054, b == 1.445160655902004]
sage: points(R)+plot(model(a=find_fit(R,model)[0].rhs(),b=find_fit(R,model)[1].rhs()),(x,0,10),color='red')

If you are serious about your needs, though, you should probably use some of the tools in Scipy or R (numerous YouTube videos on this, though for reason I can't watch them right now.

edit flag offensive delete link more

Comments

nice find @kcrisman!

niles gravatar imageniles ( 2011-03-16 16:41:45 +0100 )edit
1
DSM gravatar imageDSM ( 2011-03-16 22:39:45 +0100 )edit

Thank you for your answer

tmaxara gravatar imagetmaxara ( 2011-03-26 17:45:09 +0100 )edit
3

answered 2012-08-04 15:07:56 +0100

Andre Mikulec gravatar image
    mydata = [[1,3],[2,7],[3,13],[4,24]]
    var('a,b,c')
    mymodel(x) = a*x^2 + b*x + c 
    myfit = find_fit(mydata,mymodel,solution_dict=True)
    myfit
    points(mydata,color='purple') + plot(
      mymodel(
        a=myfit[a],
        b=myfit[b],
        c=myfit[c]
        ), 
        (x,0,4,),
        color='red'
      )
edit flag offensive delete link more
0

answered 2017-10-06 16:44:29 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Any way to find the Correlation Coefficient r and the Coefficient of Determination r^2 using find_fit()? TIA, AJG calcpage@gmail.com http://shadowfaxrant.blogspot.com http://www.youtube.com/calcpage2009 (www.youtube.com/calcpage2009)

edit flag offensive delete link more

Comments

Welcome to Ask Sage. Please post this as a new question rather than as an answer here (then delete the answer here). Avoid checking the "community wiki" checkbox on Ask Sage.

slelievre gravatar imageslelievre ( 2017-10-06 23:31:49 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-03-15 14:42:53 +0100

Seen: 8,253 times

Last updated: Aug 04 '12