' ' ****************************************************** ' * * ' * CALCUl DU CHI-DEUX D'INDEPENDANCE * ' * * ' ****************************************************** ' ' Sub chiDeux() Dimensions nblig, nbcol ' détermination du nombre de lignes et de colonnes If (nblig = 0) Or (nbcol = 0) Then MsgBox "Vous n'avez pas de données..." Exit Sub End If Titres nblig, nbcol ' noms de ligne et de colonne Marges nblig, nbcol, totg ' totaux en ligne et en colonne ValInd nblig, nbcol, totg ' valeurs sous hypothèse d'indépendance c1 = Calchi(nblig, nbcol) ' calcul de la distance du chi-deux c2 = Chith(nblig, nbcol) ' valeur du chi-deux théorique If c1 > c2 Then TabContri nblig, nbcol ' contributions dans le tableau End If ' on arrange le coin supérieur gauche Cells(1, 1).Select Cells(1, 1).Value = "Données" Cells(1, 1).Font.Bold = True End Sub ' ' Détermination du nombre de lignes et de colonnes ' Sub Dimensions(nblig, nbcol) Let lig = 2 While IsEmpty(Cells(lig, 1).Value) = False Let lig = lig + 1 Wend Let nblig = lig - 2 Let col = 2 While IsEmpty(Cells(1, col).Value) = False Let col = col + 1 Wend Let nbcol = col - 2 End Sub ' ' Recopie des noms de ligne et de colonne ' Sub Titres(nblig, nbcol) ' on laisse une colonne vide entre les données ' originales et le tableau des contributions col = nbcol + 3 For i = 1 To nblig Cells(i + 1, col).Value = " " & Cells(i + 1, 1).Value ' on met en gras et en bleu Cells(i + 1, col).Font.Bold = True Cells(i + 1, col).Font.ColorIndex = 5 Next i For j = 1 To nbcol Cells(1, j + col).Value = " " & Cells(1, 1 + j).Value ' on met en gras et en rouge Cells(1, j + col).Font.Bold = True Cells(1, j + col).Font.ColorIndex = 3 Next j ' avec un titre, c'est mieux Cells(1, nblig + 3).Value = "Théoriques" Cells(1, nblig + 3).Font.Bold = True End Sub ' ' Calcul des totaux en lignes et en colonnes ' et du total général ' Sub Marges(nblig, nbcol, totg) ' initialisation du total général totg = 0 ' totaux en ligne For i = 1 To nblig somc = 0 For j = 1 To nbcol somc = somc + Cells(i + 1, 1 + j).Value Next j Cells(i + 1, 2 * nbcol + 4).Value = somc Next i ' totaux en colonne et total gnéral For j = 1 To nbcol soml = 0 For i = 1 To nblig soml = soml + Cells(i + 1, 1 + j).Value Next i Cells(nblig + 2, nbcol + 3 + j).Value = soml totg = totg + soml Next j Cells(nblig + 2, 2 * nbcol + 4).Value = totg ' on met en gars et en vert foncé Cells(nblig + 2, 2 * nbcol + 4).Font.Bold = True Cells(nblig + 2, 2 * nbcol + 4).Font.Color = RGB(0, 150, 0) End Sub ' ' calcul des valeurs théoriques sous hypothèse d'indépendance ' Sub ValInd(nblig, nbcol, totg) col = nbcol + 3 For i = 1 To nblig For j = 1 To nbcol totlig = Cells(i + 1, 2 * nbcol + 4).Value totcol = Cells(nblig + 2, j + col).Value contri = totlig * totcol / totg Cells(i + 1, j + col).Value = contri Cells(i + 1, j + col).NumberFormat = "###.00" Next j Next i End Sub ' ' Calcul de la distance du chideux ' Function Calchi(nblig, nbcol) Cells(nblig + 3, 1).Value = " Distance chi-deux" vchi = 0 col = nbcol + 3 For i = 1 To nblig For j = 1 To nbcol obs = Cells(i + 1, j + 1).Value th = Cells(i + 1, j + col).Value dif = obs - th vchi = vchi + dif * dif / th Next j Next i Cells(nblig + 3, 3).Value = vchi Cells(nblig + 3, 3).NumberFormat = "###.000" Calchi = vchi End Function ' ' Valeur théorique du chi-deux ' Function Chith(nblig, nbcol) Cells(nblig + 4, 1).Value = " Chi-deux table " vchi = "=chiinv(0.05," & (nblig - 1) * (nbcol - 1) & ")" Cells(nblig + 4, 3).Formula = vchi ' autre solution avec la fonction en français: ' ' vchi = "=KHIDEUX.INVERSE(0,05;" & (nblig - 1) * (nbcol - 1) & ")" ' Cells(nblig + 4, 3).FormulaLocal = vchi Cells(nblig + 4, 3).NumberFormat = "####.000" Chith = Cells(nblig + 4, 3).Value End Function ' ' Affichage trié des contributions signées ' Sub TabContri(nblig, nbcol) lig = nblig + 6 Cells(lig, 1).Value = "Contributions" numlig = lig col = nbcol + 3 Let n = nblig * nbcol For i = 1 To nblig For j = 1 To nbcol numlig = numlig + 1 obs = Cells(i + 1, j + 1).Value th = Cells(i + 1, j + col).Value dif = obs - th vchi = dif * dif / th Cells(numlig, 2).Value = " -" If dif > 0 Then Cells(numlig, 2).Value = " +" End If ' ??? Cells(numlig, 2).Format.Align = "Right" Cells(numlig, 3).Value = vchi Cells(numlig, 3).NumberFormat = "###.000" Cells(numlig, 4).Value = " " & Cells(i + 1, 1).Value Cells(numlig, 5).Value = " " & Cells(1, 1 + j).Value Next j Next i ' et on trie... Range(Cells(nblig + 7, 2), Cells(nblig + 7 + n, 5)).Select Selection.Sort Key1:=Cells(nblig + 7, 3), Order1:=xlDescending, Header _ :=xlGuess, OrderCustom:=1, MatchCase:=False, _ Orientation:=xlTopToBottom End Sub