gpt4 book ai didi

excel - 多项式函数 vba 的 LinEst

转载 作者:行者123 更新时间:2023-12-04 22:21:51 24 4
gpt4 key购买 nike

我正在尝试使用 vba 计算多项式回归。首先,我尝试了 y=x^2+b:

OUTPUT = WorksheetFunction.Application.LinEst (A,Application.Power(C,2),True,True)

其中 A 和 C 是数组
OUTPUT很好。我可以阅读 r2来自 OUTPUT使用 Application.Index(OUTPUT,3)
但是,当我想尝试 y=x+x^2+b通过将 Array 添加到 Array 的参数中:
OUTPUT = WorksheetFunction.Application.LinEst (A,Application.Power(C,Array(1,2)),True,True)

我无法阅读 r2来自 OUTPUT使用 Application.Index(OUTPUT,3)
有什么解决办法吗?我究竟做错了什么?

解决方案:
R_SQUARE = Application.Index(WorksheetFunction.LinEst(yVal, Application.Power(xVal, Application.Transponse(Array(1, 2))), True, True), 3,1)

最佳答案

尝试跟随..

Sub LinEst()
Dim yVal As Range, xVal As Range
Set yVal = Range("C5:C14")
Set xVal = Range("B5:B14")

'You tried following formula which gives incorrect results for polynomial order 2
Range("B17") = Application.Index(WorksheetFunction.LinEst(yVal, _
Application.Power(xVal, 2), True, True), 3)

'For linear
Range("B18") = Application.Index(WorksheetFunction.LinEst(yVal, _
xVal, True, True), 3)
'For polynomial order 2
Range("B19") = Application.Index(WorksheetFunction.LinEst(yVal, _
Application.Power(xVal, Array(1, 2)), True, True), 3)
'For polynomial order 3
Range("B20") = Application.Index(WorksheetFunction.LinEst(yVal, _
Application.Power(xVal, Array(1, 2, 3)), True, True), 3)

End Sub

enter image description here

编辑

我试过 =INDEX(LINEST({3,2,5,7,4,2,1,-2,-5,-1},{0,1,2,3,4,5,6,7,8,9},TRUE,TRUE),3)在工作表中。但是在 VBA 中,我无法将值分配给数组作为 double 值。但是,当在没有数据类型的情况下在下面的评论中尝试@Domenic 的建议时,它起作用了。

以下作品。
Sub LinEst()
'Dim xVal(1 To 10) As Double, yVal(1 To 10) As Double 'This fails

xVal = Application.Transpose(Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
yVal = Application.Transpose(Array(3, 2, 5, 7, 4, 2, 1, -2, -5, -1))

'For polynomial order 3
Range("B20") = Application.Index(WorksheetFunction.LinEst(yVal, _
Application.Power(xVal, Array(1, 2, 3)), True, True), 3)

End Sub

关于excel - 多项式函数 vba 的 LinEst,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62226403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com