# Linear regression in a chosen range of points

Hi I'm absolutely newbie in Mathematica and have following problem:

My list looks like {{50, 0.75}, {51, 0.76}, ..., and I want to choose a range out of my x-values (I don't want so say: from point 150 to ..., I want to choose with values like from x value 50 to 60), with only these points a linear regression has to be done.

I tried with LinearModelFit[data, x, x], and Mathematica made a linear regression with all values, now I want to choose a range but I can't find a solution =(

Has somebody an idea?

-
You can use Select to filter your data. – b.gatessucks May 9 '13 at 18:43

As suggested by @b.gatessucks you can use Select.

data = Table[{x, Sin[x] + RandomReal[{-0.5, 0.5}]}, {x, 0, 15, 0.25}];
pl = ListPlot[data];
ff = LinearModelFit[data, Sin[x], x]
Show[pl, Plot[ff[x], {x, 0, 15}]];


and after selection over the range [2,8]:

ff2 = LinearModelFit[Select[data, #[[1]] > 2 && #[[1]] < 8 &], Sin[x], x]
Show[pl, Plot[{ff[x],ff2[x]}, {x, 0, 15}]];


-
Thanks a lot, I will try it ;) – Jacccy May 9 '13 at 19:33
The Select[] looks a bit nicer if you use a compound inequality: 2 < #[[1]] < 8 &. – J. M. May 10 '13 at 1:54

You can dynamically select the desired data range by using locators:

data = Table[{x, x x }, {x, 0, 100}];
Manipulate[((p1 = Sort[p][[All, 1]];
lm = LinearModelFit[Select[data, (p1[[1]] < #[[1]] < p1[[2]] &)], x, x];
Show[ListPlot[data, PlotRange -> 10^4 {-1, 1}],
Plot[lm[x], {x, 0, 100},  PlotStyle -> Red]]),
{{p, {{1, 0}, {100, 0}}}, {1, 0}, {100, 0}, Locator}]


-