#Aufgabe 3 x<-c(20,16,34,23,27,32,18,22) y<-c(64,61,84,70,88,92,72,77) #3a) plot(x,y) #3b) modell<-lm(y~x) abline(modell$coefficient[1], modell$coefficient[2]) #3c) #Berechne Teststatistik T #zu 2b) gamma<-0.99 S_xx<-var(x) S_squared <- function(x, y, modell) { S<-0 for(i in 1:length(y)){ S<-S+(y[i]-modell$coefficient[1]- modell$coefficient[2]*x[i])^2 } return(S / (length(y)-2)) } T<- (abs(modell$coefficient[2]))* (sqrt((length(x)-1)*S_xx))/(sqrt(S_squared(x,y,modell))) T qt(1-(1-gamma)/2, length(x)-2) #zu 2c) LU <- function(x, y, modell, x0, gamma) { t<- qt(1-(1-gamma)/2, length(x)-2) L<- modell$coefficient[1]+modell$coefficient[2]*x0 - t* sqrt(S_squared(x,y,modell)) * sqrt(1/length(x)+((x0-mean(x))^2)/((length(x-1))*var(x))) U<- modell$coefficient[1]+modell$coefficient[2]*x0 + t* sqrt(S_squared(x,y,modell)) * sqrt(1/length(x)+((x0-mean(x))^2)/((length(x-1))*var(x))) return(cbind(L,U)) } LU(x,y,modell,30,0.90) #zu 2d) Prognose <- function(x, y, modell, x0, gamma) { t<- qt(1-(1-gamma)/2, length(x)-2) L<- modell$coefficient[1]+modell$coefficient[2]*x0 - t* sqrt(S_squared(x,y,modell)) * sqrt(1+1/length(x)+((x0-mean(x))^2)/((length(x-1))*var(x))) U<- modell$coefficient[1]+modell$coefficient[2]*x0 + t* sqrt(S_squared(x,y,modell)) * sqrt(1+1/length(x)+((x0-mean(x))^2)/((length(x-1))*var(x))) return(cbind(L,U)) } Prognose(x,y,modell,30,0.90)