# Why is slope of my linear fit the wrong sign for my data? [closed]

For my lab, I have to fit a line to a linear set of data. Pretty simple. I've done it before with good results using the Fit function. Well this time it didn't work--as the picture below shows, the slope of the data I have is going very clear positive slope, the linear fit Mathematica spits out is not just wrong but negative.

data = {{Sin[21*2*Pi/360], 587.6}, {Sin[339.5*Pi/180],
587.6}, {Sin[(360 - 342.5)*Pi/180], 501.5}, {Sin[18*Pi/180],
501.5}, {Sin[(360 - 343.8)*Pi/180], 471.3}, {17.8*Pi/180,
471.3}, {Sin[16.6*Pi/180], 438.8}, {Sin[(360 - 343)*Pi/180],
438.8}, {Sin[19.6*Pi/180], 546.1}, {Sin[(360 - 341)*Pi/180],
546.1}, {Sin[15.5*Pi/180], 435.8}, {Sin[(360 - 345)*Pi/180],
435.8}, {Sin[20.9*Pi/180], 579.0}, {Sin[(360 - 339.9)*Pi/180],
579.0}, {Sin[(360 - 343)*Pi/180], 491.6}, {Sin[17.8*Pi/180],
491.6}}

ListPlot[data]

Fit[data, {1, x}, x] 538.641 - 84.6634 x

• Add PlotRange -> All to the ListPlot – you'll understand why you obtain a negative slope. – corey979 Nov 18 at 21:23
• I did that, and it's still positive. – Elizabeth Kaiser Nov 18 at 21:29
• You have an outlier which affects your fitting significantly. – Silvia Nov 21 at 6:54

data = {{Sin[21*2*Pi/360], 587.6}, {Sin[339.5*Pi/180],
587.6}, {Sin[(360 - 342.5)*Pi/180], 501.5}, {Sin[18*Pi/180],
501.5}, {Sin[(360 - 343.8)*Pi/180], 471.3}, {17.8*Pi/180,
471.3}, {Sin[16.6*Pi/180], 438.8}, {Sin[(360 - 343)*Pi/180],
438.8}, {Sin[19.6*Pi/180], 546.1}, {Sin[(360 - 341)*Pi/180],
546.1}, {Sin[15.5*Pi/180], 435.8}, {Sin[(360 - 345)*Pi/180],
435.8}, {Sin[20.9*Pi/180], 579.0}, {Sin[(360 - 339.9)*Pi/180],
579.0}, {Sin[(360 - 343)*Pi/180], 491.6}, {Sin[17.8*Pi/180],
491.6}}


If data is numericized as

N@data


{{0.358368, 587.6}, {-0.350207, 587.6}, {0.300706, 501.5}, {0.309017, 501.5}, {0.278991, 471.3}, {0.310669, 471.3}, {0.285688, 438.8}, {0.292372, 438.8}, {0.335452, 546.1}, {0.325568, 546.1}, {0.267238, 435.8}, {0.258819, 435.8}, {0.356738, 579.}, {0.34366, 579.}, {0.292372, 491.6}, {0.305695, 491.6}}

we can see the second point is a rather extreme outlier in the data. Fit of course includes this point in the fit, which gives the negative slope.

If we ignore the outlier with

DeleteCases[N@data, {x_?Negative, _}]


{{0.358368, 587.6}, {0.300706, 501.5}, {0.309017, 501.5}, {0.278991, 471.3}, {0.310669, 471.3}, {0.285688, 438.8}, {0.292372, 438.8}, {0.335452, 546.1}, {0.325568, 546.1}, {0.267238, 435.8}, {0.258819, 435.8}, {0.356738, 579.}, {0.34366, 579.}, {0.292372, 491.6}, {0.305695, 491.6}}

then we get the expected result.

Fit[DeleteCases[N@data, {x_?Negative, _}], {1, x}, x]


-15.0492 + 1675.17 x

Show[
ListPlot[data, PlotRange -> Full],
Plot[
Evaluate@Fit[DeleteCases[N@data, {x_?Negative, _}], {1, x}, x],
{x, 0.2, 0.4}
]
] 