# 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.

Try this

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


and after that

fun[.05]


EDIT

Let's see if we can adapt the code to what I think is you repeatedly trying until you get an acceptable result.

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

fit[degree_]:=(lstplt = LinearModelFit[data, Table[x^i, {i, degree}], x];
Print[Plot[lstplt[x], {x, 0, 15}]];
reslist = Inner[List, {data}[[1, All, 1]], lstplt["FitResiduals"], List];
Print[Show[ListPlot[data], Plot[lstplt[x], {x, 0, 15}]];
Print[ListPlot[reslist]]);

select[threshold_]:=(bres = Select[reslist, Abs[#[[2]]] > threshold &];
gres = DeleteCases[reslist, Alternatives @@ bres];
gpoints = gres[[All, 1]] \[Intersection] reslist[[All, 1]];
Print[ListPlot[listtr3 = Select[data, gpoints~MemberQ~First[#] &]]]);


To use this you get your initial data.

Then you can do fit[2] or fit[4] or any other degree. That doesn't change your data and only shows you the result of fitting with that degree.

When you think you are satisfied with that you can do select[.07] or select[.05] or any other threshold. That doesn't change the result of your fit and only selects your subset and displays the result.

When you think you are satisfied with that then you can do data=listtr3

Now you are ready to repeat these three steps with new degree and new threshold.

Does that seem to accomplish what you wanted?

Please test this very carefully to make certain that there are no mistakes.

• Excellent, I need to rework it a tad. The linearmodelfit function needs to have the data set that is saved at the end of the module when I do the next iteration. So, when I do the next calculation I need listtr3 to go into the linearmodelfit and then change the p value. – Carrson Baldwin Jul 18 at 20:32
• It has been edited, hopefully it makes more sense! – Carrson Baldwin Jul 18 at 22:18