1
$\begingroup$

I have two lists containing X/Y values which are used to plot points on a graph. The X-values are always sequential (1, 2, 3, 4...) but the Y values can vary.

For example:

var x = new List<int>(new[] {0, 1, 2, 3, 4, 5, 6, 7});
var y = new List<int>(new[] {0, 3, 7, 12, 14, 15, 17, 19});
var avgY = // What goes here?

With these values (the Y-values being more important in this case), how can I calculate the average rate of change linear regression line so that I can plot another "average" line (or "prediction" line if we were to extend the graph)?

$\endgroup$
3
  • $\begingroup$ It's called 'Linear regression'. After quick googling found a link that should give you everything you need $\endgroup$
    – ULazdins
    Feb 15, 2012 at 15:23
  • $\begingroup$ There are a lot of possible answers - have you looked into keywords / topics like curve fitting, splines, derivatives, extrapolation? $\endgroup$
    – Mikeb
    Feb 15, 2012 at 15:24
  • $\begingroup$ For extending the line, would there be data points in this area? Or are you talking about extending the line past your data points? $\endgroup$
    – Michelle
    Feb 16, 2012 at 20:23

2 Answers 2

3
$\begingroup$

You should start out with linear regression, and see if that gives you what you want. Unfortunately C# doesn't make linear regression particularly easy, but have a browse around some statistics packages and see if you can find one that looks easy to use.

I took advantage of R's quick modelling capabilities to do a linear regression with your data, to see if this is the kind of thing you're after:

x = c(0,1,2,3,4,5,6,7)
y = c(0,3,7,12,14,15,17,19)
model = lm(y ~ x)
plot(x,y)
abline(coef(model))

which gives the following output. Is this the kind of thing you want? enter image description here

$\endgroup$
0
1
$\begingroup$

@ChrisTaylor has provided the basic answer to your question. I just want to add another detail. You ask, "how can I calculate the... linear regression line so that I can plot... [the] "prediction" line if we were to extend the graph?" Doing this is called extrapolation, and it is generally considered to be a very dangerous practice. See here for a simple overview of the topic.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy