简体   繁体   English

计算加权标准偏差

[英]calculating weighted standard deviation error

I want to calculate the weighted mean and weighted standard deviation. 我想计算加权平均值和加权标准差。 I was able to calculate the mean but not the standard deviation. 我能够计算平均值,但不能计算标准偏差。 How should I go about it? 我应该怎么做?

     With Application.WorksheetFunction


        Range("AH" & 2) = .SumProduct(Columns(7), Columns(8)) / .Max(Columns(8))  'This code works very well. It calculates the mean


        Dim Nprime As Double

        Nprime = .CountIf(Range("H2:H" & lengthRows), "<>" & 0)  'This code works well


        Range("AM" & 2) = 2 * .SQRT(.SumProduct((Columns(7) - Columns(34)) ^ 2, Columns(8)) / ((Nprime - 1) * .Sum(Columns(8))) / Nprime) 'This line does not work. It should calculate the weighted standard deviation.

Range("AM" & 2) = Evaluate("2*SQRT(SumProduct((Columns(7) - weightedMean)^2), Columns(8)) / ((Nprime - 1) * .Sum(Columns(8))) / Nprime)") 'This line code evaluates with an answer of #VALUE! probably due to the titles in Row1, how do I fix the code to only evaluate the numerical values within the columns?

        End With

Not shure about the "weighted" AV or SD, but maybe you can adapt the following. 不必担心“加权” AV或SD,但也许您可以调整以下内容。 Code loads data in an array, and then gets AV and SD. 代码将数据加载到数组中,然后获取AV和SD。

Private Sub CommandButton1_Click()

    Dim MyArray() As Variant
    Dim lAV As Double
    Dim lSD As Double
    Dim i As Integer

    lLastRow = Worksheets("MyData").UsedRange.Rows.Count
    ReDim MyArray(1 To 1) As Variant

    For i = 1 To lLastRow
        MyArray(UBound(MyArray)) = Worksheets("MyData").Cells(i, 7).Value * Worksheets("MyData").Cells(i, 8).Value
        ReDim Preserve MyArray(1 To UBound(MyArray) + 1) As Variant 'now array is 1 element longer
    Next

    'Now MyArray contains desired data.
    'Get average and SD

    lAV = Application.WorksheetFunction.Average(MyArray)
    lSD = Application.WorksheetFunction.StDev(MyArray)

    'write results (be shure not to overwrite your data)
    Worksheets("MyData").Cells(1, 10) = "Average: "
    Worksheets("MyData").Cells(1, 11) = lAV
    Worksheets("MyData").Cells(2, 10) = "SD: "
    Worksheets("MyData").Cells(2, 11) = lSD
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM