-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path08_uncertainty_the_wrong_way_by_yanai.R
544 lines (413 loc) · 15.5 KB
/
08_uncertainty_the_wrong_way_by_yanai.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# this copy of the error script assigns error for each tree
# yanai et al. 2010 says NO
require(plyr)
require(dplyr)
require(tidyverse)
df3 <- read.csv("neon_dbh_test.csv")
##### pull in data file
unique(df3$jenkins_model)
df3 %>%
select(plotid, jenkins_model, individualid, dbh16, dbh17, siteid) -> bob
#pull out empties
bob <- na.omit(bob, cols = c("dbh16"))
bob <- data.frame(bob)
bob <- na.omit(bob, cols = "jenkins_model")
bob <- na.omit(bob, cols = "plotid")
bob <-droplevels(bob)
table(count(bob$plotid), bob$siteid)
bob %>%
group_by(siteid) %>%
summarize(n_unique = n_distinct(plotid)) -
CVI$ID[ELEV=="low" & VEG=="canopy" & PLOT == 3] <-"LC3"
attach(bob)
bob$plot.count[bob$siteid == "BART"] <- 20
bob$plot.count[bob$siteid == "DELA"] <- 19
bob$plot.count[bob$siteid == "GRSM"] <- 19
bob$plot.count[bob$siteid == "HARV"] <- 20
bob$plot.count[bob$siteid == "LENO"] <- 10
bob$plot.count[bob$siteid == "ORNL"] <- 20
bob$plot.count[bob$siteid == "OSBS"] <- 20
bob$plot.count[bob$siteid == "SCBI"] <- 20
bob$plot.count[bob$siteid == "SERC"] <- 20
bob$plot.count[bob$siteid == "TALL"] <- 20
bob$plot.count[bob$siteid == "TREE"] <- 20
bob$plot.count[bob$siteid == "UNDE"] <- 17
# bob$lndbh16 <- log(bob$dbh16)
# bob$lndbh17 <- log(bob$dbh17)
# ###########################################
# ###########################################
# ###########################################
# ###########################################
# ###########################################
bob %>%
filter(siteid == "HARV") -> jeff
mc.biomass <- function(df){
# df <- jeff
plot.name <- unique(df$plotid)
# for ease
hw.check <- any(df$jenkins_model == "aspen/alder/cottonwood/willow")
##### HARDWOOD ONE aspen/alder/cottonwood/willow
if(hw.check == FALSE){hw.growth <- matrix(0, 1, 1000)}
if(hw.check == TRUE){
# parameters from jenkins
B0 = -2.2094
B1 = 2.3867
rse = 0.507441
cf <- rse^2/2
# filter
df %>%
filter(jenkins_model == "aspen/alder/cottonwood/willow") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
hw.growth <- exp(biomass.2017) - exp(biomass.2016)
hw.growth[hw.growth < 0] <- 0
}
##### soft maple check
mb.check <- any(df$jenkins_model == "soft maple/birch")
##### Soft maple/birch model
#############################
if(mb.check == FALSE){mb.growth <- matrix(0, 1, 1000)}
if(mb.check == TRUE){
# parameters from jenkins –1.9123 2.3651 316 66 0.491685 0.958
B0 = -1.9123
B1 = 2.3651
rse = 0.491685
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "soft maple/birch") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
mb.growth <- exp(biomass.2017) - exp(biomass.2016)
mb.growth[mb.growth < 0] <- 0
}
#########################Mixed hardwood
##### mixed hardwood model
mh.check <- any(df$jenkins_model == "mixed hardwood")
##### Soft maple/birch model
#############################
if(mh.check == FALSE){mh.growth <- matrix(0, 1, 1000)}
if(mh.check == TRUE){
# parameters from jenkins –2.4800 2.4835 289 56 0.360458 0.980
B0 = -2.4800
B1 = 2.4835
rse = 0.360458
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "mixed hardwood") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
mh.growth <- exp(biomass.2017) - exp(biomass.2016)
mh.growth[mh.growth < 0] <- 0
}
####### hard maple/oak/hickory/beech
hm.check <- any(df$jenkins_model == "hard maple/oak/hickory/beech")
##### hard maple/oak/hickory/beech
#############################
if(hm.check == FALSE){hm.growth <- matrix(0, 1, 1000)}
if(hm.check == TRUE){
# parameters from jenkins –2.0127 2.4342 485 73 0.236483 0.988
B0 = -2.0127
B1 = 2.4342
rse = 0.236483
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "hard maple/oak/hickory/beech") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
hm.growth <- exp(biomass.2017) - exp(biomass.2016)
hm.growth[hm.growth < 0] <- 0
}
#Softwood Cedar/larch –2.0336 2.2592 196 250 0.294574 0.981
####### softwood cedar/larch
cl.check <- any(df$jenkins_model == "softwood cedar/larch")
##### softwood cedar/larch model
#############################
if(cl.check == FALSE){cl.growth <- matrix(0, 1, 1000)}
if(cl.check == TRUE){
# parameters from jenkins –2.0336 2.2592 196 250 0.294574 0.981
B0 = -2.0336
B1 = 2.2592
rse = 0.294574
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "softwood cedar/larch") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
cl.growth <- exp(biomass.2017) - exp(biomass.2016)
cl.growth[df.growth < 0] <- 0
}
#Douglas-fir –2.2304 2.4435 165 210 0.218712 0.992
####### douglas-fir
df.check <- any(df$jenkins_model == "douglas-fir")
##### douglas-fir
#############################
if(df.check == FALSE){df.growth <- matrix(0, 1, 1000)}
if(df.check == TRUE){
# parameters from jenkins –2.2304 2.4435 165 210 0.218712 0.992
B0 = -2.2304
B1 = 2.4435
rse = 0.218712
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "douglas-fir") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
df.growth <- exp(biomass.2017) - exp(biomass.2016)
df.growth[df.growth < 0] <- 0
}
##### fir/hemlock check
fh.check <- any(df$jenkins_model == "fir/hemlock")
#####FIR HEMLOCK MODEL
##############################################
if(fh.check == FALSE){fh.growth <- matrix(0, 1, 1000)}
if(fh.check == TRUE){
# parameters from jenkins –2.5384 2.4814 395 230 0.182329
B0 = -2.5384
B1 = 2.4814
rse = 0.182329
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "fir/hemlock") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
fh.growth <- exp(biomass.2017) - exp(biomass.2016)
fh.growth[fh.growth < 0] <- 0
}
#Pine –2.5356 2.4349 331 180 0.253781 0.987
##### pine check
pine.check <- any(df$jenkins_model == "pine")
##### pine model
##############################################
if(pine.check == FALSE){pine.growth <- matrix(0, 1, 1000)}
if(pine.check == TRUE){
# parameters from jenkins –2.5356 2.4349 331 180 0.253781 0.987
B0 = -2.5356
B1 = 2.4349
rse = 0.253781
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "pine") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
pine.growth <- exp(biomass.2017) - exp(biomass.2016)
pine.growth[pine.growth < 0] <- 0
}
#Spruce –2.0773 2.3323 212 250 0.250424 0.988
##### spruce
spruce.check <- any(df$jenkins_model == "spruce")
##### SPRUCE MODEL
##############################################
if(spruce.check == FALSE){ spruce.growth <- matrix(0, 1, 1000)}
if(spruce.check == TRUE){
# parameters from jenkins –2.0773 2.3323 212 250 0.250424 0.988
B0 = -2.0773
B1 = 2.3323
rse = 0.250424
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "spruce") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
spruce.growth <- exp(biomass.2017) - exp(biomass.2016)
spruce.growth[spruce.growth < 0] <- 0
}
#Woodland|| Juniper/oak/mesquite –0.7152 1.7029 61 78 0.384331 0.938
##### juniper/oak/mesquite check
w.check <- any(df$jenkins_model == "juniper/oak/mesquite")
##### juniper/oak/mesquite model
##############################################
if(w.check == FALSE){ w.growth <- matrix(0, 1, 1000)}
if(w.check == TRUE){
# parameters from jenkins –0.7152 1.7029 61 78 0.384331 0.938
B0 = -0.7152
B1 = 1.7029
rse = 0.384331
cf = rse^2/2
# filter
df %>%
filter(jenkins_model == "juniper/oak/mesquite") -> x
## define DBH matrix ##
DBH.2016 = matrix(0, length(x$dbh16), 1000)
DBH.2017 = matrix(0, length(x$dbh17), 1000)
## make 1000 estimates of DBH [cm] for each tree ##
for(i in 1:length(x$dbh16)){
DBH.2016[i,] = (rnorm(1000, 0, 0.05)) + x[i,4] # 0.05 is standard msmt error in cm for dbh repeats
DBH.2017[i,] = (rnorm(1000, 0, 0.05)) + x[i,5]
}
## matrices for biomass variables ##
biomass.2016 = matrix(0,length(x[,1]),1000)
biomass.2017 = matrix(0,length(x[,1]),1000)
# biomass
for(i in 1:1000){
biomass.2016[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2016[,i]))
biomass.2017[,i] = (rnorm(1, 0, rse) ) + (B0 + B1 * log(DBH.2017[,i]))
}
#
w.growth <- exp(biomass.2017) - exp(biomass.2016)
w.growth[w.growth < 0] <- 0
}
#######
npp.mat <- rbind(hw.growth, mb.growth, mh.growth, hm.growth, cl.growth, df.growth, fh.growth, pine.growth, spruce.growth, w.growth)
npp.sums <- colSums(npp.mat)
#### plot corrction
# plot.no.cf <- unique(df$plot.count)
#
# npp.sums <- npp.sums/plot.no.cf
## change to per hectare
npp.sums <- npp.sums * 12.5
# change to Mg per hectare
npp.sums <- npp.sums * 0.001
#### make output
npp.output <- data.frame(matrix(ncol = 2, nrow = length(npp.sums)))
jj <- c("plotid", "npp_mc")
colnames(npp.output) <- jj
npp.output$plotid <- plot.name
npp.output$npp_mc <- npp.sums
#output procedure for variables
outputname <- paste(plot.name, "mc", sep = "_")
filename <- paste(outputname, ".csv", sep="")
output_directory <- "./summary/mc_simulations"
utils::write.csv(npp.output, file.path(output_directory, filename))
}