#Fit the Least Squares Line to bivariate normal data. #Start with Z_1, Z_2 iid standard normal. #Construct X=Az #where z = (z_1, z_2)' and A is 2x2 with independent standard normal entries. #Cov(X)=AA' and this yields the correlation which is printed out. # n=50 A=0*diag(2) A[1,1]=rnorm(1) A[1,2]=rnorm(1) A[2,1]=rnorm(1) A[2,2]=rnorm(1) # xbig=NULL for(iter in 1:n) xbig=cbind(xbig,A%*%rnorm(2)) covmat=A%*%t(A) rho=covmat[1,2]/sqrt(covmat[1,1]*covmat[2,2]) plot(xbig[1,],xbig[2,],xlab="x",ylab="y") title(main=paste("Simulated Data and Least Squares Line n=",n, ", Corr(X,Y)=",round(rho,2))) abline(lsfit(xbig[1,],xbig[2,])) #