Summary statistics in visual basic
- by ben
Below I am trying to write a script the goal of which is to calculate some summary statistics for a few different columns of numbers. I have gotten some help on it up to the "Need help below" mark. But beyond that I am flabergasted as to how to calculate the simple stats (sum, mean, standard deviation, coefficient of variation). I know VB has scripts for these stats, which I have included in my code, but I guess I need to do some extra declaring or something. Advice much appreciated. Thanks.
Sub TOAinput()
Const n As Integer = 648
Dim stratum(n), hybrid(n), acres(n), hhsz(n), offinc(n)
Dim s1 As Integer
Dim s2 As Integer
Dim i As Integer
For i = 1 To n
stratum(i) = Worksheets("hhid level").Cells(i + 1, 2).Value
Next i
s1 = 0
s2 = 0
For i = 1 To n
If stratum(i) = 1 Then
s1 = s1 + 1
Else:
s2 = s2 + 1
End If
Next i
Dim acres1(), hhsz1(), offinc1(), acres2(), hhsz2(), offinc2()
ReDim acres1(s1), hhsz1(s1), offinc1(s1), acres2(s2), hhsz2(s2), offinc2(s2)
'data infiles: acres, hh size, off-farm income,
For i = 1 To n
acres(i) = Worksheets("hhid level").Cells(i + 1, 4).Value
hhsz(i) = Worksheets("hhid level").Cells(i + 1, 5).Value
offinc(i) = Worksheets("hhid level").Cells(i + 1, 6).Value
Next i
s1 = 0
s2 = 0
For i = 1 To n
If stratum(i) = 1 Then
s1 = s1 + 1
acres1(s1) = acres(i)
hhsz1(s1) = hhsz(i)
offinc1(s1) = offinc(i)
Else:
s2 = s2 + 1
acres2(s2) = acres(i)
hhsz2(s2) = hhsz(i)
offinc2(s2) = offinc(i)
End If
Next i
'****************************
'Need help below
'****************************
Dim sumac1, sumac2, mhhsz1, mhhsz2, cvhhsz1, cvhhsz2
sumac1 = Sum(acres1)
sumac2 = Sum(acres2)
mhhsz1 = Average(hhsz1)
mhhsz2 = Average(hhsz2)
cvhhsz1 = StDev(hhsz1) / Average(hhsz1)
cvhhsz2 = StDev(hhsz2) / Average(hhsz2)
End Sub