【问题标题】:LinEst for polynomial function vba多项式函数 vba 的 LinEst
【发布时间】:2020-06-07 22:16:29
【问题描述】:

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

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

其中 A 和 C 是数组 OUTPUT 很好。我可以使用Application.Index(OUTPUT,3)OUTPUT读取r2

但是,当我想通过将 Array 添加到 Array 的参数来尝试 y=x+x^2+b 时:

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

我无法使用Application.Index(OUTPUT,3)OUTPUT读取r2

有什么解决办法吗?我做错了什么?


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

【问题讨论】:

    标签: excel vba


    【解决方案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
    

    编辑

    我尝试在工作表中使用=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 中,我无法将值分配给数组作为双精度值。但是当在没有数据类型的情况下在下面的 cmets 中尝试 @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
    

    【讨论】:

    • 谢谢!!!哦!我想我找出了问题所在。我的 xVal 和 yVal 未定义为“范围”,但它们被定义为:将 xVal(1 到 10) 调暗为 Double。 vba 代码用值填充 xVal。是否可以将我的 xVal 数组转换为 LinEst 可以使用的格式?再次感谢
    • @Ramon 虽然 xvalues 和 yvalues 的数组方向需要相同,但多项式的数组方向必须与 xvalues 和 yvalues 不同。因此,例如,对于 2 的多项式阶数,请尝试 Application.Transpose(Array(1, 2))
    • 我尝试在工作表中使用=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 中,我无法将值分配给数组作为双精度值。但是,当在没有数据类型的情况下尝试 @Domenic 的建议时,它起作用了。谢谢多梅尼克
    • @Domenic 谢谢!进步,一定!我使用了 Application.Transpose 并且它有效;然而,新的问题出现了!我将 r2 的值存储在一个变量中,如下所示: R_SQUARE = Application.Index(WorksheetFunction.LinEst(yVal, Application.Power(xVal, Application.Transponse(Array(1, 2))), True, True), 3) ,但我无法将 R_SQUARE 与另一个数值变量进行比较。例如,如果我使用 If R_SQUARE > 0 Then... 我得到一个错误。但是,如果我使用 Activecell.Value = R_SQUARE 可以完美地工作,我会在工作表中得到 r2 。如何将 R_SQUARE 与代码中的数字进行比较?谢谢!!!
    • R_SQUARE 始终为正,因此无需检查它是否 >0。此外,我尝试使用 R_SQUARE 在 VBA 中进行进一步计算,但有 type mismatch error 。在 StackOverflow 上提问here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    相关资源
    最近更新 更多