############# ## SHEET 2 ## ############# ################################################## ## Exercise 1 ################################################## ## load data d.banknoten <- read.table("http://www.stat.math.ethz.ch/~stahel/courses/multivariate/datasets/banknoten.dat", header=TRUE) ## extract unforged notes (CODE==0), get rid of CODE variable d.banknoten <- d.banknoten[d.banknoten[,"CODE"]==0,-1] ## 1a r.mean <- mean(d.banknoten) r.sd <- sd(d.banknoten) round(r.mean,3) round(r.sd,3) ## 1b r.var <- var(d.banknoten) r.cor <- cor(d.banknoten) round(r.var, 2) round(r.cor, 2) ## 1c C <- solve(t(chol(r.var))) d.banknoten.st <- as.data.frame(scale(d.banknoten, scale=FALSE) %*% t(C)) ## the function "scale" type converts the data frame ## "d.banknoten" into a matrix, and "as.data.frame" turns the ## matrix back into a data frame par(mfrow=c(2,1)) plot(RIGHT~LEFT,data=d.banknoten.st,main="transformed data") plot(RIGHT~LEFT,data=d.banknoten,main="original data") ################################################## ## Exercise 2 ################################################## ## 2a t.d <- iris[iris[,"Species"] == "setosa", 1:4] r.mean <- mean(t.d) r.sd <- sd(t.d) round(r.mean,3) round(r.sd,3) ## 2b round(var(t.d), 2) round(cor(t.d), 2) ## 2c n <- nrow(t.d) one <- rep(1,n) t.dc <- as.matrix(t.d) - one %*% t(r.mean) covEst <- t(t.dc) %*% t.dc / (n - 1) round(covEst,2) ################################################## ## Exercise 3 ################################################## ## 3a xbar <- c(-1.5,0.3) Sigma.x <- rbind(c(1.1,-0.3),c(-0.3,1.3)) a <- c(-1,2) B <- rbind(c(2,0),c(1,-4)) ybar <- a+B%*%xbar Sigma.y <- B %*% Sigma.x %*% t(B) ybar Sigma.y