analytics mit r: prüfung von verteilungen (normalverteilung)
TRANSCRIPT
Prof. Dr. Jan Kirenz
Analytics mit RPrüfung der Verteilung (Normalverteilung)
Prof. Dr. Jan Kirenz
Deskriptive Statistik
Schritte zur Ermittlung der passenden Kennzahlen Statistische Kennzahlen
(1) Skalenniveau (2) Prüfung der Verteilung Lagemaße Streuungsmaße
Nominal - Modus -
Ordinal - ModusMedian Quartilsabstand
Metrisch
Fall 1: Daten sind nicht normalverteilt
ModusMedian Quartilsabstand
Fall 2: Daten sind normalverteilt
ModusMedian
Mittelwert
QuartilsabstandStandardabweichung
Ausgehend von der Verteilung kann z.B. entschieden werden, welche Kennzahlen berechnet werden können:
Prof. Dr. Jan Kirenz
Deskriptive Statistik
Schritte zur Ermittlung der passenden Kennzahlen Statistische Kennzahlen
(1) Skalenniveau (2) Prüfung der Verteilung Lagemaße Streuungsmaße
Nominal -Modus
nicht relevant für die Prüfung
-
Ordinal -ModusMedian
median(...)
QuartilsabstandIQR(...)
quantiles()
Metrisch
Fall 1: Daten sind nicht normalverteilt
ModusMedian
median(...)
QuartilsabstandIQR(...)
quantiles()
Fall 2: Daten sind normalverteilt
ModusMedian
median(...)Mittelwertmean(...)
QuartilsabstandIQR(...)
quantiles()Standardabweichung
sd(...)
Kennzahlen mit R-codes
Prof. Dr. Jan Kirenz
Test auf Normalverteilung mit R
l Visuelle Prüfung: - Histogramm - Q-Q Plot- Dichtefunktion- Boxplot
l Testverfahren: - Shapiro–Wilk-Test
16
Prof. Dr. Jan Kirenz
Test auf Normalverteilung: Histogramm
l Pakete laden: library(ggplot2) library(dplyr)l Histogramm erstellen hist(mtcars$mpg)
l Histogramm mit Änderung der Standardoptionen:
hist(mtcars$mpg, breaks = 20, main='Histogramm der Variable mpg', xlab = 'Miles per Gallon')
17
Prof. Dr. Jan Kirenz
Test auf Normalverteilung: Q-Q Plot
l Q-Q Plot erstellen qqnorm(mtcars$mpg)qqline(mtcars$mpg)
18
Prof. Dr. Jan Kirenz
Test auf Normalverteilung: Dichtefunktion
l Pakete laden: library(ggplot2) library(dplyr)l Kerndichteschätzer (Kernel Density Plot)d <- density(mtcars$mpg) plot(d)l Alternativ mit ggplot:dichte_1 <- ggplot(data = mtcars) + geom_density(mapping= aes(x=mpg))plot(dichte_1)
19
Prof. Dr. Jan Kirenz
Test auf Normalverteilung: Boxplot
20
Box plots 133
6.5 Box plotsA “box-and-whiskers” plot describes the distribution of a continuous variable by plot-ting its five-number summary: the minimum, lower quartile (25th percentile), median(50th percentile), upper quartile (75th percentile), and maximum. It can also displayobservations that may be outliers (values outside the range of ± 1.5*IQR, where IQR isthe interquartile range defined as the upper quartile minus the lower quartile). Forexample:
boxplot(mtcars$mpg, main="Box plot", ylab="Miles per Gallon")
produces the plot shown in figure 6.11. I added annotations by hand to illustrate thecomponents.
By default, each whisker extends to the most extreme data point, which is no morethan the 1.5 times the interquartile range for the box. Values outside this range aredepicted as dots (not shown here).
For example, in our sample of cars the median mpg is 19.2, 50 percent of the scoresfall between 15.3 and 22.8, the smallest value is 10.4, and the largest value is 33.9. Howdid I read this so precisely from the graph? Issuing boxplot.stats(mtcars$mpg)
10
15
20
25
30
Box plot
Upper hinge
Upperquart ile
Lowerquart ile
Lower hinge
W hisker
M edian
W hisker
Mes
Per
Ga
on
Figure 6.11 Box plot with annotations added by hand
Prof. Dr. Jan Kirenz
Test auf Normalverteilung: Boxplot
l Boxplot (Box-Whisker-Plot) ist ein Diagramm, welches zur Visualisierung der Verteilung einer mindestens ordinalskalierten Variablen verwendet wird.
l Boxplot in R mit ggplot:
ggplot(data = datensatz) + geom_boxplot(mapping = aes( y = testvariable))
l Wenn sich der Median mittig in der Box befindet, deutet dies darauf hin, dass die Daten normalverteilt sind.
21
Prof. Dr. Jan Kirenz
Test auf Normalverteilung: Shapiro-Wilk-Testl Anwendbar bei Stichproben vom Umfang 3 ≤ n ≤ 5000l Nullhypothese: Daten sind normalverteiltl Alternativhypothese: Daten sind nicht normalverteilt
l Shapiro-Wilk-Test in R:shapiro.test(mtcars$mpg)l Ergebnis: data: mtcars$mpgW = 0.94756, p-value = 0.1229
l Wenn p-value > 0.05, dann liegt die Normalverteilung vor. Ansonsten nicht.
22
Prof. Dr. Jan Kirenz
Test auf Normalverteilung in R für mehrere „Gruppen“l Visuelle Prüfung (Dichtefunktion, Boxplot)l Testverfahren (Shapiro–Wilk-Test)
l Wenn wir Variablen (z.B. Variable Körpergröße) mit unterschiedlichen „Gruppen“ (z.B. Variable Geschlecht mit Gruppe 1=Frauen und Gruppe 2 =Männer) untersuchen, ist es sinnvoll, die Verteilungen der Gruppen getrennt voneinander zu untersuchen
l Testvariable = Variable, die untersucht werden solll Gruppenvariable = Variable, nach welcher die Gruppen eingeteilt werden sollen
(die Gruppierungsvariable muss in R als factor definiert sein)
23
Source: Kabacoff (2011)
Prof. Dr. Jan Kirenz
Test auf Normalverteilung für mehrere Gruppen: Dichtefunktionl Pakete laden: library(ggplot2) library(dplyr)l Beispiel: Daten: mtcars, Variable: am = Schaltung
(0=Automatik, 1=Handschaltung)l Zunächst Anpassung des Skalenniveaus der Variable
von metrisch (dbl) zu Faktor (fctr), damit die Gruppierung vorgenommen werden kann: nehmen kann:
mtcars$am <- as.factor(mtcars$am)
l # Darstellung von beiden Dichtefunktionen in einer Abbildung:
dichte_2 <- ggplot(data = mtcars) + geom_density(mapping= aes(x=mpg, colour=am))plot(dichte_2)
24
Prof. Dr. Jan Kirenz
Test auf Normalverteilung für mehrere Gruppen: Dichtefunktionl Pakete laden: library(ggplot2) library(dplyr)l Beispiel: Daten: mtcars, Variable: am = Schaltung
(0=Automatik, 1=Handschaltung)l Zunächst Anpassung des Skalenniveaus der Variable
von metrisch (dbl) zu Faktor (fctr), damit die Gruppierung vorgenommen werden kann: nehmen kann:
mtcars$am <- as.factor(mtcars$am)
l # Darstellung von beiden Dichtefunktionen in einer Abbildung mit optischer Variation:
dichte_3 <- ggplot(data = mtcars) + geom_density(alpha=0.1, mapping= aes(x=mpg, colour=am, fill=am)) plot(dichte_3)
25
Prof. Dr. Jan Kirenz
Test auf Normalverteilung für mehrere Gruppen: Boxplotl Der Boxplot (Box-Whisker-Plot) ist ein Diagramm, welches zur Visualisierung der
Verteilung einer mindestens ordinalskalierten Variablen verwendet wird. l Boxplot in R mit ggplot:
ggplot(data = datensatz) + geom_boxplot(mapping = aes( x= gruppenvariable, y = testvariable))
l Wenn sich der Median mittig in der Box befindet, deutet dies darauf hin, dass die Daten normalverteilt sind.
26
Prof. Dr. Jan Kirenz
Test auf Normalverteilung für mehrere Gruppen: Shapiro-Wilk-Testl Anwendbar bei Stichproben vom Umfang 3 ≤ n ≤ 5000l Nullhypothese: Daten sind normalverteiltl Alternativhypothese: Daten sind nicht normalverteiltl Wenn p-value > 0.05, dann liegt die Normalverteilung vor. Ansonsten nicht.
l Vorgehensweise, wenn die Verteilung von verschiedenen Gruppen für eine Variable mit dem Shapiro-Wilk-Test in R getestet werden soll:
l tapply(datensatz$testvariable, datensatz$gruppenvariable, shapiro.test)l Mit „tapply“ werden die Gruppen erzeugt
27