-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSS2018_VSMethods.R
256 lines (179 loc) · 9.25 KB
/
SS2018_VSMethods.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
# install.packages
install.packages("dplyr")
# load packages if needed
library(dplyr)
# Selection of preliminary candidates
# 20 candidates
ALS_candidate_predictors <- c('Elev_minimum',
'Elev_maximum',
'Elev_mean',
'Elev_stddev',
'Elev_P10',
'Elev_P20',
'Elev_P30',
'Elev_P40',
'Elev_P50',
'Elev_P60',
'Elev_P70',
'Elev_P80',
'Elev_P90',
'Canopy_relief_ratio',
'FC_1',
'FC_All',
'Percentage_first_returns_above_mean',
'Percentage_first_returns_above_mode',
'Percentage_all_returns_above_mean',
'Percentage_all_returns_above_mode')
# Select those variables for fitting datasets
# Ref
PpPsyl_ALS_Ref <- PpPsyl_Ref[, ALS_candidate_predictors]
# 1m
PpPsyl_ALS_1m_a1 <- PpPsyl_1m_a1[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a2 <- PpPsyl_1m_a2[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a3 <- PpPsyl_1m_a3[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a4 <- PpPsyl_1m_a4[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a5 <- PpPsyl_1m_a5[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a6 <- PpPsyl_1m_a6[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a7 <- PpPsyl_1m_a7[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a8 <- PpPsyl_1m_a8[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a9 <- PpPsyl_1m_a9[, ALS_candidate_predictors]
PpPsyl_ALS_1m_a10 <- PpPsyl_1m_a10[, ALS_candidate_predictors]
# 2m
PpPsyl_ALS_2m_a1 <- PpPsyl_2m_a1[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a2 <- PpPsyl_2m_a2[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a3 <- PpPsyl_2m_a3[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a4 <- PpPsyl_2m_a4[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a5 <- PpPsyl_2m_a5[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a6 <- PpPsyl_2m_a6[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a7 <- PpPsyl_2m_a7[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a8 <- PpPsyl_2m_a8[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a9 <- PpPsyl_2m_a9[, ALS_candidate_predictors]
PpPsyl_ALS_2m_a10 <- PpPsyl_2m_a10[, ALS_candidate_predictors]
# 3m
PpPsyl_ALS_3m_a1 <- PpPsyl_3m_a1[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a2 <- PpPsyl_3m_a2[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a3 <- PpPsyl_3m_a3[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a4 <- PpPsyl_3m_a4[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a5 <- PpPsyl_3m_a5[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a6 <- PpPsyl_3m_a6[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a7 <- PpPsyl_3m_a7[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a8 <- PpPsyl_3m_a8[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a9 <- PpPsyl_3m_a9[, ALS_candidate_predictors]
PpPsyl_ALS_3m_a10 <- PpPsyl_3m_a10[, ALS_candidate_predictors]
# 5m
PpPsyl_ALS_5m_a1 <- PpPsyl_5m_a1[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a2 <- PpPsyl_5m_a2[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a3 <- PpPsyl_5m_a3[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a4 <- PpPsyl_5m_a4[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a5 <- PpPsyl_5m_a5[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a6 <- PpPsyl_5m_a6[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a7 <- PpPsyl_5m_a7[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a8 <- PpPsyl_5m_a8[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a9 <- PpPsyl_5m_a9[, ALS_candidate_predictors]
PpPsyl_ALS_5m_a10 <- PpPsyl_5m_a10[, ALS_candidate_predictors]
# Same for Ppin datasets
###############################################
#Prelimary graphs to explore y versus x variables
# Basal area
# Use PpPsyl_ALS_Ref
par(mar=c(2,2,2,2))
par(mfrow=c(4,5))
for (i in ALS_candidate_predictors){
plot(PpPsyl_ALS_Ref[, i], PpPsyl_FieldData$Basal.area, main = i)
}
# Interact
for (i in ALS_candidate_predictors){
plot(PpPsyl_ALS_5m_a3[, i], PpPsyl_FieldData$Basal.area, main = i)
}
# Dominant height
# Use PpPsyl_ALS_Ref
par(mar=c(2,2,2,2))
par(mfrow=c(4,5))
for (i in ALS_candidate_predictors){
plot(PpPsyl_ALS_Ref[, i], PpPsyl_FieldData$Dominant.height, main = i)
}
# Interact
for (i in ALS_candidate_predictors){
plot(PpPsyl_ALS_1m_a9[, i], PpPsyl_FieldData$Dominant.height, main = i)
}
# Modeling techniques
# Multiple linear regression
# Step procedure to add/drop variables from the model based on the AIC criteria
# Predictor candidates are presented by order
# Three approaches to select predictors, all with the same purpose
# https://stat.ethz.ch/R-manual/R-devel/library/stats/html/step.html
# Letś define our model first using all candidate predictors
lm_Basal_Area_all <- lm (PpPsyl_FieldData$Basal.area ~ . , data = PpPsyl_Ref)
summary(lm_Basal_Area_all)
# Fitting statistics
# R-squared
summary(lm_Basal_Area_all)$adj.r.squared
# RMSE (%)
mean(sqrt(residuals(lm_Basal_Area_all)^2))/mean(PpPsyl_FieldData$Basal.area)
# Iterat over k
par(mar=c(4,7,1,1))
par(mfrow=c(3,2))
# #1
lm_Basal_Area_sep <- step( lm_Basal_Area_all , k=5)
summary(lm_Basal_Area_sep)
# Fitting statistics
# R-squared
summary(lm_Basal_Area_sep)$adj.r.squared
# RMSE (%)
mean(sqrt(residuals(lm_Basal_Area_sep)^2))/mean(PpPsyl_FieldData$Basal.area)
plot(PpPsyl_FieldData$Basal.area, residuals(lm_Basal_Area_sep),
main = "k=5", xlab = "Basal area", ylab ="Model residuals")
abline (0,0)
plot(PpPsyl_FieldData$Basal.area, fitted(lm_Basal_Area_sep),
main = "k=5", xlab = "Basal area", ylab ="Basal area estimates")
abline (1,1)
# #2
lm_Basal_Area_sep <- step( lm_Basal_Area_all , k=9)
summary(lm_Basal_Area_sep)
# Fitting statistics
# R-squared
summary(lm_Basal_Area_sep)$adj.r.squared
# RMSE (%)
mean(sqrt(residuals(lm_Basal_Area_sep)^2))/mean(PpPsyl_FieldData$Basal.area)
plot(PpPsyl_FieldData$Basal.area, residuals(lm_Basal_Area_sep),
main = "k=9", xlab = "Basal area", ylab ="Model residuals")
abline (0,0)
plot(PpPsyl_FieldData$Basal.area, fitted(lm_Basal_Area_sep),
main = "k=9", xlab = "Basal area", ylab ="Basal area estimates")
abline (1,1)
# par(mar=c(4,7,1,1))
# par(mfrow=c(1,1))
# plot(PpPsyl_ALS_Ref$Percentage_first_returns_above_mean,
# PpPsyl_ALS_Ref$Percentage_all_returns_above_mean)
# cor(PpPsyl_ALS_Ref$Percentage_first_returns_above_mean,
# PpPsyl_ALS_Ref$Percentage_all_returns_above_mean)
# par(mar=c(4,7,1,1))
# par(mfrow=c(1,1))
# plot(PpPsyl_ALS_Ref$Elev_mean,
# PpPsyl_ALS_Ref$Elev_P40)
# cor(PpPsyl_ALS_Ref$Elev_mean,
# PpPsyl_ALS_Ref$Elev_P40)
# #3
lm_Basal_Area_sep <- step( lm_Basal_Area_all , k=10)
summary(lm_Basal_Area_sep)
# Fitting statistics
# R-squared
summary(lm_Basal_Area_sep)$adj.r.squared
# RMSE (%)
mean(sqrt(residuals(lm_Basal_Area_sep)^2))/mean(PpPsyl_FieldData$Basal.area)
plot(PpPsyl_FieldData$Basal.area, residuals(lm_Basal_Area_sep),
main = "k=10", xlab = "Basal area", ylab ="Model residuals")
abline (0,0)
plot(PpPsyl_FieldData$Basal.area, fitted(lm_Basal_Area_sep),
main = "k=10", xlab = "Basal area", ylab ="Basal area estimates")
abline (1,1)
# Define your model for Volume, Basal area, Dominant height, Mean_ dbh
# Imagine the following scenario:
# Ref_V = f(FC_1, Elev_mean, Elev_P20)
# Ref_G = f(FC_All, Elev_mean, Elev_P70)
# Ref_Ho = f(Elev_P95)
# Ref_dbh = F(Elev_mean, Elev_P70, FC_1)
# SUR
# See script Script_SUR.R
# ALS seminar Dec 2017
# https://www.dropbox.com/sh/oq6f4w9nu5ipz4b/AABlmvqThf71V3wM2Kmj5bNva?dl=0