1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
Option Base 1
Function stat(matxy() As Variant) As Variant
'la fonction recoit en paramètres une matrice de taille (x,2)
'où x est quelconque
'matxy(x,1)=valeur x
'matxy(x,2)=valeur y
les valeurs nulles sont écartées du calcul
Dim rep(14, 2) As Variant
Dim nbval As Long
Dim somx As Double
Dim somx2 As Double
Dim somy As Double
Dim somy2
Dim somxy As Double
Dim prodecar As Double
Dim dimension As Long
Dim moyx As Double
Dim moyy As Double
Dim boucle As Long
For boucle = 1 To UBound(matxy)
If Not (IsEmpty(matxy(boucle, 1)) Or IsEmpty(matxy(boucle, 2))) Then
nbval = nbval + 1
somx = somx + matxy(boucle, 1)
somy = somy + matxy(boucle, 2)
somxy = somxy + (matxy(boucle, 1) * matxy(boucle, 2))
somx2 = somx2 + (matxy(boucle, 1) * matxy(boucle, 1))
somy2 = somy2 + (matxy(boucle, 2) * matxy(boucle, 2))
End If
Next boucle
rep(1, 1) = "nombre valeurs: "
rep(1, 2) = nbval
rep(2, 1) = "somme des x: "
rep(2, 2) = somx
rep(3, 1) = "somme des y: "
rep(3, 2) = somy
rep(4, 1) = "somme des xy: "
rep(4, 2) = somxy
rep(5, 1) = "somme des x2: "
rep(5, 2) = somx2
rep(6, 1) = "pente: "
rep(6, 2) = ((nbval * somxy) - (somx * somy)) / ((nbval * somx2) - (somx * somx))
rep(7, 1) = "ordonnée origine: "
rep(7, 2) = (somy / nbval) - (rep(6, 2) * (somx / nbval))
rep(8, 1) = "moyenne x: "
moyx = somx / nbval
rep(8, 2) = moyx
rep(9, 1) = "moyenne y: "
moyy = somy / nbval
rep(9, 2) = moyy
rep(10, 1) = "écart type p x: "
rep(10, 2) = (((somx2 * nbval) - (somx * somx)) / (nbval * nbval)) ^ 0.5
rep(11, 1) = "écart type p y: "
rep(11, 2) = (((somy2 * nbval) - (somy * somy)) / (nbval * nbval)) ^ 0.5
For boucle = 1 To UBound(matxy)
If Not (IsEmpty(matxy(boucle, 1)) Or IsEmpty(matxy(boucle, 2))) Then
prodecar = prodecar + ((matxy(boucle, 1) - moyx) * (matxy(boucle, 2) - moyx))
End If
Next boucle
rep(12, 1) = "covariance: "
rep(12, 2) = prodecar / nbval
rep(13, 1) = "coefficient de corrélation: "
rep(13, 2) = rep(12, 2) / (rep(11, 2) * rep(10, 2))
rep(14, 1) = "coefficient détermination: "
rep(14, 2) = rep(13, 2) * rep(13, 2)
stat = rep
End Function
Sub testemat()
Dim mat(100, 2) As Variant
Dim rep As Variant
Dim x As Long
For x = 1 To 100
mat(x, 1) = x
mat(x, 2) = (x * 3.5) + Rnd()
Next x
rep = stat(mat)
For x = 1 To 14
Debug.Print (rep(x, 1) & rep(x, 2))
Next x
Debug.Print "tendance pour 101:" & (rep(6, 2) * 101) + rep(7, 2)
End Sub |
Partager