# 2014-03-31 # Introduction of matrix operations # calculation of the covariance matrix # clear memory rm(list=ls()) # reset the plot window to full sized plot par(mfrow=c(1,1)) source("scripts/loadano2.R") # note this logical variable will allow us # to calculate the correlation matrix # by first normalizing the station data # to be centered and normalized to standard # deviation 1 iscor<-TRUE varname<-"tavg" month<-"Jan" start<-1950 end<-2012 # sample size, used to define matrix n<-end-start+1 stationlist<-c("USW00014735","USW00014750","USW00014771","USW00094728") # variables (stations), used to define matrix m<-length(stationlist) # define a n x m matrix filled with NA entries X<-matrix(NA,n,m) # here I form a loop to repeat the loading of the # data for the stations given in stationlist for ( i in 1:m) { buffer<-loadano2(station=stationlist[i],state="NY",month=month,start=start,end=end) mhelp<-mean(buffer$ano) # now I assign the centered data vector # into the column i of the matrix if (iscor) { # standardize to get the correlation coefficients # NOTE: standard deviation function is using 1/(n-1) # factor, but below we work with 1/n # That's why sqrt(n-1/n) shelp<-sd(buffer$ano)*sqrt((n-1)/n) X[,i]<-(buffer$ano-mhelp)/shelp } else { # default is the covariance will be calculated # from centered data X[,i]<-(buffer$ano-mhelp) } } print("size of the data matrix X: ") print(dim(X)) # the function t() is forming the transpose # of matrix X print("size of the transpose of data matrix X: ") print(dim(t(X))) # the matrix multiplication # %*% is the operator for matrix multiplication C<-t(X)%*%X # multiply the matrix with factor 1/n # to get the covariance (correlation matrix) C<-1/n*C if (iscor) { print("size of the correlation matrix: ") print(dim(C)) print ("Correlation Matrix") print(C) } else { print("size of the Covariance matrix: ") print(dim(C)) print ("Covariance Matrix") print(C) } # The variance for the four variables (stations) # are stored on the diagonal (top left down to bottom right) for ( i in 1:m){ print(paste("Variance for ",stationlist[i]," :",C[i,i]),sep='') }