# Robuste Statistik # Blatt 6 # Aufgabe 6.1: # Datensaetze data <- list(y1 = c(2.3, 3.4, 5.6, 7.1, 8.9), y2 = c(-10, 3.4, 5.6, 7.1, 8.9), y3 = c(-10, -10, 5.6, 7.1, 8.9), y4 = c(-10, -10, -10, 7.1, 8.9), y5 = c(-10, -10, -10, -10, 8.9)) # a) # shortest_half: Funktion fuer die kuerzeste Haelfte # Eingabe: y (numeric): Datenvektor, fuer den die Schaetzung berechnet werden soll # Ausgabe: (numeric): Schaetzwert shortest_half <- function(y){ y <- sort(y) h <- ceiling(length(y)/2) y_diff <- min(diff(y, lag=h-1)) return(y_diff) } sapply(data, sd) # y1 y2 y3 y4 y5 # 2.681977 7.542215 9.492997 9.879524 8.452337 sapply(data, shortest_half) # y1 y2 y3 y4 y5 # 3.3 3.3 3.3 0.0 0.0 # verfaelschung: Funktion, welche die Verfaelschung durch Hinzufuegen berechnet # Eingabe: data (numeric): Datenvektor, auf dessen Grundlage die Verfaelschung # berechnet wird # fun (function): Schaetzfunktion, fuer die die Verfaelschung berechnet wird # x0 (numeric): Vektor mit hinzuzufuegenden Werten zu data # Ausgabe: (numeric): Verfaelschung verfaelschung <- function(data, fun, x0){ true <- fun(data) schaetzung <- sapply(x0, function(x){ dat <- c(data, x) fun(dat) }) return(schaetzung - true) } # Plot der Verfaelschung plot(seq(-5, 15, 0.01), verfaelschung(data[[1]], shortest_half, seq(-5, 15, 0.01)), type = "l", main = "Verfaelschung", xlab = expression(x[0]), ylab = "Verfaelschung", ylim = c(-2.5, 2.5)) lines(seq(-5, 15, 0.01), verfaelschung(data[[1]], sd, seq(-5, 15, 0.01)), col = "red") legend("bottomleft", legend = c("kuerzeste Haelfte", "sd"), col = c("black", "red"), lty = 1, bty = "n") # Plot der absoluten Verfaelschung plot(seq(-5, 15, 0.01), abs(verfaelschung(data[[1]], shortest_half, seq(-5, 15, 0.01))), type = "l", main = "absolute Verfaelschung", xlab = expression(x[0]), ylab = "Verfaelschung", ylim = c(-0.5, 2.5)) lines(seq(-5, 15, 0.01), abs(verfaelschung(data[[1]], sd, seq(-5, 15, 0.01))), col = "red") legend("bottomleft", legend = c("kuerzeste Haelfte", "sd"), col = c("black", "red"), lty = 1, bty = "n") # Ein tüchtiger Student hat folgende grafische Darstellung eingebaut. # Plot der Verfaelschung plot(seq(-5, 15, 0.01), verfaelschung(data[[1]], shortest_half, seq(-5, 15, 0.01)), type = "l", main = "Verfaelschung", xlab = expression(x[0]), ylab = "Verfaelschung", ylim = c(-2.5, 2.5)) lines(seq(-5, 15, 0.01), verfaelschung(data[[1]], sd, seq(-5, 15, 0.01)), col = "red") segments(2.3,-4,2.3,5.2, lty = 3, col = "red") segments(3.4,-4,3.4,5.2, lty = 3, col = "red") segments(4.5,-4,4.5,5.2, lty = 3, col = "blue") segments(4.9,-4,4.9,5.2, lty = 3, col = "blue") segments(5.6,-4,5.6,5.2, lty = 3, col = "red") segments(7.1,-4,7.1,5.2, lty = 3, col = "red") segments(7.4,-4,7.4,5.2, lty = 3, col = "blue") segments(8.9,-4,8.9,5.2, lty = 3, col = "red") axis(side = 1, y1, c(expression(y[1]), expression(y[2]), expression(y[3]), expression(y[4]), expression(y[5])) , col.axis = "red") legend("bottomleft", legend = c("kuerzeste Haelfte", "sd"), col = c("black", "red"), lty = 1, bty = "n")