Inhaltsverzeichnis
library(carData)
library(gmodels)
library(tibble)
titanic <- TitanicSurvival
as_tibble(titanic)
# A tibble: 1,309 x 4
survived sex age passengerClass
<fct> <fct> <dbl> <fct>
1 yes female 29 1st
2 yes male 0.917 1st
3 no female 2 1st
4 no male 30 1st
5 no female 25 1st
6 yes male 48 1st
7 yes female 63 1st
8 no male 39 1st
9 yes female 53 1st
10 no male 71 1st
# ... with 1,299 more rows
Chi-Quadrat-Test für zwei Variablen
Häufigkeitstabellen
table(titanic$survived, titanic$passengerClass)
1st 2nd 3rd
no 123 158 528
yes 200 119 181
CrossTable(titanic$survived, titanic$passengerClass, prop.chisq = F)
Cell Contents
|-------------------------|
| N |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 1309
| titanic$passengerClass
titanic$survived | 1st | 2nd | 3rd | Row Total |
-----------------|-----------|-----------|-----------|-----------|
no | 123 | 158 | 528 | 809 |
| 0.152 | 0.195 | 0.653 | 0.618 |
| 0.381 | 0.570 | 0.745 | |
| 0.094 | 0.121 | 0.403 | |
-----------------|-----------|-----------|-----------|-----------|
yes | 200 | 119 | 181 | 500 |
| 0.400 | 0.238 | 0.362 | 0.382 |
| 0.619 | 0.430 | 0.255 | |
| 0.153 | 0.091 | 0.138 | |
-----------------|-----------|-----------|-----------|-----------|
Column Total | 323 | 277 | 709 | 1309 |
| 0.247 | 0.212 | 0.542 | |
-----------------|-----------|-----------|-----------|-----------|
Testdurchführung
chisq.test(table(titanic$survived, titanic$passengerClass))
Pearson's Chi-squared test
data: table(titanic$survived, titanic$passengerClass)
X-squared = 127.86, df = 2, p-value < 2.2e-16
chisq.test(table(titanic$survived, titanic$passengerClass))$p.value
[1] 1.720826e-28
Alternative
CrossTable(titanic$survived, titanic$passengerClass, prop.chisq=F, expected=T, chisq=T)
Cell Contents
|-------------------------|
| N |
| Expected N |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 1309
| titanic$passengerClass
titanic$survived | 1st | 2nd | 3rd | Row Total |
-----------------|-----------|-----------|-----------|-----------|
no | 123 | 158 | 528 | 809 |
| 199.623 | 171.194 | 438.183 | |
| 0.152 | 0.195 | 0.653 | 0.618 |
| 0.381 | 0.570 | 0.745 | |
| 0.094 | 0.121 | 0.403 | |
-----------------|-----------|-----------|-----------|-----------|
yes | 200 | 119 | 181 | 500 |
| 123.377 | 105.806 | 270.817 | |
| 0.400 | 0.238 | 0.362 | 0.382 |
| 0.619 | 0.430 | 0.255 | |
| 0.153 | 0.091 | 0.138 | |
-----------------|-----------|-----------|-----------|-----------|
Column Total | 323 | 277 | 709 | 1309 |
| 0.247 | 0.212 | 0.542 | |
-----------------|-----------|-----------|-----------|-----------|
Statistics for All Table Factors
Pearson's Chi-squared test
------------------------------------------------------------
Chi^2 = 127.8592 d.f. = 2 p = 1.720826e-28
Verwendete Pakete und Funktionen
base
Funktion | Beschreibung |
---|---|
table(Variable1, Variable2) | Einfache Kreuztabelle erstellen. |
gmodels
Funktion | Beschreibung |
---|---|
CrossTable( Variable1, Variable2, prop.chisq = F) | Erweiterte Kreuztabelle erstellen. – Anteil an der Chi-Quadrat-Verteilung wird nicht benötigt. |
CrossTable( Variable1, Variable2, prop.chisq = F, expected=T chisq=T) | Zusätzlich Ausgabe der erwarteten Häufigkeiten und das Ergebnis des Chi-Quadrat-Tests. |
stats
Funktion | Beschreibung |
---|---|
chisq.test(table()) | Chi-Quadrat-Test durchführen. – Wird angewendet auf die Kreuztabelle. – ❗ Die Angabe zum p-Wert ist etwas irreführend. Bei einem sehr kleinen p-Wert ist das Ergebnis wie folgt formuliert: p-value < Zahl. |
chisq.test()$p.value | 👉 Der exakte p-Wert lässt sich auf diese Weise abfragen. |
tibble
Funktion | Beschreibung |
---|---|
as_tibble() | Eine Datensatz, wie bspw. einen Data Frame, eine List oder Matrix in ein sogenanntes Tibble umwandeln. Ein Tibble ist ein Data Frame der Klasse tbl_df. |