remove(list=ls()) #setwd("/Users/michael/Dropbox/Angewandte Statistik SoSe11/Uebungen/Blatt06/R") setwd("/Volumes/MichaelsHD/Dropbox/Angewandte Statistik SoSe11/Uebungen/Blatt06/R") #Daten einlesen data <- matrix(scan("./data6.dat"),ncol=2,byrow=T) x <- data[,1] t <- data[,2] t2 <- t^2 beta1 <- 3 beta2 <- 0.5 #erstes Modell mod1 <- lm(x ~ t) #Punkte plotten plot(t,x) #Regressionskurve hinzufuegen lines(t,mod1$fit) #Konfidenzintervall erzeugen konf <- predict(mod1,interval="conf",level=0.95) #Konfidenzintervall hinzufuegen matlines(t,konf) #Vorhersageinterval erzeugen vorh <- predict(mod1,interval="predict",level=0.95) #Vorhersageintervall hinzufuegen matlines(t,vorh) #wahre Kurve hinzufuegen lines(t,beta1 + beta2*(t)^2,col=3) #zweites Modell mod2 <- lm(x ~ t2) plot(t,x) lines(t,mod2$fit) konf2 <- predict(mod2,interval="conf",level=0.95) matlines(t,konf2) vorh2 <- predict(mod2,interval="predict",level=0.95) matlines(t,vorh2) lines(t,beta1 + beta2*(t)^2,col=3) #drittes Modell tdach <- mean(t) tkorr <- t-tdach mod3 <- lm(x ~ tkorr) plot(t,x) lines(t,mod3$fit) konf3 <- predict(mod3,interval="conf",level=0.95) matlines(t,konf3) vorh3 <- predict(mod3,interval="predict",level=0.95) matlines(t,vorh3) lines(t,beta1 + beta2*(t)^2,col=3) #interval="conf" erzeugt 95%-Konfidenzintervall fuer Regressionskurve #interval="predict" erzeugt 95%-Konfidenzintervall fuer Punkte X_i #erstes und drittes Modell unterscheiden sich nicht pdf("./Plots.pdf") op <- par(mfrow=c(2,2)) plot(t,x,main="erstes Modell") lines(t,mod1$fit) matlines(t,konf) matlines(t,vorh) lines(t,beta1 + beta2*(t)^2,col=4) plot(t,x,main="zweites Modell") lines(t,mod2$fit) matlines(t,konf2) matlines(t,vorh2) lines(t,beta1 + beta2*(t)^2,col=4) plot(t,x,main="drittes Modell") lines(t,mod3$fit) matlines(t,konf3) matlines(t,vorh3) lines(t,beta1 + beta2*(t)^2,col=4) par(op) dev.off()