#Übung 4 #Aufgabe 1 #a) service <- read.table("http://stat.ethz.ch/Teaching/Datasets/softdrink.dat", header=T) #y=servicezeit, x1=# nachzufüllende produkte, x2=weg: servicewagen automat service summary(service) mod_1 <- lm(formula = y ~ x1 + x2,data=service) summary(mod_1) #b) newdata <- data.frame(x1=8,x2=275) pred <- predict(mod_1,newdata=newdata,interval="prediction") pred mod_1$coefficients[1] + 8*mod_1$coefficients[2] + 275*mod_1$coefficients[3] #predict macht also das was es soll #c) par(mfrow = c(2,2)) plot(mod_1) #normal q-q plot eine art zwei geteilt, 9 (evtl. 20, 22 auch noch) scheint eine einflussreiche messung zu sein. service_neu <- service[-9,] mod_2 <- lm(formula = y ~ x1 + x2,data=service_neu) summary(mod_2) plot(mod_2) pred_1 <- predict(mod_2,newdata=newdata,interval="prediction") pred_1 #wert kann genauer geschätzt werden #Aufgabe 2 schueler <- read.table("http://stat.ethz.ch/Teaching/Datasets/concept.dat", header=T) summary(schueler) #a) attach(schueler) mod_1 <- lm(formula = gpa ~ iq + alter + sex + total + c1 + c2 + c3 + c4 + c5 + c6, data=schueler) summary(mod_1) #nur iq ist signifikant stepback <- step(mod_1,direction="backward") formula(stepback) modback <- lm(formula(stepback)) summary(modback) ausgangsm <- lm(gpa~1) stepforward <- step(ausgangsm, scope=formula(mod_1),direction="forward") modfrow <- lm(formula(stepforward)) formula(stepforward) summary(modfrow) #b) par(mfrow=c(2,2)) plot(mod_1) #22,48,51 evtl. 41 sind ausreisser entfernen <- c(22,48,51) schueler_neu <- schueler[-entfernen,] attach(schueler_neu) mod_2 <- lm(gpa ~ iq + alter + sex + total + c1 + c2 + c3 + c4 + c5 + c6) summary(mod_2) par(mfrow=c(2,2)) plot(mod_2) #Aufgabe 3 #a) chriket <- read.table("http://stat.ethz.ch/Teaching/Datasets/cricket.dat", header=T) attach(chriket) mod_1 <- lm(formula = y ~ x1 + x2 + x3 + x4 + I(x1^2) + I(x4^2) + I(x1*x4)) summary(mod_1) #b) #die quadratischen terme sind nicht signifikant, mal rausnehmen und sehen was passiert mod_2 <- lm(formula = y ~ x1 + x2 + x3 + x4 + I(x1*x4)) summary(mod_2) #x1 wird nun signifikant scopeback <- list(lower=y~x1+x2+x4+I(x1*x4), upper=formula(mod_1)) modback <- step(mod_1, scope=scopeback, direction="backward") modback <- lm(formula(modback)) amod <- lm(y ~ x1 + x2 + x4 + I(x1*x4)) scopeforward <- list(lower = y ~ x1 + x2 + x4 + I(x1*x4), upper=formula(mod_1)) modforward <- step(amod, scope=scopeforward, direction="forward") #c) mod_3 <- lm(formula = y ~ x1 + x2 + x4 + I(x1*x4) + I(x2^2) + I(x1*x2) + I(x2*x4)) summary(mod_3) #werden sehr signifikant mod_4 <- lm(y ~ x1 + x2 + x4 + I(x1*x4) + I(x2^2) + I(x1*x2) + I(x2*x4)) scopeback <- list(lower = y ~ x1 + x2 + x4 + I(x1*x4), upper = formula(mod_4)) modback_2 <- step(mod_4, scope=scopeback, direction="backward") modback_2 <- lm(formula(modback_2)) amod <- lm(y ~ x1 + x2 + x4 + I(x1*x4)) scopeforward <- list(lower = y ~ x1 + x2 + x4 + I(x1*x4), upper=formula(mod_4)) modforward_2 <- step(amod, scope=scopeforward, direction="forward") par(mfrow=c(2,2)) plot(mod_4,which=c(1,4)) plot(modback_2,which=c(1,4)) plot(modforward_2,which=c(1,4)) summary(mod_4) summary(modback_2) summary(modforward_2) #d) #ausreiser 1,4,13 chriket <- chriket[-c(1,4,13),] attach(chriket) mod_4 <- lm(y ~ x1 + x2 + x4 + I(x1*x4) + I(x2^2) + I(x1*x2) + I(x2*x4)) scopeback <- list(lower = y ~ x1 + x2 + x4 + I(x1*x4), upper = formula(mod_4)) modback_2 <- step(mod_4, scope=scopeback, direction="backward") modback_2 <- lm(formula(modback_2)) amod <- lm(y ~ x1 + x2 + x4 + I(x1*x4)) scopeforward <- list(lower = y ~ x1 + x2 + x4 + I(x1*x4), upper=formula(mod_4)) modforward_2 <- step(amod, scope=scopeforward, direction="forward") par(mfrow=c(2,2)) plot(mod_4,which=c(1,4)) plot(modback_2,which=c(1,4)) plot(modforward_2,which=c(1,4)) summary(mod_4) summary(modback_2) summary(modforward_2) #etwa das gleiche