-
Notifications
You must be signed in to change notification settings - Fork 0
/
Council_Soil_Chamber_Stats_Working.Rmd
1895 lines (1341 loc) · 66 KB
/
Council_Soil_Chamber_Stats_Working.Rmd
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Council Soil Chamber Analysis - STATS"#Breaking up the figures and data analysis codes
#time is in AK daylight time** NEE/RECO/GPP are in gC/m2/s; FCO2 and FCH4 are in gC/m2/s, flux_CO2 is in umol/m2/s; flux_CH4 is in nano mol/m2/s
output: html_document
date: "2024-11-18"
---
#Working code - very far from finalized / still a mess
#Note that for comparison purposes, both instruments
were used to measure chamber fluxes on July 18, 2018 --> remove potential measurement duplicates from this date?
#measure the Net Ecosystem Exchange (NEE) with the transparent chamber during the day (when photosynthesis is occurring) and the Ecosystem Respiration (Reco) with the opaque chamber during the night (when only respiration is happening), then subtract the Reco value from the NEE value to get GPP: GPP = NEE (transparent chamber) - Reco (opaque chamber)
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
#Load libraries
```{r, include=FALSE}
rm(list= ls())
library(data.table)
library(ggplot2)
library(cowplot)
library(openair)
library(plotrix)
library(signal)
library(svMisc)
library(zoo)
library(stringr)
library(plyr)
library(viridis)
library(lubridate)
library(tidyverse)
library(gridExtra)
library(plotly)
library(RColorBrewer)
library(pracma)
library(dplyr)
library(openair)
library(nlme)
library(lme4)
```
#Load filtered and merged df of soil chamber fluxes, moisture, temp (I upload multiples but only using df_NEE_RECO2 and df_NEE_RECO2_GPP for analysis below)
```{r}
#filtered for p<0.05; units umol/m2/s or nmol/m2/s
df_soilchambers_filtered = fread('C:/Users/kkent/Documents/Council Data/Soil Chambers_Council/council_filtered_soil_chamber_fluxes_2017to2019.csv')
#fluxes and moisture/temp df merged; FCO2 in units g/m2/s
df_fulljoin = fread('C:/Users/kkent/Documents/Council Data/Soil Chambers_Council/council_fulljoin_soilchamber_fluxes_moisttemp_2017to2019.csv')
# *****************Use these two, above are just extra if needed for looking at*********************
#used transparent and opaque chambers to identify NEE and RECO, then merged back together
df_NEE_RECO2 = fread('C:/Users/kkent/Documents/Council Data/Soil Chambers_Council/council_fulljoin_soilchamber_fluxes_moisttemp_2017to2019.csv')
#calculated GPP (NEE - Reco)
df_NEE_RECO2_GPP = fread('C:/Users/kkent/Documents/Council Data/Soil Chambers_Council/council_NEE_RECO2_GPP_2017to2019.csv')
```
#Re-shape df
```{r}
library(tidyr)
#Remove the NAs from inundation
library(dplyr)
df_NEE_RECO2_GPP<- df_NEE_RECO2_GPP %>%
filter(!is.na(inundated))
# Reshape the dataframe to long format
df_long <- df_NEE_RECO2_GPP %>%
select(plot_ID, plot_type, landscape_position, measurement_date, FCH4, NEE, RECO, GPP, inundated, soil_temp_10_cm, soil_temp_15_cm) %>%
pivot_longer(cols = c(NEE, RECO, GPP),
names_to = "flux_type",
values_to = "flux_value")
```
#Filter df by landscape position and flux type (GPP, NEE, RECO)
####Create new df for each plot type for analysis
```{r}
# library(dplyr)
#Filter the dataframe for plot_ID = "EC" "MW" and "BGC", and by flux type to create df for diff analysis options
#EC - eddy covar tower plot types
df_EC <- df_NEE_RECO2_GPP %>%
filter(plot_type == "EC")
#EC - eddy covar tower plot types; just another way to subset - doing it this way saves you from having to make df_EC into d_long format, but below way allows you to choose variables of interest and simplify the df
# df_EC2 <- df_long %>%
# filter(plot_type == "EC")
#MW - moisture warming plot types
df_MW <- df_NEE_RECO2_GPP %>%
filter(plot_type == "MW")
#BGC - biogeochem plot types
df_BGC <- df_NEE_RECO2_GPP %>%
filter(plot_type == "BGC")
#GPP
df_GPP <-df_long %>%
filter(flux_type == "GPP")
#NEE
df_NEE <-df_long %>%
filter(flux_type == "NEE")
#RECO
df_RECO <-df_long %>%
filter(flux_type == "RECO")
#Reshape the dataframe to long format and choose variables of interest
#EC
df_EClong <- df_EC %>%
#select(plot_ID, plot_type, landscape_position, measurement_date, NEE, RECO, GPP) %>% *choosing variables of interest
select(plot_ID, plot_type, landscape_position, measurement_date, FCH4, NEE, RECO, GPP, inundated, soil_temp_10_cm, soil_temp_15_cm) %>% #choosing all variables or just subset from the d_long data
pivot_longer(cols = c(NEE, RECO, GPP),
names_to = "flux_type",
values_to = "flux_value")
#MW
df_MWlong <- df_MW %>%
#select(plot_ID, plot_type, landscape_position, measurement_date, NEE, RECO, GPP) %>%
select(plot_ID, plot_type, landscape_position, measurement_date, FCH4, NEE, RECO, GPP, inundated, soil_temp_10_cm, soil_temp_15_cm) %>% #choosing all variables or just subset from the d_long data
pivot_longer(cols = c(NEE, RECO, GPP),
names_to = "flux_type",
values_to = "flux_value")
#BGC
df_BGClong <- df_BGC %>%
#select(plot_ID, plot_type, landscape_position, measurement_date, NEE, RECO, GPP) %>%
select(plot_ID, plot_type, landscape_position, measurement_date, FCH4, NEE, RECO, GPP, inundated, soil_temp_10_cm, soil_temp_15_cm) %>% #choosing all variables or just subset from the d_long data
pivot_longer(cols = c(NEE, RECO, GPP),
names_to = "flux_type",
values_to = "flux_value")
#Re-arrange by flux type (NEE, GPP, RECO) so you can analyze more easily
# Sort the dataframe by the flux_type column
df_EClong <- df_EClong %>% arrange(flux_type)
df_MWlong <- df_MWlong %>% arrange(flux_type)
df_BGClong <- df_BGClong %>% arrange(flux_type)
```
#Variance spread in data
####Spread in dataset - NEE
```{r}
library(dplyr)
#looking at spread in the datasets
# Boxplots for categorical predictors
ggplot(df_NEE, aes(x = landscape_position, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Landscape Position")
#looks good, very little spread/difference here
ggplot(df_NEE, aes(x = inundated, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Inundation")
#also looks good, very little spread here
ggplot(df_NEE, aes(x = plot_type, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Plot Type")
#also looks good, very little spread
# Scatterplot for continuous predictor
ggplot(df_NEE, aes(x = soil_temp_10_cm, y = flux_value)) +
geom_point() +
theme_minimal() +
labs(title = "Flux Value vs Soil Temperature (10 cm)")
#also very little spread
```
####Spread in Variance among diff variables - NEE
```{r}
# Calculate variance within groups
#exclude NA --> edited to be done beforehand in code chunks above
# sum(is.na(df_NEE$flux_value)) #flux val has 0 NAs; inundated has 2
# df_NEE.c1 <- df_NEE[complete.cases(df_NEE[, c("inundated")]), ]
#Flux and landscape pos
df_variance_landpos <- df_NEE %>%
group_by(landscape_position) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_landpos)
# Variance plot - flux and landscape pos
ggplot(df_variance_landpos, aes(x = landscape_position, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Landscape Position",
y = "Variance")
#higher variance in upland landscape position than in slope and lowland*
#Flux and plot type
df_variance_plottype <- df_NEE %>%
group_by(plot_type) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_plottype)
# Variance plot - flux and plot type
ggplot(df_variance_plottype, aes(x = plot_type, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Plot Type",
y = "Variance")
#higher variance in BGC
#Flux and inundated
#dropped NAs in inundated
df_variance_inundated <- df_NEE.c1 %>%
group_by(inundated) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_inundated)
# Variance plot - flux and inundated
ggplot(df_variance_inundated, aes(x = inundated, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Inundation",
y = "Variance")
#higher variance in inundated plots
# Shapiro-Wilk test for normality
shapiro_test <- shapiro.test(df_NEE$flux_value)
print(shapiro_test) #lower than 0.05, so does deviate from normality
# Q-Q plot
qqnorm(df_NEE$flux_value)
qqline(df_NEE$flux_value, col = "red")
#homogeneity of variance - levene's test
# p > 0.05: Variances are homogenous (no significant difference between variances).
# p ≤ 0.05: Variances are not homogenous.
library(car)
leveneTest(flux_value ~ landscape_position, data = df_NEE)
#p=0.43, homogeneity of var is ok
```
####Spread in dataset - GPP
```{r}
library(dplyr)
#looking at spread in the datasets
# Boxplots for categorical predictors
ggplot(df_GPP, aes(x = landscape_position, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Landscape Position")
#looks good, very little spread/difference here
ggplot(df_GPP, aes(x = inundated, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Inundation")
#also looks good, very little spread here
ggplot(df_GPP, aes(x = plot_type, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Plot Type")
#also looks good, very little spread
# Scatterplot for continuous predictor
ggplot(df_GPP, aes(x = soil_temp_10_cm, y = flux_value)) +
geom_point() +
theme_minimal() +
labs(title = "Flux Value vs Soil Temperature (10 cm)")
#also very little spread
```
####Spread in Variance among diff variables - GPP
```{r}
# Calculate variance within groups
#exclude NA
sum(is.na(df_GPP$flux_value)) #flux val has 3 NAs
sum(is.na(df_GPP$inundated)) #inundated has 2 NAs
df_GPP.c1 <- df_GPP[complete.cases(df_GPP[, c("inundated")]), ]
df_GPP.c2 <- df_GPP[complete.cases(df_GPP[, c("flux_type")]), ]
#Flux and landscape pos
df_variance_landpos <- df_GPP %>%
group_by(landscape_position) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_landpos)
# Variance plot - flux and landscape pos
ggplot(df_variance_landpos, aes(x = landscape_position, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Landscape Position",
y = "Variance")
#higher variance in upland landscape position than in slope and lowland*
#Flux and plot type
df_variance_plottype <- df_GPP %>%
group_by(plot_type) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_plottype)
# Variance plot - flux and plot type
ggplot(df_variance_plottype, aes(x = plot_type, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Plot Type",
y = "Variance")
#higher variance in BGC
#Flux and inundated
df_variance_inundated <- df_GPP %>%
group_by(inundated) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_inundated)
# Variance plot - flux and inundated
ggplot(df_variance_inundated, aes(x = inundated, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Inundation",
y = "Variance")
#higher variance in not inundated plots
# Shapiro-Wilk test for normality
shapiro_test <- shapiro.test(df_GPP$flux_value)
print(shapiro_test) #lower than 0.05, so does deviate from normality
# Q-Q plot
qqnorm(df_GPP$flux_value)
qqline(df_GPP$flux_value, col = "red")
#homogeneity of variance - levene's test
# p > 0.05: Variances are homogenous (no significant difference between variances).
# p ≤ 0.05: Variances are not homogenous.
library(car)
leveneTest(flux_value ~ landscape_position, data = df_GPP)
#p=0.68, homogeneity of var is ok
```
####Spread in dataset - RECO
```{r}
library(dplyr)
#looking at spread in the datasets
# Boxplots for categorical predictors
ggplot(df_RECO, aes(x = landscape_position, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Landscape Position")
#looks good, very little spread/difference here
ggplot(df_RECO, aes(x = inundated, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Inundation")
#also looks good, very little spread here
ggplot(df_RECO, aes(x = plot_type, y = flux_value)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Flux Value by Plot Type")
#also looks good, very little spread
# Scatterplot for continuous predictor
ggplot(df_RECO, aes(x = soil_temp_10_cm, y = flux_value)) +
geom_point() +
theme_minimal() +
labs(title = "Flux Value vs Soil Temperature (10 cm)")
#also very little spread
```
####Spread in Variance among diff variables - RECO
```{r}
# Calculate variance within groups
#exclude NA
sum(is.na(df_RECO$flux_value)) #flux val has 3 NAs
sum(is.na(df_RECO$inundated)) #inundated has 2 NAs
df_RECO.c1 <- df_RECO[complete.cases(df_RECO[, c("inundated")]), ]
df_RECO.c2 <- df_RECO[complete.cases(df_RECO[, c("flux_type")]), ]
#Flux and landscape pos
df_variance_landpos <- df_RECO %>%
group_by(landscape_position) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_landpos)
# Variance plot - flux and landscape pos
ggplot(df_variance_landpos, aes(x = landscape_position, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Landscape Position",
y = "Variance")
#higher variance in upland landscape position than in slope and lowland*
#Flux and plot type
df_variance_plottype <- df_RECO %>%
group_by(plot_type) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_plottype)
# Variance plot - flux and plot type
ggplot(df_variance_plottype, aes(x = plot_type, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Plot Type",
y = "Variance")
#all very similar
#Flux and inundated
df_variance_inundated <- df_RECO %>%
group_by(inundated) %>%
summarize(variance = var(flux_value, na.rm = TRUE))
print(df_variance_inundated)
# Variance plot - flux and inundated
ggplot(df_variance_inundated, aes(x = inundated, y = variance)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Variance of Flux Value by Inundation",
y = "Variance")
#higher variance in not inundated plots
# Shapiro-Wilk test for normality
shapiro_test <- shapiro.test(df_RECO$flux_value)
print(shapiro_test) #lower than 0.05, so does deviate from normality
# Q-Q plot
qqnorm(df_RECO$flux_value)
qqline(df_RECO$flux_value, col = "red") #bows upward at both ends
#homogeneity of variance - levene's test
# p > 0.05: Variances are homogeneous (no significant difference between variances).
# p ≤ 0.05: Variances are not homogeneous.
library(car)
leveneTest(flux_value ~ landscape_position, data = df_RECO)
#p=0.78, homogeneity of var is ok
```
#Testing Models
#NEE
#df_NEE: Testing df_NEE models
```{r}
#Testing parameters in df_NEE as a whole - using Kyle's "fluxes" code as guide
#Make plot_ID, inundated, plot_type, landscape_position as factor so they'll work with gls
df_NEE$plot_ID = factor(df_NEE$plot_ID)
df_NEE$plot_type = factor (df_NEE$plot_type)
df_NEE$landscape_position = factor(df_NEE$landscape_position)
df_NEE$inundated = factor(df_NEE$inundated)
#df_NEE$soil_temp_10_cm = as.numeric (df_NEE$soil_temp_10_cm) #use this in case it reads it in as factor or character
#Tried with both 'ML' and 'REML' - no major diffs in results
library(nlme)
#gls - no random effect
gls.NEE = gls(flux_value ~ plot_type + landscape_position + inundated + soil_temp_10_cm, data = df_NEE, method = 'ML', na.action=na.exclude)
anova(gls.NEE) #this shows no sig diff in flux type among these
#lme - with random effect of plot_ID
lme.NEE <- lme(flux_value ~ plot_type + landscape_position + inundated + soil_temp_10_cm,
random = ~1 | plot_ID,
data = df_NEE, na.action=na.exclude ,
method = 'ML')
anova(lme.NEE) #shows no sig diff in flux types among these
#Comparing the models
anova(gls.NEE, lme.NEE) #shows no diff in model with or without random effect - so random effect not needed here
```
####Multicollinearity - NEE
```{r}
library(car)
vif(gls.NEE) # <2, no impactful multicollinearity
```
####Checking each variable on its own
```{r}
#double checking each var on its own
gls.NEE.landscape = gls(flux_value ~ landscape_position, data = df_NEE, method = 'ML', na.action=na.exclude)
anova(gls.NEE.landscape) #p=0.11, not sig
gls.NEE.plottype = gls(flux_value ~ plot_type, data = df_NEE, method = 'ML', na.action=na.exclude)
anova(gls.NEE.plottype) #p=0.0645, not sig
gls.NEE.inundated = gls(flux_value ~ inundated, data = df_NEE, method = 'ML', na.action=na.exclude)
anova(gls.NEE.inundated) #p=0.32, not sig
gls.NEE.soiltemp = gls(flux_value ~ soil_temp_10_cm, data = df_NEE, method = 'ML', na.action=na.exclude)
anova(gls.NEE.soiltemp) #p=0.28, not sig
```
#### Variance Structre - df_NEE
```{r}
#testing variance structure
#based on inundation
var.NEE.inundated = update(gls.NEE, weights = varIdent(form = ~1|inundated))
anova(var.NEE.inundated) #no diffs among variables
anova(gls.NEE, var.NEE.inundated) #p = <0.04, adding in the variance by inundation improves the model fit (but only slightly lower AIC, lower BIC, and higher L.Ratio)
#based on landscape position
var.NEE.landpos = update(gls.NEE, weights = varIdent(form = ~1|landscape_position))
anova(var.NEE.landpos) #no diffs among variables
anova(gls.NEE, var.NEE.landpos) #p = <0.0001, adding in the variance by inundation significantly improves the model fit (but only slightly lower AIC, lower BIC, and higher LogLik)
#based on plot type
var.NEE.plottype = update(gls.NEE, weights = varIdent(form = ~1|plot_type))
anova(var.NEE.plottype) #now showing sig diffs for landscape pos (p=<0.04), plot type (p=0.03), marginally soil temp (<0.053)
anova(gls.NEE, var.NEE.plottype) #p = <0.0001, adding in the variance by inundation significantly improves the model fit (lower AIC, lower BIC, and higher LogLik)
#based on soil temp --> will not work for this model, "false convergence" --> leaving out, not categorical
# var.NEE.soiltemp = update(gls.NEE, weights = varIdent(form = ~1|soil_temp_10_cm))
# anova(var.NEE.soiltemp) #"false convergence", will not work for this var structure
#now testing which models are best fit
anova(var.NEE.inundated, var.NEE.landpos, var.NEE.plottype) #sig improvement in landpos over inundated
anova(var.NEE.inundated, var.NEE.landpos) #land pos better
anova(var.NEE.inundated,var.NEE.plottype) #plot type better
anova(var.NEE.landpos, var.NEE.plottype) #no sig diff but plottype lower AIC/BIC, higher logLik
```
#### Multiple Var structure - NEE
```{R}
#looking at models with multiple variance structures
#inundation and land pos
library(nlme)
var.NEE.landpos.inun = update(gls.NEE, weights = varComb(varIdent(form = ~ 1| inundated), varIdent(form = ~ 1 | landscape_position)))
anova(var.NEE.landpos.inun) #no sig diff among variables
#inundation and plot type
var.NEE.plottype.inun = update(gls.NEE, weights = varComb(varIdent(form = ~ 1| inundated), varIdent(form = ~ 1 | plot_type)))
anova(var.NEE.plottype.inun) #shows sig diff in landpos (p=0.004), soil temp (p=0.04), and marginally plot type (0.05)
#land pos and plot type
var.NEE.landpos.plottype = update(gls.NEE, weights = varComb(varIdent(form = ~ 1| landscape_position), varIdent(form = ~ 1 | plot_type)))
anova(var.NEE.landpos.plottype) #all variables sig except inundated
anova(var.NEE.landpos.inun,var.NEE.plottype.inun) #no sig diff here, but AIC/BIC lower & logLik higher in plottype.inun
anova(var.NEE.landpos.inun, var.NEE.landpos.plottype, var.NEE.plottype.inun) #appears landpos-plottype is best model of these three
#land pos and plot type and inun
var.NEE.all = update(gls.NEE, weights = varComb(varIdent(form = ~ 1| landscape_position), varIdent(form = ~ 1 | plot_type), varIdent(form = ~1 |inundated)))
anova(var.NEE.all) #all variables sig except inundated
#testing all multiple var models
anova(var.NEE.plottype.inun, var.NEE.all) #NEE.all is better fit, sig
anova(var.NEE.landpos.inun, var.NEE.landpos.plottype, var.NEE.plottype.inun, var.NEE.all) #sig diff, NEE.plottype.inun has slightly lower AIC/BIC, higher logLik --> seems to fit best, though NEE.all very close to being the same as NEE.plottype.inun
#testing multiple var models with single var models
anova(gls.NEE, var.NEE.plottype.inun, var.NEE.inundated, var.NEE.landpos, var.NEE.plottype)
#NEE.plottype.inun has lowest AIC/BIC, highest logLik so it appears to be better model
#looking at fixed effects with selected model of var.NEE.plottype.inun
anova(var.NEE.plottype.inun, type = "marginal") # soil temp (p<0.04) is sig
#refit with REML
NEE.final = update(var.NEE.plottype.inun, method = "REML")
anova(NEE.final) #plottype marginal (p=0.049), landscape pos (p=0.006) and soil temp (p=0.046) are sig
#checking colinearity
gls.NEE2 = gls(flux_value ~ plot_type + landscape_position + inundated + soil_temp_10_cm, data = df_NEE, method = 'REML', na.action=na.exclude)
anova(gls.NEE2)
library(car)
vif(gls.NEE2) #all values <2, so multicolinearity is not a problem here
vif(gls.NEE)
```
####NEE.final model - Plot model residuals and qqplot
```{r}
#plot model residuals (homogeneity of variance)
plot(NEE.final)
#qqplot to verify normality
qqnorm(NEE.final)
#checking how this QQ plot compares to plots created with normally distributed residuals
op <- par(mar = c(2,2,1,1), mfrow = c(5,5))
# create first qq plot using model residuals
# color it red
qqnorm(residuals(NEE.final), xlab = "", ylab = "", main = "",
col = "red")
qqline(residuals(NEE.final))
# now create 24 qq plots using Normal data with sigma(dataset)
for(i in 1:24){
# rnorm() samples from a Normal distribution
d <- rnorm(length(residuals(NEE.final)),
mean = 0, sd = sigma(NEE.final))
qqnorm(d, xlab = "", ylab = "", main = "")
qqline(d)
}
#doesn't look awful, doesn't look great....have Kyle take a look
#comparing data QQplot to a normal QQplot and hist
qqnorm(residuals(NEE.final))
hist(residuals(NEE.final)) #shows a bit of left skew
car::qqPlot(x = residuals(NEE.final)) #shows where residuals breach normal distr
```
#GPP
#df_GPP: Testing df_GPP models
```{r}
#Testing parameters in df_GPP as a whole - using Kyle's "fluxes" code as guide
#Make plot_ID, inundated, plot_type, landscape_position as factor so they'll work with gls
df_GPP$plot_ID = factor(df_GPP$plot_ID)
df_GPP$plot_type = factor (df_GPP$plot_type)
df_GPP$landscape_position = factor(df_GPP$landscape_position)
df_GPP$inundated = factor(df_GPP$inundated)
#df_NEE$soil_temp_10_cm = as.numeric (df_NEE$soil_temp_10_cm)
#need to remove NA's - check for NAs and remove
sum(is.na(df_GPP))
summary(df_GPP$soil_temp_10_cm)
# Omit rows with NA values in specific columns so it works with analyses below
df_GPP.c1 <- df_GPP[complete.cases(df_GPP[, c("inundated")]), ]
df_GPP.c2 <- df_GPP[complete.cases(df_GPP[, c("soil_temp_10_cm")]), ]
df_GPP.c3 <- df_GPP[complete.cases(df_GPP[, c("flux_value")]), ]
sum(is.na(df_GPP.c3$flux_value)) #double check to make sure NAs are gone -> gone
sum(is.infinite(df_GPP.c3$flux_value)) #and infinite values? --> no
#Tried with both 'ML' and 'REML' - no major diffs in results
library(nlme)
#gls - no random effect
gls.GPP = gls(flux_value ~ plot_type + landscape_position + inundated + soil_temp_10_cm, data = df_GPP, method = 'ML', na.action=na.exclude)
anova(gls.GPP) #only soil temp is sig
#lme - with random effect of plot_ID
lme.GPP <- lme(flux_value ~ plot_type + landscape_position + inundated + soil_temp_10_cm,
random = ~1 | plot_ID,
data = df_GPP, na.action=na.exclude ,
method = 'ML')
anova(lme.GPP) #only soil temp is sig
#Comparing the models
anova(gls.GPP, lme.GPP) #shows no diff in model with or without random effect - so random effect not needed here
```
####Multicollinearity - GPP
```{r}
library(car)
vif(gls.GPP) # <2, no impactful multicollinearity
```
####Checking each variable on its own
```{r}
#double checking each var on its own
gls.GPP.landscape = gls(flux_value ~ landscape_position, data = df_GPP, method = 'ML', na.action=na.exclude)
anova(gls.GPP.landscape) #p=0.29, not sig
gls.GPP.plottype = gls(flux_value ~ plot_type, data = df_GPP, method = 'ML', na.action=na.exclude)
anova(gls.GPP.plottype) #p=0.259, not sig
gls.GPP.inundated = gls(flux_value ~ inundated, data = df_GPP, method = 'ML', na.action=na.exclude)
anova(gls.GPP.inundated) #p=0.65, not sig
gls.GPP.soiltemp = gls(flux_value ~ soil_temp_10_cm, data = df_GPP, method = 'ML', na.action=na.exclude)
anova(gls.GPP.soiltemp) #p=0.053, not sig/borderline
```
#****Variance Structure -GPP- inundation and soil temp not working
```{r}
#select variance structure
var.GPP.inundated = update(gls.GPP, weights = varIdent(form = ~1|inundated))
anova(var.GPP.inundated) #soil temp sig, p = 0.03
anova(gls.GPP, var.GPP.inundated) #p=0.03, GPP.inundated slightly better
#based on landscape position
var.GPP.landpos = update(gls.GPP, weights = varIdent(form = ~1|landscape_position))
anova(var.GPP.landpos) #sig diffs for soil temp (<0.02)
anova(gls.GPP, var.GPP.landpos) #p = 0.002, GPP.landpos model slightly better
#based on plot type
var.GPP.plottype = update(gls.GPP, weights = varIdent(form = ~1|plot_type))
anova(var.GPP.plottype) #no sig diffs
anova(gls.GPP, var.GPP.plottype) #p = 0.001, GPP.plottype slightly better
#leaving out soil temp since it is not categorical
ggplot(df_GPP)+
geom_point(aes(soil_temp_10_cm,flux_value))
#now testing which models are best fit
anova(var.GPP.inundated, var.GPP.landpos, var.GPP.plottype) #GPP.landpos slightly better
anova(var.GPP.inundated, var.GPP.landpos)#lanpos slightly better p = 0.006
anova(var.GPP.inundated,var.GPP.plottype) #plottype slightly better, p = 0.03
anova(var.GPP.landpos, var.GPP.plottype) #no sig diff
```
#### Multiple Var structure - GPP
```{r}
#looking at models with multiple variance structures
#inundation and land pos
library(nlme)
var.GPP.landpos.inun = update(gls.GPP, weights = varComb(varIdent(form = ~ 1| inundated), varIdent(form = ~ 1 | landscape_position)))
anova(var.GPP.landpos.inun) #shows sig diff in soil temp (p=0.017)
#inundation and plot type
var.GPP.plottype.inun = update(gls.GPP, weights = varComb(varIdent(form = ~ 1| inundated), varIdent(form = ~ 1 | plot_type)))
anova(var.GPP.plottype.inun) #no sig diffs
#land pos and plot type
var.GPP.landpos.plottype = update(gls.GPP, weights = varComb(varIdent(form = ~ 1| landscape_position), varIdent(form = ~ 1 | plot_type)))
anova(var.GPP.landpos.plottype) #no sig diffs
anova(var.GPP.landpos.inun,var.GPP.plottype.inun) #no sig diff
anova(var.GPP.landpos.inun, var.GPP.landpos.plottype, var.GPP.plottype.inun) #appears landpos-plottype is best model of these three
#land pos and plot type and inun
var.GPP.all = update(gls.GPP, weights = varComb(varIdent(form = ~ 1| landscape_position), varIdent(form = ~ 1 | plot_type), varIdent(form = ~1 |inundated)))
anova(var.GPP.all) #no variables sig, soil temp borderline p=0.05
#testing all multiple var models
anova(var.GPP.plottype.inun, var.GPP.all) #GPP.all is slightly better fit, p=0.001
anova(var.GPP.landpos.inun, var.GPP.landpos.plottype, var.GPP.plottype.inun, var.GPP.all) #sig diff, but landpost-plottype and GPP.all are nearly the same, landpos-plottype slightly better
#testing multiple var models with single var models
anova(gls.GPP, var.GPP.landpos.plottype, var.GPP.plottype.inun, var.GPP.landpos.inun, var.GPP.inundated, var.GPP.landpos, var.GPP.plottype)
#sig diffs but all very close, GPP.landpos-plottype appears best
#looking at fixed effects with selected model of var.GPP.plottype.inun
anova(var.GPP.landpos.plottype, type = "marginal") #soil temp is marginal (p=0.05), no others sig
#refit with REML
GPP.final = update(var.GPP.landpos.plottype, method = "REML")
anova(GPP.final) #only soil temp is margincally sig p = 0.05
```
#RECO
#df_RECO: Testing df_RECO models
```{r}
#Testing parameters in df_RECO as a whole - using Kyle's "fluxes" code as guide
#Make plot_ID, inundated, plot_type, landscape_position factor so they'll work with gls
df_RECO$plot_ID = factor(df_RECO$plot_ID)
df_RECO$plot_type = factor (df_RECO$plot_type)
df_RECO$landscape_position = factor(df_RECO$landscape_position)
df_RECO$inundated = factor(df_RECO$inundated)
#df_NEE$soil_temp_10_cm = as.numeric (df_NEE$soil_temp_10_cm)
#Tried with both 'ML' and 'REML' - no major diffs in results
#gls - no random effect
gls.RECO = gls(flux_value ~ plot_type + landscape_position + inundated + soil_temp_10_cm, data = df_RECO, method = 'ML', na.action=na.exclude)
anova(gls.RECO) #sig diff in soil temp p<0.001
#lme - with random effect of plot_ID
lme.RECO <- lme(flux_value ~ plot_type + landscape_position + inundated + soil_temp_10_cm,
random = ~1 | plot_ID,
data = df_RECO, na.action=na.exclude ,
method = 'ML')
anova(lme.RECO) #sig diff in soil temp, p <0.001
#Comparing the models
anova(gls.RECO, lme.RECO) #does show a sig diff, that lme.RECO is slightly better fit (p=0.02 but nearly identical AIC/BIC?logLik)
#comparing data QQplot to a normal QQplot and hist
qqnorm(residuals(lme.RECO))
hist(residuals(lme.RECO)) #shows a bit of left skew
car::qqPlot(x = residuals(lme.RECO)) #shows where residuals breach normal distr
#comparing data QQplot to a normal QQplot and hist
qqnorm(residuals(gls.RECO))
hist(residuals(gls.RECO)) #shows a bit of left skew
car::qqPlot(x = residuals(gls.RECO)) #shows where residuals breach normal distr
```
####Multico;llinearity - RECO
```{r}
library(car)
vif(gls.RECO) # <2, no impactful multicollinearity
```
####Checking each variable on its own
```{r}
#double checking each var on its own
gls.RECO.landscape = gls(flux_value ~ landscape_position, data = df_RECO, method = 'ML', na.action=na.exclude)
anova(gls.RECO.landscape) #p=0.88, not sig
gls.RECO.plottype = gls(flux_value ~ plot_type, data = df_RECO, method = 'ML', na.action=na.exclude)
anova(gls.RECO.plottype) #p=0.49, not sig
gls.RECO.inundated = gls(flux_value ~ inundated, data = df_RECO, method = 'ML', na.action=na.exclude)
anova(gls.RECO.inundated) #p=0.51, not sig
gls.RECO.soiltemp = gls(flux_value ~ soil_temp_10_cm, data = df_RECO, method = 'ML', na.action=na.exclude)
anova(gls.RECO.soiltemp) #p=0.006, Sig**
anova(gls.RECO, gls.RECO.soiltemp) #no sig difference
plot(df_RECO$flux_value, df_RECO$soil_temp_10_cm) + geom_point()
```
###Variance Structure - RECO
```{r}
#select variance structure
#based on inundation --> won't work, error: "Error in eigen(val, only.values = TRUE) : infinite or missing values in 'x'"
var.RECO.inundated = update(gls.RECO, weights = varIdent(form = ~1|inundated), na.action=na.exclude)
anova(var.RECO.inundated) #soil temp sig, p < 0.0001
anova(gls.RECO, var.RECO.inundated) #inundated better (p=0.04) but only slightly better AIC/BIC
#based on landscape position
var.RECO.landpos = update(gls.RECO, weights = varIdent(form = ~1|landscape_position))
anova(var.RECO.landpos) ##soil temp sig, p < 0.0001
anova(gls.RECO, var.RECO.landpos) #p = 0.7 - var does not improve model
#based on plot type
var.RECO.plottype = update(gls.RECO, weights = varIdent(form = ~1|plot_type))
anova(var.RECO.plottype) #soil temp sig, p < 0.0001
anova(gls.RECO, var.RECO.plottype) #p = <0.17, var does not improve model
#based on soil temp --> "infinite or missing values in x" --> skipping, not categorical
# var.RECO.soiltemp = update(gls.RECO, weights = varIdent(form = ~1|soil_temp_10_cm), na.action=na.exclude)
# anova(var.RECO.soiltemp)
#now testing which models are best fit
anova(var.RECO.inundated, var.RECO.landpos, var.RECO.plottype) #no sig diff
anova(var.RECO.inundated, var.RECO.landpos) #no sig diff
anova(var.RECO.inundated,var.RECO.plottype) #no sig diff
anova(var.RECO.landpos, var.RECO.plottype) #no sig diff
#looks like models with variance structure do not make a significant improvement upon model, so not needed
```
#Multiple Var Structure - RECO
```{r}
#looking at models with multiple variance structures
#inundation and land pos
library(nlme)
var.RECO.landpos.inun = update(gls.RECO, weights = varComb(varIdent(form = ~ 1| inundated), varIdent(form = ~ 1 | landscape_position)))
anova(var.RECO.landpos.inun) #shows sig diff in soil temp (p<0.0001)
#inundation and plot type
var.RECO.plottype.inun = update(gls.RECO, weights = varComb(varIdent(form = ~ 1| inundated), varIdent(form = ~ 1 | plot_type)))
anova(var.RECO.plottype.inun) #shows sig diff in soil temp (p<0.0001)
#testing landpos.inun vs plottype.inun models
anova(var.RECO.landpos.inun,var.RECO.plottype.inun) #no sig diff here
#land pos and plot type
var.RECO.landpos.plottype = update(gls.RECO, weights = varComb(varIdent(form = ~ 1| landscape_position), varIdent(form = ~ 1 | plot_type)))
anova(var.RECO.landpos.plottype) #shows sig diff in soil temp (p<0.0001)
#testing the three models
anova(var.RECO.landpos.inun, var.RECO.landpos.plottype, var.RECO.plottype.inun) #no sig diffs
#land pos and plot type and inun
var.RECO.all = update(gls.RECO, weights = varComb(varIdent(form = ~ 1| landscape_position), varIdent(form = ~ 1 | plot_type), varIdent(form = ~1 |inundated)))
anova(var.RECO.all) #soil temp sig, p<0.001
#testing all multiple var models
anova(var.RECO.plottype.inun, var.RECO.all) #no sig diff
anova(var.RECO.landpos.inun, var.RECO.landpos.plottype, var.RECO.plottype.inun, var.RECO.all) #no sig diff
#testing multiple var models with single var models
anova(gls.RECO, var.RECO.plottype.inun, var.RECO.landpos.inun, var.RECO.landpos.plottype, var.RECO.all, var.RECO.inundated, var.RECO.landpos, var.RECO.plottype)
#no sig diffs, use model without variance structure
#looking at fixed effects with selected model of var.RECO.plottype.inun
anova(gls.RECO, type = "marginal") #soil temp sig (p<0.0001)
#refit with REML
RECO.final = update(gls.RECO, method = "REML")
anova(RECO.final) #soil temp sig, p<0.0001
#checking residuals
plot(RECO.final)
#checking normality with qq plot
qqnorm(RECO.final)
#comparing data QQplot to a normal QQplot and hist
qqnorm(residuals(RECO.final))
hist(residuals(RECO.final)) #shows a bit of left skew
car::qqPlot(x = residuals(RECO.final)) #shows where residuals breach normal distr
summary(RECO.final)
#slope and BGC plottype not showing up in the summary or stats....? or am I reading this wrong?
```