## Zufallszahlen reproduzieren #### rnorm(1) rnorm(1) set.seed(123) rnorm(1) rnorm(1) set.seed(123) rnorm(1) rnorm(1) ## Fehlende Werte #### library(mice) ## data ?boys str(boys) head(boys) ## Look at missing values md.pattern(boys) ## 223 rows are complete 223/748 ## only 30% of rows are complete 1622/(748*9) ## 24% missing values in total ## Just for illustrative purposes: ## We want to explain the head circumference (hc) by age, hgt and wgt ## complete case analysis ?na.omit datCC <- na.omit(boys) dim(boys) dim(datCC) fmCC <- lm(hc ~ age + hgt + wgt, data = datCC) summary(fmCC) ## Single Imputation (Conditional Mean imputation using missForest) str(boys) library(missForest) set.seed(32) impMF <- missForest(boys) impMF$OOBerror datImp <- impMF$ximp str(datImp) fmMF <- lm(hc ~ age + hgt + wgt, data = datImp) summary(fmMF) ## Schoene und einfache Streudiagramme: ggplot2 #### ## Herkömmliche Grafik plot(x=1:10, y=1:10, col = 2, pch = 2, xlab = "x-Achse", ylab = "y-Achse", main = "Haupttitel") points(x=c(2,2), y=c(3,4), col = c(3,4), pch = c(3,4)) lines(x=1:10, y=(1:10)/2) ?plot ?plot.default ## qplot: Wir beschränken uns auf Streudiagramme, aber qplot kann viel mehr... library(ggplot2) ?diamonds dim(diamonds) head(diamonds) str(diamonds) qplot(carat, price, data = diamonds) set.seed(1410) # Make the sample reproducible dsmall <- diamonds[sample(nrow(diamonds), 100), ] qplot(carat, price, data = dsmall) qplot(log(carat), log(price)-10, data = dsmall) ## einfache Formeln qplot(carat, price, data = dsmall, color = color) ## Plotfarbe wird durch Faktor "color" bestimmt qplot(carat, price, data = dsmall, color = color, shape = cut) ## zudem: Plotsymbol durch Faktor "cut" bestimmt qplot(carat, price, data = dsmall, size = depth) ## Symbolgroesse durch kont. Var. "depth" bestimmt ## Punktwolke und geglättete Verlaufskurve mit Standardfehler qplot(carat, price, data = dsmall, geom = c("point", "smooth")) ## ein plot pro Faktorstufe: facets qplot(carat, price, data = dsmall, facets = color ~.) ## Ein Plot pro Farbe (zeilenweise) qplot(carat, price, data = dsmall, facets = .~color) ## Ein Plot pro Farbe (spaltenweise) qplot(carat, price, data = dsmall, facets = cut~color) ## Ein Plot pro Farbe (spaltenweise) und cut (zeilen) ## die üblichen plot-Argumente funktionieren auch qplot(carat, price, data = dsmall, xlab = "Price ($)", ylab = "Weight (carats)", main = "Price-weight relationship")