Simulationen in R: Korrelation und Regression
29. Juni 2020
Abstract
Diese Dokument soll dir einen leichten Überblick verschaffen, wie man eine simple Simulation durchführt und wie man Korrelations- und Regressionsanylsen durchführt und interpretiert.Zufallszahlen
Wie erzeugt man Zufallszahlen?
Um in R Zufallszahlen erzeugen zu können gibt es verschiedene Algorithmen, die alle gut dokumentiert sind. In diesem Schritt werden zufällige Werte für das Einkommen, Alter und die Kinderanzahl für 350 Beobachtungen aus Normalverteilungen gezogen. Dabei wird immer ein Mittelwert und eine Standardabweichung mitkodiert. Das Geschlecht wird aus einer Gleichverteilung für 350 Beobachtungen gezogen, da es hier binär mit Null und Eins kodiert wird (auch wenn es mehr gender Auspägungen gibt).
set.seed(54321) # Zufallszahlengenerator
inc <- round(rnorm(350, mean=2800, sd=850)) #sample aus einer Normalverteilung
# ein zufälliges Einkommen zwischen 450 und 8500 Euro, runde auf 2 Dezimalstellen
sex <- round(runif(350, min=0, max=1)) # sample aus einer Gleichverteilung
# das zufällige Geschlecht 0=Männlich 1=weiblich, runde auf integre Werte
child <- round(rnorm(350, mean=0.8, sd=0.47)) # sample aus einer Normalverteilung
# die zufällige Kinderanzahl,runde auf integre Werte
age <- round(rnorm(350, mean = 47.7, sd=12.4)) # sample aus einer Normalverteilung
# das zufällige Alter, runde auf integre Werte
data <- data.frame(inc, sex, child, age) #Erzeuge einen data.frame mit allen Variablen
X <- data.frame(sex, inc) # Um Cramer´s V Kontingenzkoeffizent zu berechnen benötigen wir einen eignen data.frame
Korrelation
# Forschungsfrage: Korreliert das Einkommen mit den möglichen Einflussfaktoren?
Um diese Frage zu beantworten wird im nächsten Schritt wird eine Korrelationsanalyse durchgeführt. Einkommen, und Alter ist metrisch, Kinderanzahl ordinal und das Geschlecht binär kodiert.
# Hypothese: Das monatliche Einkommen ist mit höheren Werten des Alters im Schnitt höher.
cor.test(inc, age, alternative = "greater", conf.level = 0.95) # Pearson´s r Korrelation
##
## Pearson's product-moment correlation
##
## data: inc and age
## t = -0.050646, df = 348, p-value = 0.05732
## alternative hypothesis: true correlation is greater than 0
## 95 percent confidence interval:
## -0.0907648 1.0000000
## sample estimates:
## cor
## -0.002714911
Pearson´s r Produkt-Moment Korrelation liegt zwischen -1 und 1. Der Wert ist hier negativ und vernachlässigbar gering in seiner Stärke. Der P-Wert testet die Signifikanz der Effektgröße. Die Signifikanz gibt an wie wahrscheinlich es ist, solch einen Wert oder extremer in einer Stichprobe zu erhalten, wenn man unendlich viele Stichproben zöge. Im vorliegenden Beispiel ist der Wert auf 94 prozentigen Konfidenzniveau signifikant. Um diese Aussage zu vereinfachen könnte man sagen, dass von von 100 gezognen Stichproben nur in 6 Stichproben der Wert nicht wie vorliegend auffindbar wäre. In 94 Stichproben wäre es der wahre Wert (zur Logik der klassischen Inferenz siehe das Kapitel zu Wahrscheinlichkeit).
# Hypothese: Das Einkommen sinkt mit der Anzahl der Kinder im Schnitt.
cor.test(inc, child, alternative = "less", conf.level = 0.95, method = "kendall") # Kendalls´tau-b Korrelation
##
## Kendall's rank correlation tau
##
## data: inc and child
## z = 0.95607, p-value = 0.8305
## alternative hypothesis: true tau is less than 0
## sample estimates:
## tau
## 0.04086507
Der Kendalls tau-b Wert zeigt einen positiven Zusammenhang zwischen monatlichen Einkommen und Kinderanzahl an. Er liegt zwischen -1 und 1. Steigt also das Einkommen, steigt die Kinderanzahl und umgekehrt, jedoch ist die Effektgröße vernachlässigbar. Der p-Wert gibt an, dass dieser Effekt bei einem Konfidenzniveau von 95 Prozent und einer Irrtumswahrscheinlichkeit von 5 Prozent insifnifikant ist. Der Effekt liegt also nicht so vor, wie angenommen.
# Hypothese: Das Einkommen von weiblich kodierten Beobachtungen ist im Schnitt geringer.
library("lsr") # install.packages("lsr") # Um Cramer´s V zu berechnen benötigen wir das Paket lsr
cramersV(X) # Effektgröße von Geschlecht auf Einkommen
## [1] -0.01469437
Wir können feststellen, dass die binär geschlechtlichen Kategorien lediglich einen vernachlässigbar kleinen Effekt auf das monatliche Einkommen aufweisen. Wenn das Geschlecht weiblich ausgeprägt ist, ist das Einkommen marginal geringer.
Zur Erinnerung: Wir nutzen simulierte Daten.
Regression
# Forschungsfrage: Haben das Geschlecht, die Kinderanzahl und das Alter von Befragten einen Effekt auf das monatliche Einkommen?
Berechnen wir ein Regressionsmodell mit den simulierten Daten und lassen uns direkt die gesamte Zusammenfassung des Modells anzeigen.
summary(lm(inc ~ sex + child + age, data)) # Fasse das Regressionsmodell in der Ausgabe zusammen
##
## Call:
## lm(formula = inc ~ sex + child + age, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2311.43 -586.80 -47.31 569.33 2211.24
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2658.5261 192.9940 13.775 <2e-16 ***
## sex -169.1493 91.3061 1.853 0.0648 .
## child -68.1095 83.1670 0.819 0.4134
## age 0.2745 3.5353 0.078 0.9382
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 850.9 on 346 degrees of freedom
## Multiple R-squared: 0.01151, Adjusted R-squared: 0.002941
## F-statistic: 1.343 on 3 and 346 DF, p-value: 0.2602
Überblick über die Zusammenfassung:
- Wir haben unter “call” das Modell als Formel und die Angabe das dafür der Datensatz data genutzt wurde.
- Unter “residuals” gibt uns R die 5-Punkte-Zusammenfassung der Residualanalyse aus.
- “Coefficients” ist für die allgemeine Interpreation der interessante Teil der Zusammenfassung.
- “Signif. codes” gibt nur an, wie die Signifikanztestergebnisse unter “coefficients” angezeigt wird.
- Im unteren Teil haben wir noch “residual standard error”, “multiple R-squared” und die “F-statistic”.
Zur Interpretation: In Bezug auf die simulierten Daten können wir feststellen, dass das Geschlecht und die Kinderanzahl einen negativen Effekt auf das Einkommen hat. Wobei das Geschlecht auf dem 90 Prozent Konfidenznieveau signifikant ist.
Das Alter hingegen hat einen positiven Effekt auf das Einkommen. Dies geht jeweils aus der Spalte “estimate” hervor.
Die Zeile “Intercept” steht für den y-Achsenabschnitt und stellt das Nullmodell dar. Also das Modell, wenn alle inkludierten Variablen einen Wert von Null haben. In solch einem Falle würde man sagen, dass eine theoretische Beobachtung eines Mannes (weil Null kodiert), ohne Kinder und Null Jahren ein durchschnittliches monatliches Einkommen von 2658,53 Euro besäße.
In Bezug auf, dass Geschlecht interpretieren wir, dass wenn eine Beobachtung als weiblich kodiert ist (=1), dann sinkt die abhängige Variable Einkommen durchschnittlich um 169,15 Euro.
Steigt die Kinderanzahl um jeweils ein weiteres Kind, so verringert sich das durchscnittliche monatliche Einkommen einer beobachteten Person um jeweils 68,11 Euro.
Steigt das Alter um ein Lebensjahr so steigt im Schnitt das monatliche Einkommen um 0,2745 Euro.
Der Standardfehler in der Spalte “Std.Error” zeigt, dass es einen sehr hohen Fehler bei diesen Aussagen gibt. Es weichen also viele Fälle von diesen durchschnittlichen Aussagen ab. Die beiden letzten Spalten bilden die Signifikanztests für die Effektstärken ab.
Alle Variablen sind auf dem 95 Prozent Niveau insignifikant, lediglich die Ordinate ist signifikant. Wir können bei den beobachtenten simulierten Fällen nicht davon ausgehen, dass die durchschnittlichen linearen Effekte so vorliegen, wie eben interpretiert. Auch die F-Statistik belegt, dass das gesamte Modell insignifikant ist.
R² liegt zwischen 0 und 1 und kann daher umgerechnet auf prozentuale Angaben interpretiert werden. Es gibt an, dass lediglich 1,151 Prozent der Varianz durch das multiple Modell erklärt werden können. Im Umkehrschluss bedeutet das, dass 98,849 Prozent der Streuung der abhängigen Variablen “monatliches Einkommen” nicht durch die unabhängigen Variablen im Modell erklärt werden konnten.
Es müssten hier weitergehende Analysen durchgeführt werden, um festzustellen ob und inwiefern Effekte auf das monatliche Einkommen vorliegen.
Um noch einmal festzuhalten. Dieses Beispiel soll nur verdeutlichen, wie eine Regression in R durchgeführt wird und wie eine Regression im allgemeinen interpretiert wird. Es handelt sich um simulierte Fälle, keine echten Personen!
Fraglich bleibt: Wie kann ich Regressionen grafisch darstellen? Wie wird eine Modellüberprüfung (Diagnostik) durchgeführt? Wie kann ich weiterführende Tests durchführen? Was kann man noch alles mit Zufallszahlen anstellen? Wie wird das ganze mathematisch berechnet?
Gemeinsam können wir diese Fragen und weitere gerne beantworten. Schreibe mir einfach eine Mail.