-
Notifications
You must be signed in to change notification settings - Fork 0
/
plspmPredict.R
260 lines (222 loc) · 10.4 KB
/
plspmPredict.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
##########################################################################################
# plspmPredict.R
# Description: This function predicts PLS-PM latent and measurement variables from
# a 'plspm' object ('plspm' R-package)
# This is based on the publication:
# Shmueli, G., Ray, S., Estrada, J. M., & Chatla, S. (n.d.). The Elephant in the Room:
# Evaluating the Predictive Performance of Partial Least Squares (PLS) Path Models (2015).
# SSRN Electronic Journal SSRN Journal.
#
# The scriptis based on the following code:
# https://github.com/ISS-Analytics/pls-predict/blob/master/lib/PLSpredict.R
# in order to work directly with an plspm object and to allow for raster data predicitons
#
# data input:
# -----------
# - pls: An plspm object from the plspm package
# - dat: dataframe or Raster Stack with the model predictors
#
# @author: Javier Lopatin
#
##########################################################################################
plspmPredict <- function(pls, dat)
{
if (class(pls) != "plspm")
stop("\n'plspmPredict()' requires a 'plspm' object")
if (all(class(dat) != "data.frame" && class(dat) != "RasterStack" && class(dat) != "RasterBrick"))
stop("\n'plspmPredict()' requires a 'data.frame', 'RasterStack', or 'RasterBrick' object")
# Determine method
if (class (dat) == "data.frame") method <- 'dat'
if (class (dat) == 'RasterBrick') method <- 'rst'
if (class (dat) == 'RasterStack') method <- 'rst'
# =======================================================
# inputs setting
# =======================================================
# from plspm object
ltVariables <- pls$model$gen$lvs_names # latent variables
mmVariables <- pls$model$gen$mvs_names # measurement variables
path_coef <- pls$path_coefs # path coefficients
# Extract and Normalize the measurements for the model
normDataTrain <- scale(pls$data[, mmVariables], TRUE, TRUE)
# Extract Mean and Standard Deviation of measurements for future prediction
meanData <- attr(normDataTrain, "scaled:center")
sdData <- attr(normDataTrain, "scaled:scale")
# =======================================================
# prepare data
# =======================================================
# get relationship matrix
mmMatrix = matrix(nrow = length(mmVariables),
ncol = 2, byrow =TRUE,
dimnames = list(1:length(mmVariables), c("latent","measurement")))
mmMatrix[,'latent'] = as.character(pls$outer_model[,2])
mmMatrix[,'measurement'] = as.character(pls$outer_model[,1])
# Create a matrix of outer_weights
outer_weights <- matrix(data=0,
nrow=length(mmVariables),
ncol=length(ltVariables),
dimnames = list(mmVariables,ltVariables))
#Initialize outer_weights matrix with value 1 for each relationship in the measurement model
for (i in 1:length(ltVariables)) {
outer_weights [mmMatrix[mmMatrix[,"latent"]==ltVariables[i],
"measurement"], ltVariables[i]] = 1
}
# get relationship matrix
eff = which( rowSums(pls$effects[,2:4]) != 0 )
smMatrix = matrix(nrow = length(eff),
ncol = 2, byrow =TRUE,
dimnames = list(1:length(eff), c("source","target")))
for (i in 1:length(eff)){
exVar = strsplit(as.character(pls$effects[eff,1][i]), "[->]")[[1]][1]
enVar = strsplit(as.character(pls$effects[eff,1][i]), "[->]")[[1]][3]
smMatrix[i,1] <- gsub(" ", "", exVar, fixed = TRUE)
smMatrix[i,2] <- gsub(" ", "", enVar, fixed = TRUE)
}
# Create a matrix of outer_loadins
outer_loadings <- matrix(data=0,
nrow=length(mmVariables),
ncol=length(ltVariables),
dimnames = list(mmVariables,ltVariables))
for (i in 1:length(ltVariables)) {
mesVar = mmMatrix[mmMatrix[,"latent"]==ltVariables[i], "measurement"]
idx = which(pls$outer_model$name %in% mesVar)
outer_loadings[mesVar , ltVariables[i]] = pls$outer_model$loading[idx]
}
# Identify Exogenous and Endogenous Variables
exVariables <- unique(smMatrix[,1])
pMeasurements <- NULL
for (i in 1:length(exVariables)){
pMeasurements <- c(pMeasurements,mmMatrix[mmMatrix[,"latent"]==exVariables[i],"measurement"])
}
enVariables <- unique(smMatrix[,2])
resMeasurements <- NULL
for (i in 1:length(enVariables)){
resMeasurements <- c(resMeasurements, mmMatrix[mmMatrix[, "latent"] == enVariables[i],"measurement"])
}
enVariables <- setdiff(enVariables,exVariables)
eMeasurements <- NULL
for (i in 1:length(enVariables)){
eMeasurements <- c(eMeasurements, mmMatrix[mmMatrix[, "latent"] == enVariables[i],"measurement"])
}
# =======================================================
# predict PLS-PM values
# =======================================================
# Extract Measurements needed for Predictions
if (method == 'dat'){
normData <- dat[, pMeasurements]
# Normalize data
for (i in pMeasurements){
normData[,i] <- (normData[,i] - meanData[i])/sdData[i]
}
# Convert dataset to matrix
normData <- data.matrix(normData)
# Add empty columns to normData for the estimated measurements
for (i in 1:length(eMeasurements)){
normData = cbind(normData, seq(0,0,length.out = nrow(normData)))
colnames(normData)[length(colnames(normData))] = eMeasurements[i]
}
# Estimate Factor Scores from Outer Path
fscores <- normData %*% outer_weights
# Estimate Factor Scores from Inner Path and complete Matrix
fscores <- fscores + fscores %*% t(path_coef)
# Predict Measurements with loadings
predictedMeasurements <- fscores %*% t(outer_loadings)
# Denormalize data
for (i in mmVariables){
predictedMeasurements[,i] <- (predictedMeasurements[,i] * sdData[i]) + meanData[i]
}
# Calculating the measurement residuals and accuracies if validation data is provided
if (!is.na(sum( match( eMeasurements, colnames(dat) ) ))){ # if validation data is presented for the endogenous variables
# measurement variables data
mmData = dat[, eMeasurements]
# get residuals
mmResiduals <- dat[,resMeasurements] - predictedMeasurements[,resMeasurements]
# get accuracies of measurement predictions
fit_measurements <- matrix(ncol=length(resMeasurements), nrow = 4, byrow = T)
colnames(fit_measurements) <- resMeasurements
rownames(fit_measurements) <- c('r_square','RMSE','nRMSE','bias')
for (i in 1:length(resMeasurements)){
fit_measurements[1,i] <- cor(dat[,resMeasurements[i]], predictedMeasurements[,resMeasurements[i]], method="pearson")
fit_measurements[2,i] <- sqrt(mean((dat[,resMeasurements[i]] - predictedMeasurements[,resMeasurements[i]])^2))
fit_measurements[3,i] <- (fit_measurements[2,i]/(max(dat[,resMeasurements[i]]) - min(dat[,resMeasurements[i]])))*100
lm = lm(predictedMeasurements[,resMeasurements[i]] ~ dat[,resMeasurements[i]]-1)
fit_measurements[4,i] <- 1-coef(lm)
}
} else {
mmData = dat[,pMeasurements]
residuals = NA
fit_measurements = NA
}
# Prepare return Object
predictResults <- list(mmData = mmData,
mmPredicted = predictedMeasurements[,resMeasurements],
mmResiduals = mmResiduals,
mmfit = fit_measurements,
Scores = fscores)
}
if (method == 'rst'){
names(dat) = pMeasurements
normData <- (dat[[1]] - meanData[1])/sdData[1]
for (i in 2:length(pMeasurements)){
#normData[[i]] <- (dat[[i]] - meanData[i])/sdData[i]
normData <- addLayer(normData, (dat[[i]] - meanData[i])/sdData[i])
}
# Add empty columns to normData for the estimated measurements
for (i in 1:length(eMeasurements)){
normData <- addLayer(normData, normData[[1]])
names(normData)[length(names(normData))] <- eMeasurements[i]
normData[[length(names(normData))]][normData[[length(names(normData))]]] <- 0
}
# Estimate Factor Scores from Outer Path
fscores <- raster(normData)
for (i in 1:length(ltVariables)){
fscores <- addLayer(fscores, calc(normData[[which(outer_weights[,i] == 1)]], fun=prod))
}
# Estimate Factor Scores from Inner Path and complete Matrix
for (i in 1:length(ltVariables)){
idx = which(t(path_coef)[,i] != 0)
n = length(idx)
if (n == 0){
fscores[[i]] <- fscores[[i]]
names(fscores[[i]]) = ltVariables[i]
} else if (n == 1){
fscores[[i]] <- fscores[[i]] + (fscores[[idx]] * t(path_coef)[,i][idx])
names(fscores[[i]]) = ltVariables[i]
} else {
r = raster(fscores)
for (j in 1:n){ r = addLayer(r, (fscores[[idx[j]]] * t(path_coef)[,i][idx][j])) }
fscores[[i]] <- fscores[[i]] + calc(r, fun=sum)
names(fscores[[i]]) = ltVariables[i]
}
}
# Predict Measurements with loadings
predictedMeasurements <- raster(fscores[[1]])
if (length(eMeasurements) == 1){
idx = which(mmVariables == eMeasurements) # measurement
idx2 = which(ltVariables == enVariables) # LV
predictedMeasurements <- addLayer(predictedMeasurements, fscores[[idx2]] * t(outer_loadings)[idx2, idx])
names(predictedMeasurements) = eMeasurements[i]
} else{
idx2 = which(ltVariables == enVariables) # LV
for (i in 1:length(eMeasurements)){ # measurements
idx = which(mmVariables == eMeasurements[i]) # measurement
r = raster(fscores[[1]])
for (k in 1:length(idx)){ r = addLayer(r, (fscores[[idx2]] * t(outer_loadings)[idx2, idx])) }
predictedMeasurements <- addLayer(predictedMeasurements, calc(r, fun=sum))
names(predictedMeasurements)[i] = eMeasurements[i]
}
}
# Denormalize data
for (i in 1:length(eMeasurements)){
idx = which(names(sdData) == eMeasurements[i])
predictedMeasurements[[i]] <- (predictedMeasurements[[i]] * sdData[idx]) + meanData[idx]
}
# Prepare return Object
predictResults <- list(mmPredicted = predictedMeasurements,
Scores = fscores)
}
# =======================================================
# function output
# =======================================================
class(predictResults) <- "plspmPredict"
return(predictResults)
}