### Aufgabe 1 (a) ### RB<-Total.Bill.in...incl..Tax TG<-Tip.in.. TR<-Tip.in...of.Bill ## von der Variable "Size.of.Party" haengt die Tipping Rate am staerksten ab. A1_a5<-lm(TR~Size.of.Party) anova(A1_a5) summary(A1_a5) ## boxplot boxplot(TR~Size.of.Party,main="Tipping Rate vs. Size.of.Party",xlab="Size.of.Party",ylab="Tipping Rate",border="red") ### Aufgabe 1 (b) ### ## Korrelationsmatrix A1_num<-data.frame(cbind(Sex.n=as.numeric(Sex),Smoker.n=as.numeric(Smoker),Day.n=as.numeric(Day), Time.n=as.numeric(Time),SoP.n=as.numeric(Size.of.Party),RB,TG,TR)) attach(A1_num) cor(A1_num) ## Hoch positiv korreliert sind Trinkgeld und Rechnungsbetrag. plot(RB,TG,main="Trinkgeld vs. Rechnungsbetrag",col=2,xlab="Rechnungsbetrag",ylab="Trinkgeld") ## Hoch positiv korreliert sind Rechnungsbetrag und Size.of.Party. boxplot(RB~SoP.n,main="Rechnungsbetrag vs. Size.of.Party",xlab="Size.of.Party",ylab="Rechnungsbetrag",border="red") ## Hoch positiv korreliert sind Trinkgeld und Size.of.Party. boxplot(TG~SoP.n,main="Trinkgeld vs. Size.of.Party",xlab="Size.of.Party",ylab="Trinkgeld",border="red") ## Time und Day sind stark untereinander abhaengig. mosaicplot(table(Day,Time),col=2:3,main="Day & Time") ## Time und Sex sind untereinander abhaengig. mosaicplot(table(Day,Sex),col=2:3,main="Day & Sex") ## Time und Smoker sind untereinander abhaengig. mosaicplot(table(Time,Smoker),col=3:2,main="Time & Smoker") ### Aufgabe 1 (c) ### ## ohne Beruecksichtigung der Variablen Trinkgeld und Rechnungsbetrag A1_a5<-lm(TR~Size.of.Party) ## mit Beruecksichtigung der Variablen Trinkgeld und Rechnungsbetrag A1_m<-lm(TR~RB*TG+RB*SoP.n+TG*SoP.n+Smoker) ### Aufgabe 2 (a) ### Anz.S<-c(1036303,1336674,1834341,1857906,1799338,1938811) Anz.P<-c(28220,30267,34702,37672,37794,37861) Jahr<-c(1980,1985,1992,1995,2000,2002) ## Verhaeltnisse ## Prop.S<-Anz.S/Anz.S[1] Prop.P<-Anz.P/Anz.P[1] ## Grafische Darstellung ## plot(Jahr,Prop.S,main="Plot Aufgabe 2",type="l",col=3,xlab="Jahr",ylim=c(1,2),ylab="Verhaeltnisse") lines(Jahr,Prop.P,type="l",col=4) legend(1980,1.95,c("Verhaeltnisse der Studenten","Verhaeltnisse der Professoren"),col=3:4,text.col=3:4, lty=c(1,1),cex=0.8) ### Aufgabe 2 (b) ### A2_quo<-Anz.S/Anz.P plot(Jahr,A2_quo,main="Anz.Studenten/Anz.Professoren vs. Jahr",type="l",col=2, ylab="Anz.Studenten/Anz.Professoren",xlab="Jahr",ylim=c(35,55)) ### Aufgabe 2 (c) ### ## Glaettung von "Anz. Studenten" plot(Jahr,Anz.S,main="Glaettung von Anz. Studenten",ylab="Anz. Studenten",type="l",col="green") lines(lowess(Jahr,Anz.S),lty=2,col=2) legend(1980,1850000,c("Empirische Zeitreihe","mit lowess"),col=c(3,2),text.col=c(3,2), lty=c(1,2),cex=0.8) ## Glaettung von "Anz. Professoren" plot(Jahr,Anz.P,main="Glaettung von Anz. Professoren",ylab="Anz. Professoren",type="l",col="blue") lines(lowess(Jahr,Anz.P),lty=2,col=2) legend(1980,37500,c("Empirische Zeitreihe","mit lowess"),col=c(4,2),text.col=c(4,2), lty=c(1,2),cex=0.8) ### Aufgabe 2 (d) ### plot(Jahr,A2_quo,main="Geglaetteter Quotient und Quotient der Glaettungen",col=3,type="l",ylab="Quotient") quo_gl<-lowess(Jahr,Anz.S)$y/lowess(Jahr,Anz.P)$y points(Jahr,quo_gl,type="l",lty=2,col="purple") lines(lowess(Jahr,A2_quo),col=2,lty=2) legend(1980,53,c("Quotient","Quotient der Glaettungen","Geglaetteter Quotient"),col=c("green","purple","red"), text.col=c("green","purple","red"),lty=c(1,2,2),cex=0.8) ### Aufgabe 3 ### ## Grad 1 A3_G1S<-lm(Anz.S~Jahr) A3_G1P<-lm(Anz.P~Jahr) A3_G1Q<-lm(A2_quo~Jahr) ## Grad 2 A3_G2S<-lm(Anz.S~Jahr+I(Jahr^2)) A3_G2P<-lm(Anz.P~Jahr+I(Jahr^2)) A3_G2Q<-lm(A2_quo~Jahr+I(Jahr^2)) ## Grad 3 A3_G3S<-lm(Anz.S~Jahr+I(Jahr^2)+I(Jahr^3)) A3_G3P<-lm(Anz.P~Jahr+I(Jahr^2)+I(Jahr^3)) A3_G3Q<-lm(A2_quo~Jahr+I(Jahr^2)+I(Jahr^3)) ## Grad 4 A3_G4S<-lm(Anz.S~Jahr+I(Jahr^2)+I(Jahr^3)+I(Jahr^4)) A3_G4P<-lm(Anz.P~Jahr+I(Jahr^2)+I(Jahr^3)+I(Jahr^4)) A3_G4Q<-lm(A2_quo~Jahr+I(Jahr^2)+I(Jahr^3)+I(Jahr^4)) ## Grad 5 A3_G5S<-lm(Anz.S~Jahr+I(Jahr^2)+I(Jahr^3)+I(Jahr^4)+I(Jahr^5)) A3_G5P<-lm(Anz.P~Jahr+I(Jahr^2)+I(Jahr^3)+I(Jahr^4)+I(Jahr^5)) A3_G5Q<-lm(A2_quo~Jahr+I(Jahr^2)+I(Jahr^3)+I(Jahr^4)+I(Jahr^5)) ## Grafische Darstellung (Student) x_Jahr<-seq(min(Jahr),max(Jahr),1) x_eins<-rep(1,length(x_Jahr)) yS1<-colSums(coefficients(A3_G1S)*t(as.matrix(cbind(x_eins,x_Jahr)))) yS23<-colSums(coefficients(A3_G2S)*t(as.matrix(cbind(x_eins,x_Jahr,x_Jahr^2)))) yS45<-colSums(na.omit(coefficients(A3_G4S))*t(as.matrix(cbind(x_eins,x_Jahr,x_Jahr^2,x_Jahr^4)))) plot(Jahr,Anz.S,main="Anpassung mittels polynomieller Regression (Student)",ylab="Anz. Studenten",type="l",col="black") points(Jahr,Anz.S,col="black") lines(x_Jahr,yS1,col=3,lty=2) points(Jahr,predict(A3_G1S),col=3) lines(x_Jahr,yS23,col=2,lty=2) points(Jahr,predict(A3_G2S),col=2) lines(x_Jahr,yS45,col=4,lty=2) points(Jahr,predict(A3_G4S),col=4) legend(1980,1900000,c("Empirische Zeitreihe","Poly. Regression Grad 1","Poly. Regression Grad 2&3","Poly. Regression Grad 4&5"),col=c("black",3,2,4),text.col=c("black",3,2,4),lty=c(1,2,2,2),cex=0.8) ## Grafische Darstellung (Professor) yP1<-colSums(coefficients(A3_G1P)*t(as.matrix(cbind(x_eins,x_Jahr)))) yP23<-colSums(coefficients(A3_G2P)*t(as.matrix(cbind(x_eins,x_Jahr,x_Jahr^2)))) yP45<-colSums(na.omit(coefficients(A3_G4P))*t(as.matrix(cbind(x_eins,x_Jahr,x_Jahr^2,x_Jahr^4)))) plot(Jahr,Anz.P,main="Anpassung mittels polynomieller Regression (Professor)",ylab="Anz. Professoren",type="l", ylim=c(27000,39000),col="black") points(Jahr,Anz.P,col="black") lines(x_Jahr,yP1,col=3,lty=2) points(Jahr,predict(A3_G1P),col=3) lines(x_Jahr,yP23,col=2,lty=2) points(Jahr,predict(A3_G2P),col=2) lines(x_Jahr,yP45,col=4,lty=2) points(Jahr,predict(A3_G4P),col=4) legend(1980,38000,c("Empirische Zeitreihe","Poly. Regression Grad 1","Poly. Regression Grad 2&3","Poly. Regression Grad 4&5"),col=c("black",3,2,4),text.col=c("black",3,2,4),lty=c(1,2,2,2),cex=0.8) ## Grafische Darstellung (Quotient) yQ1<-colSums(coefficients(A3_G1Q)*t(as.matrix(cbind(x_eins,x_Jahr)))) yQ23<-colSums(coefficients(A3_G2Q)*t(as.matrix(cbind(x_eins,x_Jahr,x_Jahr^2)))) yQ45<-colSums(na.omit(coefficients(A3_G4Q))*t(as.matrix(cbind(x_eins,x_Jahr,x_Jahr^2,x_Jahr^4)))) plot(Jahr,A2_quo,main="Anpassung mittels polynomieller Regression (Quotient)",ylab="Quotient",type="l",col="black") points(Jahr,A2_quo,col="black") lines(x_Jahr,yQ1,col=3,lty=2) points(Jahr,predict(A3_G1Q),col=3) lines(x_Jahr,yQ23,col=2,lty=2) points(Jahr,predict(A3_G2Q),col=2) lines(x_Jahr,yQ45,col=4,lty=2) points(Jahr,predict(A3_G4Q),col=4) legend(1980,53,c("Empirische Zeitreihe","Poly. Regression Grad 1","Poly. Regression Grad 2&3","Poly. Regression Grad 4&5"),col=c("black",3,2,4),text.col=c("black",3,2,4),lty=c(1,2,2,2),cex=0.6) ## Residuenquadratsumme RQS<-function(x){ return sum(x^2)} RQS_S<-c(RQS(resid(A3_G1S)),RQS(resid(A3_G2S)),RQS(resid(A3_G3S)),RQS(resid(A3_G4S)),RQS(resid(A3_G5S))) RQS_P<-c(RQS(resid(A3_G1P)),RQS(resid(A3_G2P)),RQS(resid(A3_G3P)),RQS(resid(A3_G4P)),RQS(resid(A3_G5P))) RQS_Q<-c(RQS(resid(A3_G1Q)),RQS(resid(A3_G2Q)),RQS(resid(A3_G3Q)),RQS(resid(A3_G4Q)),RQS(resid(A3_G5Q))) ## Grafische Darstellung (RQS vs. Grad) par(mfrow=c(1,3)) Gr<-c(1,2,3,4,5) ## Student plot(Gr,RQS_S,main="RQS vs. Grad (Student)",type="l",col=2) points(Gr,RQS_S,col=2) ## Professor plot(Gr,RQS_P,main="RQS vs. Grad (Professor)",type="l",col=2) points(Gr,RQS_P,col=2) ## Quotient plot(Gr,RQS_Q,main="RQS vs. Grad (Quotient)",type="l",col=2) points(Gr,RQS_Q,col=2) ### Aufgabe 4 (a) ### plot(Datum,Zugriffe,main="Hits Daten",col=3) abline(lm(Zugriffe~Datum),col=2,lty=2) legend(12310,3400,c("KQ-Gerade"),col=2,text.col=2,lty=2,cex=0.8) ## Aufgabe 4 (b) ## ## Modelle A4_lw1<-lowess(Datum,Zugriffe,f=0.05) A4_lw2<-lowess(Datum,Zugriffe,f=0.10) A4_lw3<-lowess(Datum,Zugriffe,f=0.20) A4_lw4<-lowess(Datum,Zugriffe,f=0.50) ## Grafische Darstellung plot(Datum,Zugriffe,main="Hits Daten",col=3) points(Datum,A4_lw1$y,type="l",lty=2,col="yellow") points(Datum,A4_lw2$y,type="l",lty=2,col="green") points(Datum,A4_lw3$y,type="l",lty=2,col="red") points(Datum,A4_lw4$y,type="l",lty=2,col="dark blue") legend(12310,3400,c("f=0.05","f=0.1","f=0.2","f=0.5"),col=c("yellow","green","red","dark blue"), text.col=c("yellow","green","red","dark blue"),lty=c(2,2,2,2),cex=0.8)