How can I do regression analysis in Sage?

I tried this unsuccessfully:

find_fit(data, quadratic_residues) 

I am trying to find the best fit for the water flow data: http://dl.getdropbox.com/u/175564/rate.png

--- edit after comment ---

New code:

 var('x') model(x) = x**2 find_fit((xlist, reqlist), model) 

Error message:

 Traceback (click to the left for traceback) ... TypeError: data has to be a list of lists, a matrix, or a numpy array 

--- change

Now the error message appears:

 Traceback (click to the left for traceback) ... ValueError: each row of data needs 2 entries, only 5 entries given 

Same thing here as the image: http://dl.getdropbox.com/u/175564/sage.png

+4
source share
3 answers
 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) points(mydata,color='purple') + plot( mymodel( a=myfit[a], b=myfit[b], c=myfit[c] ), (x,0,4,), color='red' ) 
+7
source

I think your problem is that quadratic_residues probably don't mean what you think it means. If you are trying to establish a better quadratic model, I think you want to do something like.

 var('a, b, c, x') model(x) = a*x*x + b*x + c find_fit(data, model) 
+3
source

Having tried Stephen with my example, I also ran into an error:

 ValueError: each row of data needs 5 entries, only 2 entries given 

Here is a more explicit example that I tested to work in sage 4.7.

 sage: l=[4*i^2+7*i+134+random() for i in xrange(100)] sage: var('a,b,c,x') (a, b, c, x) sage: model=a*x^2+b*x+c sage: find_fit(zip(xrange(100),l),model,variables=[x]) [a == 4.0000723084513217, b == 6.9904742307159697, c == 134.74698715254667] 

Apparently you will need the variables = [x] to pass sage which of a, b, c and x corresponds to the variable in your model.

+2
source

All Articles