Excel Net图表方程与R²;价值 目标
我在Excel中有一个图表,我正在尝试在VB.Net中复制相同的图表。我可以得到正确输入的图表数据。我不知道如何在VB.Net图表控件中检索公式和R²值,如我的Excel图表所示Excel Net图表方程与R²;价值 目标,excel,vb.net,charts,regression,equation,Excel,Vb.net,Charts,Regression,Equation,我在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
当前问题 以下是在我的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.797和R²=0.9949
我试图在Vb.Net中获得相同的结果,但我无法找到这些数据存储的位置
有什么想法吗?我终于解决了我的问题。这是我在多人/线程/转换网站/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
希望这将有助于某人 你的目标不明确。您是想从Excel趋势线中提取显示的公式,还是想现在了解如何在VB.Net中计算公式。如果是前者,则检索Trendline.DataLabel.Text属性(例如:
sheet1.ChartObjects.Item(1).Chart.SeriesCollection.Item(1).TrendLines.Item(1).DataLabel.Text
)。如果是后者,则研究该算法的“最小二乘回归线”,并在代码中实现它。@TnTinMn我正在查找Excel中的DataLabel文本,而不是VB.Net图表。除了计算之外,没有其他方法了?如果您已经在使用Excel,那么您可以利用工作表公式SLOPE()
和INTERCEPT()
@ScottCraner我的Excel工作正常,我只是想在VB.Net中找到等效的公式。然后,请参见此处: