4
$\begingroup$

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?

$\endgroup$
1
  • 4
    $\begingroup$ You can use Select to filter your data. $\endgroup$ – b.gates.you.know.what May 9 '13 at 18:43
6
$\begingroup$

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}]];

$\endgroup$
1
  • 1
    $\begingroup$ The Select[] looks a bit nicer if you use a compound inequality: 2 < #[[1]] < 8 &. $\endgroup$ – J. M.'s ennui May 10 '13 at 1:54
3
$\begingroup$

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}]

enter image description here

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.