forked from ISRICWorldSoil/GSIF_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspc.R
More file actions
91 lines (76 loc) · 2.92 KB
/
Copy pathspc.R
File metadata and controls
91 lines (76 loc) · 2.92 KB
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
# Purpose : Derive Spatial Predictive Components for a list of grids;
# Maintainer : Tomislav Hengl (tom.hengl@wur.nl);
# Contributions : ;
# Status : pre-alpha
# Note : Not recommended for large grids;
setMethod("spc", signature(obj = "SpatialPixelsDataFrame", formulaString = "formula"), function(obj, formulaString, scale. = TRUE, silent = FALSE, ...){
## formula string:
if(missing(formulaString)) {
formulaString <- as.formula(paste("~", paste(out@layernames, collapse="+")))
}
vars = all.vars(formulaString)
if(length(vars)< 2){
stop("At least two covarites required to run Principal Component Analysis")
}
obj@data <- obj@data[,vars]
## print warning:
if(silent==FALSE){
if(nrow(obj)>10e6){
warning('Operation not recommended for large grids', immediate. = TRUE)
}}
## convert every factor to indicators:
for(j in 1:length(vars)){
if(is.factor(obj@data[,vars[j]])){
# remove classes without pixels:
obj@data[,vars[j]] <- as.factor(paste(obj@data[,vars[j]]))
ln <- levels(obj@data[,vars[j]])
for(k in 1:length(ln)){
vn <- paste(vars[j], k, sep="_")
obj@data[,vn] <- ifelse(obj@data[,vars[j]]==ln[k], 1, 0)
}
message(paste("Converting", vars[j], "to indicators..."))
}
}
varsn = names(obj)[which(!sapply(obj@data, is.factor))]
obj@data <- obj@data[,varsn]
## filter the missing values:
if(scale. == TRUE){
x <- scale(obj@data)
x[is.na(x)] <- 0
x <- as.data.frame(x)
sd.l <- lapply(x, sd)
x0 <- sd.l==0
if(any(x0)){
message(paste("Columns with zero variance removed:", names(x)[which(x0)]), immediate. = TRUE)
formulaString.f = as.formula(paste("~", paste(varsn[-which(x0)], collapse="+")))
## principal component analysis:
pcs <- prcomp(formula=formulaString.f, x)
} else {
formulaString = as.formula(paste("~", paste(varsn, collapse="+")))
pcs <- prcomp(formula=formulaString, x)
}
} else {
formulaString = as.formula(paste("~", paste(varsn, collapse="+")))
pcs <- prcomp(formula=formulaString, obj@data)
}
## copy values:
obj@data <- as.data.frame(pcs$x)
proj4string(obj) <- obj@proj4string
if(silent==FALSE){
message(paste("Converting covariates to principal components..."))
summary(pcs)
}
pcs <- new("SpatialComponents", predicted = obj, pca = pcs[-which(names(pcs)=="x")])
return(pcs)
})
setMethod("spc", signature(obj = "list", formulaString = "list"), function(obj, formulaString, scale. = TRUE, silent = FALSE, ...){
if(!length(obj)==length(formulaString)){
stop("'obj' and 'formulaString' lists of same size expected")
}
pcs.l <- list(NULL)
for(l in 1:length(obj)){
pcs.l[[l]] <- spc(obj = obj[[l]], formulaString = formulaString[[l]], ...)
}
return(pcs.l)
})
# end of script;