带 MS 图表的线性回归/趋势线

发布于 2024-10-11 00:49:20 字数 726 浏览 3 评论 0原文

我的数据在 x 和 y 上都是数字,并使用 mschart 4.0 将它们绘制成图表,

我需要将趋势线/线性回归添加到我拥有的一堆点中。 x 和 y 上的数据都是数字(任何地方都没有日期),例如 (33.4,45.1) 将是一个点。

在我从第一个链接下载的示例中,我在代码文件 Forecasting.aspx(.cs) 中找到了一个线性回归示例,并且我发现了这个 ms 文档

我已在图​​表中添加了一条线性回归线,其中包含以下行(一旦设置了所有其他数据等),

Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, parameters, "Input:Y", "Forecasting:Y,Range:Y,Range:Y2");

这两个问题都在于它们假设使用日期。无论如何,我继续实施它,但我的回归线从 x 值 20 左右开始,如果我给参数 period 一个值,则回归线将转到其中一个点的最大 x 值(几乎 70)的 x 值700。但由于它不是从 x 值 0 开始,我不相信它是正确的。

有人知道如何实现这个吗?

I have data that are numbers both on x and y and have charted them using mschart 4.0

I need to add a trend line/linear regression to a bunch of points I have. The data on x and y are both numbers (no dates anywhere), for instance (33.4,45.1) would be a point.

In the samples I downloaded from the first link I found a linear regression sample in the code files forecasting.aspx(.cs), and I found this ms documentation

I have added a linear regression line to the chart with the following line (once all other data it setup etc)

Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, parameters, "Input:Y", "Forecasting:Y,Range:Y,Range:Y2");

The problem with both of these is that they assume dates are used. I went ahead and implemented it anyway, but my regression line starts at around the x value of 20, and will go to an x value of the max x value of one of the points (almost 70) if I give the parameter Period a value of 700. But since it doesn't start at x value 0 I don't trust it is correct.

Anyone have any idea on how to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱&纷飞 2024-10-18 00:49:20

我的解决方案是根据散点图中的最小和最大 x 值在每条趋势线的开头和结尾添加额外的数据点。

这可以通过

  • 计算趋势线的斜率
  • 计算趋势线的截距
  • 获取散点图中的最小 x 值
  • 获取散点图中的最大 x 值
  • 通过使用斜率、截距和最小 x 值计算最小 y 值
  • 计算使用斜率、截距和最大 x 值计算最大 y 值
  • 将具有 x 和 y 值的两个点添加到趋势线

以下代码片段将起点和终点添加到两条趋势线,然后使用简单的函数来计算斜率和截距:

       if (Chart1.Series["Budget Year"].Points.Count > 2 &&  Chart1.Series["Actual Last Year"].Points.Count > 2)
        {
            Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, "Linear,0,false,false", Chart1.Series["Budget Year"], Chart1.Series["Trendline (Budget Year)"]);
            Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, "Linear,0,false,false", Chart1.Series["Actual Last Year"], Chart1.Series["Trendline (Actual Last Year)"]);

            double budgetSlope = Utility.GetSlope(Chart1.Series["Trendline (Budget Year)"].Points[0], Chart1.Series["Trendline (Budget Year)"].Points[1]);
            double lastYearSlope = Utility.GetSlope(Chart1.Series["Trendline (Actual Last Year)"].Points[0], Chart1.Series["Trendline (Actual Last Year)"].Points[1]);
            double budgetIntercept = Utility.GetIntercept(Chart1.Series["Trendline (Budget Year)"].Points[0], Chart1.Series["Trendline (Budget Year)"].Points[1]);
            double lastYearIntercept = Utility.GetIntercept(Chart1.Series["Trendline (Actual Last Year)"].Points[0], Chart1.Series["Trendline (Actual Last Year)"].Points[1]);
            double minBudgetRevenue = Convert.ToDouble(dt.Select("RevCurrBudget = MIN(RevCurrBudget)")[0]["RevCurrBudget"]);
            double maxBudgetRevenue = Convert.ToDouble(dt.Select("RevCurrBudget = MAX(RevCurrBudget)")[0]["RevCurrBudget"]);
            double minLastYearRevenue = Convert.ToDouble(dt.Select("RevPrevActual = MIN(RevPrevActual)")[0]["RevPrevActual"]);
            double maxLastYearRevenue = Convert.ToDouble(dt.Select("RevPrevActual = MAX(RevPrevActual)")[0]["RevPrevActual"]);
            double minBudgetEBIT = (budgetSlope * minBudgetRevenue) + budgetIntercept;
            double maxBudgetEBIT = (budgetSlope * maxBudgetRevenue) + budgetIntercept;
            double minLastYearEBIT = (lastYearSlope * minLastYearRevenue) + lastYearIntercept;
            double maxLastYearEBIT = (lastYearSlope * maxLastYearRevenue) + lastYearIntercept;

            Chart1.Series["Trendline (Budget Year)"].Points.InsertXY(0, minBudgetRevenue, minBudgetEBIT);
            Chart1.Series["Trendline (Budget Year)"].Points.AddXY(maxBudgetRevenue, maxBudgetEBIT);
            Chart1.Series["Trendline (Actual Last Year)"].Points.InsertXY(0, minLastYearRevenue, minLastYearEBIT);
            Chart1.Series["Trendline (Actual Last Year)"].Points.AddXY(maxLastYearRevenue, maxLastYearEBIT);
        }


