## R code to accompany Lecture 2 of ## Intermediate Data Analytics & Machine Learning ## Robert B. Gramacy, Virginia Tech library("mvtnorm") ## pdf("corr.pdf") par(mfrow=c(2,2), mai=c(.5,.5,.1,.1)) plot(rmvnorm(200,rep(0,2), matrix(c(1,1,1,1), ncol=2)), pch=20, xlab="", ylab="", xlim=c(-3,3), ylim=c(-3,3)) text(x=-2, y=2.5, "corr = 1", col=2, cex=1.3) plot(rmvnorm(200,rep(0,2), matrix(c(1,.5,.5,1), ncol=2)), pch=20, xlab="", ylab="", xlim=c(-3,3), ylim=c(-3,3)) text(x=-2, y=2.5, "corr = .5", col=2, cex=1.3) plot(rmvnorm(200,rep(0,2), matrix(c(1,.8,.8,1), ncol=2)), pch=20, xlab="", ylab="", xlim=c(-3,3), ylim=c(-3,3)) text(x=-2, y=2.5, "corr = .8", col=2, cex=1.3) plot(rmvnorm(200,rep(0,2), matrix(c(1,-.8,-.8,1), ncol=2)), pch=20, xlab="", ylab="", xlim=c(-3,3), ylim=c(-3,3)) text(x=2, y=2.5, "corr = -.8", col=2, cex=1.3) ## dev.off() ## pdf("strangecor.pdf", height=4) par(mfrow=c(1,2), mai=c(.5,.5,.5,.1)) x <- rnorm(200) y <- -x^2 + rnorm(200,0,.2) z <- cbind(x,y) plot(z, pch=20, xlab="", ylab="", main=paste("corr =", round(cor(z[,1], z[,2]),2))) z <- rbind(rmvnorm(199,c(0,0)), c(20,20)) plot(z, pch=20, xlab="", ylab="", main=paste("corr =", round(cor(z[,1], z[,2]),2))) ## dev.off()