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.Wed, 26 Jun 2013 18:08:33 +0200Substitute x value in a linear regression equation instead of writing it out?https://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/Thanks to this handy post, I was able to generate a linear regression graph:
http://stackoverflow.com/questions/454606/how-can-i-do-regression-analysis-in-sage
My input thus far in sage 5.9:
r = [[0,76.4], [1,78.5], [2,81.5], [3,87.8],
[4,88.4], [5,92.4], [6,94.0], [7,95.0]]
var('a,b')
(a, b) #It seems this statement is extraneous
model(x) = a*x+b
find_fit(r, model)
plot(model(a = find_fit(r, model)[0].rhs(),
b = find_fit(r,model)[1].rhs()),
0,15, ymin = 0, ymax = 150)
What would be the most practical way of substituting the x value to generate an answer instead of:
Executing: find_fit(r, model)
Then copying and pasting the answer: [a == 2.8690476152412128, b == 76.70833334665576]
And then having to manually write out, where number 15 represents what I wanted to put substitute for x: 2.8690476152412128 * (15) + 76.70833334665576
Also 1 of my other threads has an unsolved question, incase anyone's interested:
http://ask.sagemath.org/question/2732/display-y-interceptWed, 26 Jun 2013 13:35:12 +0200https://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/Comment by kcrisman for <p>Thanks to this handy post, I was able to generate a linear regression graph:
<a href="http://stackoverflow.com/questions/454606/how-can-i-do-regression-analysis-in-sage">http://stackoverflow.com/questions/45...</a></p>
<p>My input thus far in sage 5.9:</p>
<pre><code>r = [[0,76.4], [1,78.5], [2,81.5], [3,87.8],
[4,88.4], [5,92.4], [6,94.0], [7,95.0]]
var('a,b')
(a, b) #It seems this statement is extraneous
model(x) = a*x+b
find_fit(r, model)
plot(model(a = find_fit(r, model)[0].rhs(),
b = find_fit(r,model)[1].rhs()),
0,15, ymin = 0, ymax = 150)
</code></pre>
<p>What would be the most practical way of substituting the x value to generate an answer instead of:</p>
<p>Executing: find_fit(r, model)
Then copying and pasting the answer: [a == 2.8690476152412128, b == 76.70833334665576]</p>
<p>And then having to manually write out, where number 15 represents what I wanted to put substitute for x: 2.8690476152412128 * (15) + 76.70833334665576</p>
<p>Also 1 of my other threads has an unsolved question, incase anyone's interested:
<a href="http://ask.sagemath.org/question/2732/display-y-intercept">http://ask.sagemath.org/question/2732...</a></p>
https://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/?comment=17448#post-id-17448BTW, you are right that the `(a,b)` is extraneous - that's showing the *output* if you do this in the command line.Wed, 26 Jun 2013 14:20:55 +0200https://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/?comment=17448#post-id-17448Answer by tmonteil for <p>Thanks to this handy post, I was able to generate a linear regression graph:
<a href="http://stackoverflow.com/questions/454606/how-can-i-do-regression-analysis-in-sage">http://stackoverflow.com/questions/45...</a></p>
<p>My input thus far in sage 5.9:</p>
<pre><code>r = [[0,76.4], [1,78.5], [2,81.5], [3,87.8],
[4,88.4], [5,92.4], [6,94.0], [7,95.0]]
var('a,b')
(a, b) #It seems this statement is extraneous
model(x) = a*x+b
find_fit(r, model)
plot(model(a = find_fit(r, model)[0].rhs(),
b = find_fit(r,model)[1].rhs()),
0,15, ymin = 0, ymax = 150)
</code></pre>
<p>What would be the most practical way of substituting the x value to generate an answer instead of:</p>
<p>Executing: find_fit(r, model)
Then copying and pasting the answer: [a == 2.8690476152412128, b == 76.70833334665576]</p>
<p>And then having to manually write out, where number 15 represents what I wanted to put substitute for x: 2.8690476152412128 * (15) + 76.70833334665576</p>
<p>Also 1 of my other threads has an unsolved question, incase anyone's interested:
<a href="http://ask.sagemath.org/question/2732/display-y-intercept">http://ask.sagemath.org/question/2732...</a></p>
https://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/?answer=15145#post-id-15145I am not sure to understand your question. Does the following answer it ?
sage: sol = find_fit(r, model)
sage: A,B = (sol[0].rhs(), sol[1].rhs())
sage: f(x) = model.subs(a=A, b=B) ; f
x |--> 2.8690476190478367*x + 76.70833333333225
sage: f(15)
119.74404761904981
Wed, 26 Jun 2013 13:47:54 +0200https://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/?answer=15145#post-id-15145Comment by bxdin for <p>I am not sure to understand your question. Does the following answer it ?</p>
<pre><code>sage: sol = find_fit(r, model)
sage: A,B = (sol[0].rhs(), sol[1].rhs())
sage: f(x) = model.subs(a=A, b=B) ; f
x |--> 2.8690476190478367*x + 76.70833333333225
sage: f(15)
119.74404761904981
</code></pre>
https://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/?comment=17444#post-id-17444Thanks :D.Wed, 26 Jun 2013 18:08:33 +0200https://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/?comment=17444#post-id-17444