public static double GetSlope(DataPoint pt1, DataPoint pt2)
{
    return GetSlope(pt1.XValue, pt1.YValues[0], pt2.XValue, pt2.YValues[0]);
}

public static double GetSlope(double x1, double y1, double x2, double y2) 
{
    return (y2 - y1) / (x2 - x1);
}

public static double GetIntercept(DataPoint pt1, DataPoint pt2)
{
    double slope = GetSlope(pt1, pt2);
    double y = pt1.YValues[0];
    double x = pt1.XValue;
    return y - (slope * x);
}

My solution is to add an extra datapoint at the beginning and end of each trendline based on the minimum and maximum x values in the scatterplot.

This can be done by

  • calculating the slope of the trendline
  • calculating the intercept of the trendline
  • getting the minimum x value in the scatterplot
  • getting the maximum x value in the scatterplot
  • calculating the minimum y value by using the slope, intercept and minimum x value
  • calculating the maximum y value by using the slope, intercept and maximum x value
  • adding two points with the x and y values to the trendline

The following code snippet adds start and end points to two trendlines followed by simple functions to calculate slope and intercept:

       if (Chart1.Series["Budget Year"].Points.Count > 2 &&  Chart1.Series["Actual Last Year"].Points.Count > 2)
        {
            Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, "Linear,0,false,false", Chart1.Series["Budget Year"], Chart1.Series["Trendline (Budget Year)"]);
            Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, "Linear,0,false,false", Chart1.Series["Actual Last Year"], Chart1.Series["Trendline (Actual Last Year)"]);

            double budgetSlope = Utility.GetSlope(Chart1.Series["Trendline (Budget Year)"].Points[0], Chart1.Series["Trendline (Budget Year)"].Points[1]);
            double lastYearSlope = Utility.GetSlope(Chart1.Series["Trendline (Actual Last Year)"].Points[0], Chart1.Series["Trendline (Actual Last Year)"].Points[1]);
            double budgetIntercept = Utility.GetIntercept(Chart1.Series["Trendline (Budget Year)"].Points[0], Chart1.Series["Trendline (Budget Year)"].Points[1]);
            double lastYearIntercept = Utility.GetIntercept(Chart1.Series["Trendline (Actual Last Year)"].Points[0], Chart1.Series["Trendline (Actual Last Year)"].Points[1]);
            double minBudgetRevenue = Convert.ToDouble(dt.Select("RevCurrBudget = MIN(RevCurrBudget)")[0]["RevCurrBudget"]);
            double maxBudgetRevenue = Convert.ToDouble(dt.Select("RevCurrBudget = MAX(RevCurrBudget)")[0]["RevCurrBudget"]);
            double minLastYearRevenue = Convert.ToDouble(dt.Select("RevPrevActual = MIN(RevPrevActual)")[0]["RevPrevActual"]);
            double maxLastYearRevenue = Convert.ToDouble(dt.Select("RevPrevActual = MAX(RevPrevActual)")[0]["RevPrevActual"]);
            double minBudgetEBIT = (budgetSlope * minBudgetRevenue) + budgetIntercept;
            double maxBudgetEBIT = (budgetSlope * maxBudgetRevenue) + budgetIntercept;
            double minLastYearEBIT = (lastYearSlope * minLastYearRevenue) + lastYearIntercept;
            double maxLastYearEBIT = (lastYearSlope * maxLastYearRevenue) + lastYearIntercept;

            Chart1.Series["Trendline (Budget Year)"].Points.InsertXY(0, minBudgetRevenue, minBudgetEBIT);
            Chart1.Series["Trendline (Budget Year)"].Points.AddXY(maxBudgetRevenue, maxBudgetEBIT);
            Chart1.Series["Trendline (Actual Last Year)"].Points.InsertXY(0, minLastYearRevenue, minLastYearEBIT);
            Chart1.Series["Trendline (Actual Last Year)"].Points.AddXY(maxLastYearRevenue, maxLastYearEBIT);
        }


public static double GetSlope(DataPoint pt1, DataPoint pt2)
{
    return GetSlope(pt1.XValue, pt1.YValues[0], pt2.XValue, pt2.YValues[0]);
}

public static double GetSlope(double x1, double y1, double x2, double y2) 
{
    return (y2 - y1) / (x2 - x1);
}

public static double GetIntercept(DataPoint pt1, DataPoint pt2)
{
    double slope = GetSlope(pt1, pt2);
    double y = pt1.YValues[0];
    double x = pt1.XValue;
    return y - (slope * x);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文