library(fastICA) ### See Westra et al. (2007, 2008) ##Monthly Colorado River flows at Lees Ferry ### 1906 - 2006 test=matrix(scan("http://civil.colorado.edu/~balajir/CVEN5454/r-project-data/LeesFerry-monflow-1906-2006.txt"),ncol=13,byrow=T) allMonths = test[,2:13]/10^6 #MAF ## Scale Data - mean removed. ### you can also divide by standard deviation X = scale(allMonths,scale=FALSE) ### Get the monthly mean and monthly SD allMonSd = apply(allMonths, 2, sd) allMonMean = apply(allMonths, 2, mean) ### Perform ICA K = dim(X)[2] # number of ICA components to obtain zout = fastICA(X,K) #### zout$S ~ Independent Components ##### zout$S is equivalent to Principal Components #### zout$A ~ Mixing Matrix equivalent to Eigen Vectors ### X = AS YY = zout$S %*% zout$A Xback = t(t(YY) + allMonMean) ## XK = Y ## prewhitened matrix Xpre = X %*% zout$K ICS = Xpre %*% zout$W ### ICS and zout$S should be the same.. ## S is the ICA = YW ################################################### ### PCA #get variance matrix.. Xf=scale(allMonths) zs=var(Xf) #do an Eigen decomposition.. zsvd=svd(zs) #Principal Components... pcs=Xf %*% zsvd$u flowpcs = pcs #Eigen Values.. - fraction variance lambdas=(zsvd$d/sum(zsvd$d)) ############################## For Problem 8 ### Perform ICA on the SST data #### Perform ICA on the Rainfall data ### Using the exact same data from Problem 7 ### Investigate the first four ICA ## (i) & (ii) Plot the first four Mixing Vectors ~ similar to first four Eigen Vectors ## Compare with the first four Eigen vector patterns you saw from Problem 7 ## Also plot the first four ICs as time series and do a histogram to see their distribution ### Compare with the first four PCS ### (iii) Relate the first three or four ICs of SST with that of rainfall #### compare with the same from the PCs in problem 7 ### Correlate the first three ICs of SST with rainfall and vice-versa and plot ### correlation maps. Compare with that of problem 7