# class 11 examples for correlation # loader function for Albany, NY Central Park # station data source("scripts/loaddata.R") # Albany station data: USW00014735 # only January, July anomalies buffer<-loaddata() # time series Albany and NY Central Park January plot(c(1950,2012),c(-6,6),typ='n',xlab='year',ylab='Tavg [C]',main="Monthly Anomalies") lines(buffer$time,buffer$Albany, typ='b',pch=1,col=4) lines(buffer$time,buffer$Central_Park, typ='b',pch=3,col=2) res<-readline("Next plot: a scatter plot") # scatter plot Albany July with NY Central Park July y<-buffer$Central_Park x<-buffer$Albany plot(c(-6,6),c(-6,6),typ='n', xlab='Alb. Tavg [C]',ylab='NYC Tavg [C]',main="Monthly Anomalies") points(x,y,pch=3) res<-readline("Is the distribution of y depending on the value of x?") ineg<-( x< 0) ipos<- (x > 0) xneg<-x[ineg] xpos<-x[ipos] yxneg<-y[ineg] yxpos<-y[ipos] points(xneg,yxneg,pch=1,cex=0.8,col=4) points(xpos,yxpos,pch=1,cex=0.8,col=2) lines(c(-8,8),c(0,0),col="gray") lines(c(0,0),c(-6,6),col="gray") print(summary(yxneg)) print(summary(yxpos)) res<-readline("Is the distribution of y depending on the value of x?") ineg<-( x< 0) ipos<- (x > 0) xneg<-x[ineg] xpos<-x[ipos] yxneg<-y[ineg] yxpos<-y[ipos] # plotting the center of the two sub-samples # pch=2 creates an 3 # cex=2 makes it large points(mean(xneg),mean(yxneg),pch=3,lwd=5,cex=5,col=5) points(mean(xpos),mean(yxpos),pch=3,lwd=5,cex=5,col=6) res<-readline("Fit a linear regression line into the scatter plot") # regression line y<-buffer$Central_Park x<-buffer$Albany res<-lm(y ~ x ) print(summary(res)) lines(x,res$fitted,col=3)