Skip to main content
2 of 3
added 48 characters in body

How to use Module function

Here is my issue; I want to iterate a fit to this data for a polynomial of degree 2 up till degree 6. However, at each iteration I drop certain data points that have a poor residual value. So each time the degree is raised, a subset of the previous data set's fit is used. However I will also need to change the value of the max residual that I want to include the .07 in the code below).

data = Table[{x, RandomReal[{-.1, .1}] + x^2}, {x, 0, 15}]

lstplt = LinearModelFit[data, Table[x^i, {i, 2}], x]
Plot[lstplt[x], {x, 0, 15}]
reslist = 
  Inner[List, {data}[[1, All, 1]], lstplt["FitResiduals"], List];
Show[ListPlot[data], Plot[lstplt[x], {x, 0, 15}]]
ListPlot[reslist]
bres = Select[reslist, Abs[#[[2]]] > .07 &];
gres = DeleteCases[reslist, Alternatives @@ bres];
gpoints = gres[[All, 1]] \[Intersection] reslist[[All, 1]];
ListPlot[listtr3 = Select[data, gpoints~MemberQ~First[#] &]]

Again, the idea is that it would fit a quadratic, then take out residual values greater than .07. Take those data points that have a residual greater than .07 and delete them. Then take that set and fit a cubic polynomial, and find points that have greater than a different residual value and delete them. Iterate this up to degree n polynomial.