-
Notifications
You must be signed in to change notification settings - Fork 1
/
iPCA_Process.R
224 lines (154 loc) · 7.55 KB
/
iPCA_Process.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Iterative Principal Component Analysis (iPCA) for covariate data reduction
#
# This method is currently being implemented in R by Dave White, Soil Scientist, USDA-NRCS
#
# For questions or comments please contact Dave at : [email protected]
#
# Iterative principal component analysis (iPCA)
#
# iPCA utilizes principal component analysis to narrow down a set of covariates.
#
# For more information on iPCA see:
# Matthew R. Levi, Craig Rasmussen, Covariate selection with iterative principal component analysis for predicting physical soil properties, Geoderma, Volumes 219-220, 2014,Pages 46-57,ISSN 0016-7061,https://doi.org/10.1016/j.geoderma.2013.12.013.(http://www.sciencedirect.com/science/article/pii/S0016706113004412)
#
# and
#
# J.R. Jensen, Introductory Digital Image Processing: A Remote Sensing Perspective, (3rd. ed.), Prentice Hall, Upper Saddle River, NJ (2005)
#
# The following is the step by step walk through of iPCA.
# load and install required packages ####
required.packages <- c("raster", "sp", "rgdal", "factoextra")
new.packages <- required.packages[!(required.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
lapply(required.packages, require, character.only=T)
rm(required.packages, new.packages)
# Load Raster Data
setwd("C:/DSM/test_cov")
# read raster layer names to create raster stack
r.list=list.files(getwd(), pattern="tif$", full.names = FALSE)
r.list
#create raster stack of covariates
r.stack <- stack(r.list)
names(r.stack)
# for the following data reduction technique it is necessary to convert the raster stack into a data frame. This may cause problems with large raster stacks. my recommendation is to subset the raster stack by creating a regular sample of points.
# If you need to create a regular sample due to a large set of covariates, that could be completed as follows:
#df.sR <- na.omit(as.data.frame(sampleRegular(r.stack, na.rm=T, xy=F, size = (length(r.stack$gnp_clay_index57)/2))))
#
# Another method is to run through the iPCA on the DEM derived covariates. Then run through the process on Spectral derived covariates. Finally combine the two reduced DEM and Spectral covariates into one data frame and run the iPCA process one last time.
df <- na.omit(as.data.frame(r.stack, na.rm=T, xy=F))
##########################################################################
# iterative principal component analysis - iPCA reduction, the step by step process
thresh = 95 # amount of variance to capture in %
# Run the principal component analysis
#First PCA####
#
pca.df <- prcomp(df, center = T, scale = T)
sdev <- pca.df$sdev
evector <- pca.df$rotation
evector
evalue.df <- get_eigenvalue(pca.df)
evalue <- evalue.df$eigenvalue
cum.var <- evalue.df$cumulative.variance.percent
loadings <- as.data.frame((evector*evalue) / (sdev)) # calculate loadings from Jensen
loadings$lf <- rowSums(abs(loadings))# sum up the absolute value of the loadings for the loading factors
# sort by loadings factors
loadings <- loadings[order(-loadings$lf), ]
# add cumulative variance to loadings
loadings$cumVar <- cum.var
loadings
#add covariate names to loadings
loadings$covNames <- rownames(loadings)
loadings
print(loadings[, c("covNames","cumVar")])
# figure out how many PCs capture ~(thresh)% of variance
# the threshold is the amount of variance that we want to capture, in this example it is set to 95 so the idea is that we have to select the covariates that capture a cumulative variance equal to but no less than our threshold.
idealthresh = thresh+1
remove <- which(loadings$cumVar > idealthresh)
loadingsReduced <- loadings[-c(remove:(length(loadings))),]
tokeep <- rownames(loadingsReduced)
df2 <- subset(df, select=tokeep)
df.original <- df
##Second PCA####
# run through the pca again
df <- df2
pca.df <- prcomp(df, center = T, scale = T)
#summary(pca.df)
sdev <- pca.df$sdev
evector <- pca.df$rotation
evector
evalue.df <- get_eigenvalue(pca.df)
evalue <- evalue.df$eigenvalue
cum.var <- evalue.df$cumulative.variance.percent
loadings <- as.data.frame((evector*evalue) / (sdev)) # calculate loadings from Jensen
loadings$lf <- rowSums(abs(loadings))# sum up the absolute value of the loadings for the loading factors
# sort by loadings factors
loadings <- loadings[order(-loadings$lf), ]
# add cumulative variance to loadings
loadings$cumVar <- cum.var
loadings
#add covariate names to loadings
loadings$covNames <- rownames(loadings)
loadings
print(loadings[, c("covNames","cumVar")])
# figure out how many PCs capture ~(thresh)% of variance
# the threshold is the amount of variance that we want to capture, in this example it is set to 95 so the idea is that we have to select the covariates that capture a cumulative variance equal to but no less than our threshold.
idealthresh = thresh+1
remove <- which(loadings$cumVar > idealthresh)
loadingsReduced <- loadings[-c(remove:(length(loadings))),]
tokeep <- rownames(loadingsReduced)
df2 <- subset(df, select=tokeep)
#Third PCA####
# run through the pca again
df <- df2
pca.df <- prcomp(df, center = T, scale = T)
#summary(pca.df)
sdev <- pca.df$sdev
evector <- pca.df$rotation
evector
evalue.df <- get_eigenvalue(pca.df)
evalue <- evalue.df$eigenvalue
cum.var <- evalue.df$cumulative.variance.percent
loadings <- as.data.frame((evector*evalue) / (sdev)) # calculate loadings from Jensen
loadings$lf <- rowSums(abs(loadings))# sum up the absolute value of the loadings for the loading factors
# sort by loadings factors
loadings <- loadings[order(-loadings$lf), ]
# add cumulative variance to loadings
loadings$cumVar <- cum.var
loadings
#add covariate names to loadings
loadings$covNames <- rownames(loadings)
loadings
print(loadings[, c("covNames","cumVar")])
# figure out how many PCs capture ~(thresh)% of variance
# the threshold is the amount of variance that we want to capture, in this example it is set to 95 so the idea is that we have to select the covariates that capture a cumulative variance equal to but no less than our threshold.
idealthresh = thresh+1
remove <- which(loadings$cumVar > idealthresh)
loadingsReduced <- loadings[-c(remove:(length(loadings))),]
tokeep <- rownames(loadingsReduced)
df2 <- subset(df, select=tokeep)
#Fourth PCA####
# run through the pca again
df <- df2
pca.df <- prcomp(df, center = T, scale = T)
#summary(pca.df)
sdev <- pca.df$sdev
evector <- pca.df$rotation
evector
evalue.df <- get_eigenvalue(pca.df)
evalue <- evalue.df$eigenvalue
cum.var <- evalue.df$cumulative.variance.percent
loadings <- as.data.frame((evector*evalue) / (sdev)) # calculate loadings from Jensen
loadings$lf <- rowSums(abs(loadings))# sum up the absolute value of the loadings for the loading factors
# sort by loadings factors
loadings <- loadings[order(-loadings$lf), ]
# add cumulative variance to loadings
loadings$cumVar <- cum.var
loadings
#add covariate names to loadings
loadings$covNames <- rownames(loadings)
loadings
print(loadings[, c("covNames","cumVar")])
# stop here because we have reached our threshold
# What you want to see is that the last covariate adds up to 100% of the cumulative variance, and the next to last covariate to be <= your threshold. In the test data I used It was done in 4 iterations. Often the last iteration, is a check to see if you've met your threshold, and no further reduction is done.
#
##################################################################