Skip to content

SP5: Linear Regression#2901

Merged
swharden merged 11 commits intoScottPlot:mainfrom
anewton:patch-1
Sep 9, 2023
Merged

SP5: Linear Regression#2901
swharden merged 11 commits intoScottPlot:mainfrom
anewton:patch-1

Conversation

@anewton
Copy link
Contributor

@anewton anewton commented Aug 27, 2023

Please review. Adds LinearRegressionLine to ScottPlot5. So it can be used with code similar to the following:

    // Linear regression line
    double firstDate = dates[0];
    double lastDate = dates[dates.Length - 1];
    var regressionLine = new ScottPlot.Statistics.LinearRegressionLine(dates, chartValues);
    AddLine(_plot.Plot, regressionLine.slope, regressionLine.offset, (firstDate, lastDate), lineWidth: 0.5F, color: XkcdColors.LightGreyBlue, label: "Trend");
}

private void AddLine(Plot plot, double slope, double offset, (double x1, double x2) xLimits, string label = "", Color? color = null, float lineWidth = 1)
{
    double y1 = xLimits.x1 * slope + offset;
    double y2 = xLimits.x2 * slope + offset;
    var scatter = plot.Add.Scatter(new double[] { xLimits.x1, xLimits.x2 }, new double[] { y1, y2 }, color);
    if (!string.IsNullOrWhiteSpace(label))
        scatter.Label = label;
}

Purpose:

// You can insert code examples like this
var newThing = new SpecialObject();

Please review.  Adds LinearRegressionLine to ScottPlot5.  So it can be used with code similar to the following:

``` 
    // Linear regression line
    double firstDate = dates[0];
    double lastDate = dates[dates.Length - 1];
    var regressionLine = new ScottPlot.Statistics.LinearRegressionLine(dates, chartValues);
    AddLine(_plot.Plot, regressionLine.slope, regressionLine.offset, (firstDate, lastDate), lineWidth: 0.5F, color: XkcdColors.LightGreyBlue, label: "Trend");
}

private void AddLine(Plot plot, double slope, double offset, (double x1, double x2) xLimits, string label = "", Color? color = null, float lineWidth = 1)
{
    double y1 = xLimits.x1 * slope + offset;
    double y2 = xLimits.x2 * slope + offset;
    var scatter = plot.Add.Scatter(new double[] { xLimits.x1, xLimits.x2 }, new double[] { y1, y2 }, color);
    if (!string.IsNullOrWhiteSpace(label))
        scatter.Label = label;
}
```
@anewton anewton changed the title Create LinearRegressionLine Create LinearRegressionLine for ScottPlot5. Aug 27, 2023
@swharden swharden mentioned this pull request Sep 9, 2023
@swharden
Copy link
Member

swharden commented Sep 9, 2023

Thanks for this PR @anewton! I'm extending it a bit, adding tests, and creating a cookbook demo. It's going to work something like this:

image

double[] xs = new double[] { 1, 2, 3, 4, 5, 6, 7 };
double[] ys = new double[] { 2, 2, 3, 3, 3.8, 4.2, 4 };

// plot original data as a scatter plot
var sp = myPlot.Add.Scatter(xs, ys);
sp.LineStyle = LineStyle.None;
sp.MarkerStyle.Size = 10;

// calculate the regression line
ScottPlot.Statistics.LinearRegression reg = new(xs, ys);

// plot the regression line
Coordinates pt1 = new(xs.First(), reg.GetValue(xs.First()));
Coordinates pt2 = new(xs.Last(), reg.GetValue(xs.Last()));
var line = myPlot.Add.Line(pt1, pt2);
line.MarkerStyle = MarkerStyle.None;
line.LineStyle.Pattern = LinePattern.Dash;
line.LineStyle.Width = 2;

// note the formula at the top of the plot
myPlot.Title($"y = {reg.Slope:0.###}x + {reg.Offset:0.###} (r²={reg.Rsquared:0.###})");

@swharden swharden changed the title Create LinearRegressionLine for ScottPlot5. SP5: Linear Regression Sep 9, 2023
@anewton
Copy link
Contributor Author

anewton commented Sep 9, 2023

Thanks for cleaning up the PR!! I know it was a bit messy and did not probably follow your repo PR requirements. Was going to come back to it and resubmit. But it looks like you have it and the changes look awesome. Very much look forward to using this in my Avalonia desktop apps. Thanks @swharden!

@swharden swharden merged commit 96e4713 into ScottPlot:main Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants