VB.Net 图表方程和 R² 值

VB.Net Chart Equation and R² Value

目标

我在 Excel 中有一个图表,我正在尝试在 VB.Net 中复制相同的图表。我可以正确输入图表数据。我不知道如何在 VB.Net 图表控件中检索方程式和 R² 值,如我的 Excel 图表所示。


当前问题

这是在我的 Excel 图表和 Vb.Net 图表中获得的数据:

    ' X       Y
    '0.895, 120.1
    '0.978, 160.1
    '1.461, 240.1
    '1.918, 320.1
    '2.343, 400.2
    '2.769, 480.2
    '3.131, 560.2
    '3.493, 640.3
    '3.797, 720.3
    '4.089, 800.3

我从中得到以下结果 (Excel):

如您所见,我收到一个公式 y= 203.83x - 62.797R²=0.9949 我试图在 Vb.Net 中获得相同的结果,但我无法找到此数据的存储位置。

有什么想法吗?

我终于弄明白我的问题了。这是我使用此站点上的多个 people/threads/conversion websites/Excel 帮助获取的 clsTrendline.vb。

Public Class Trendline

#Region "Variables"
Private m_Slope As Decimal
Private m_Intercept As Decimal
Private m_Start As Decimal
Private m_End As Decimal
Private m_RSquared As Decimal
#End Region

#Region "Properties"
Public Property Slope() As Decimal
    Get
        Return m_Slope
    End Get
    Private Set
        m_Slope = Value
    End Set
End Property
Public Property Intercept() As Decimal
    Get
        Return m_Intercept
    End Get
    Private Set
        m_Intercept = Value
    End Set
End Property
Public Property Start() As Decimal
    Get
        Return m_Start
    End Get
    Private Set
        m_Start = Value
    End Set
End Property
Public Property [End]() As Decimal
    Get
        Return m_End
    End Get
    Private Set
        m_End = Value
    End Set
End Property
Public Property RSquared As Decimal
    Get
        Return m_RSquared
    End Get
    Set(value As Decimal)
        m_RSquared = value
    End Set
End Property
#End Region

#Region "New..."
Public Sub New(yAxisValues As IList(Of Decimal), xAxisValues As IList(Of Decimal))
    Me.New(yAxisValues.[Select](Function(t, i) New Tuple(Of Decimal, Decimal)(xAxisValues(i), t)))
End Sub
Public Sub New(data As IEnumerable(Of Tuple(Of [Decimal], [Decimal])))
    Dim cachedData = data.ToList()

    Dim n = cachedData.Count
    Dim sumX = cachedData.Sum(Function(x) x.Item1)
    Dim sumX2 = cachedData.Sum(Function(x) x.Item1 * x.Item1)
    Dim sumY = cachedData.Sum(Function(x) x.Item2)
    Dim sumY2 = cachedData.Sum(Function(x) x.Item2 * x.Item2)
    Dim sumXY = cachedData.Sum(Function(x) x.Item1 * x.Item2)

    'b = (sum(x*y) - sum(x)sum(y)/n)
    '      / (sum(x^2) - sum(x)^2/n)
    Slope = (sumXY - ((sumX * sumY) / n)) / (sumX2 - (sumX * sumX / n))

    'a = sum(y)/n - b(sum(x)/n)
    Intercept = (sumY / n) - (Slope * (sumX / n))

    '    r = (n * (Exy) - (Ex * Ey)) / (((n * (Ex2) - (Ex) ^ 2) ^ (1 / 2)) * ((n * (Ey2) - (Ey) ^ 2) ^ (1 / 2)))
    RSquared = ((n * (sumXY) - (sumX * sumY)) / (((n * (sumX2) - (sumX) ^ 2) ^ (1 / 2)) * ((n * (sumY2) - (sumY) ^ 2) ^ (1 / 2)))) ^ 2

    Start = GetYValue(cachedData.Min(Function(a) a.Item1))
    [End] = GetYValue(cachedData.Max(Function(a) a.Item1))
End Sub
#End Region

#Region "Methods / Functions"
Public Function GetYValue(xValue As Decimal) As Decimal
    Return Intercept + Slope * xValue
End Function
#End Region

End Class

希望这对某人有所帮助!