-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhypothesis_1_clumping.R
1007 lines (807 loc) · 48 KB
/
hypothesis_1_clumping.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
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
#### Loading libraries and relevant data ####
library(tidyverse)
library(moments) # for calculating the moments of each variable
library(sf) # for plotting spatial objects
library(smatr)
library(ggpmisc)
library(PMCMRplus) # for Dunn test
library(geomtextpath) # for PCA graphing
library(spatstat) # to run the Ripley's K function: Kest
library(stars) # for sf_rasterize function
library(raster) #to use crop
library(starsExtra) #to use dist_to_nearest
library(geostatsp)
library(tmaptools)
fixed_field_data_processed <- read.csv("./analyses/fixed_field_data_processed.csv") #imports the csv created from analyzing_morpho_data_cleaned.R
#OLD upload of old river shapefile and filter out polygons for each population, before adding the tributaries/roads
rivers <- st_read("./data/QUBR Rivers and Trees.kml", "Rivers", crs = 4326)
rivers_2d <- st_zm(rivers, drop = T) #we had a z dimension with max and min, so we got rid of it because it was giving us weird errors and disrupting later statistics
river_LC_old <- filter(rivers_2d, Name == "River LC")
river_SD_old <- filter(rivers_2d, Name == "River SD")
river_LM_old <- filter(rivers_2d, Name == "LM River")
plot(rivers)
#changing the coordinate reference system of the river polygons to be equal area projection (UTM 12N), uses meters as distance measurement
river_LM_trans_old <- st_transform(river_LM_old, crs = 26912)
river_LC_trans_old <- st_transform(river_LC_old, crs = 26912)
river_SD_trans_old <- st_transform(river_SD_old, crs = 26912)
#upload ArcGIS river shapefile and filter out polygons for each population
river_LM <- st_read("./data/Shapefiles/FINAL River Shapefiles ArcGIS/LM River/LM_Rivers_Final.shp")
river_LM <- river_LM$geometry[1]
river_LC <- st_read("./data/Shapefiles/FINAL River Shapefiles ArcGIS/LC River/LC_Rivers_Final.shp")
river_LC <- river_LC$geometry[1]
river_SD <- st_read("./data/Shapefiles/FINAL River Shapefiles ArcGIS/SD River/SD_Rivers_Final.shp")
river_SD <- river_SD$geometry[1]
#changing the coordinate reference system of the river polygons to be equal area projection (UTM 12N), uses meters as distance measurement
river_LM_trans <- st_as_sf(st_transform(river_LM, crs = 26912))
river_LC_trans <- st_as_sf(st_transform(river_LC, crs = 26912))
river_SD_trans <- st_as_sf(st_transform(river_SD, crs = 26912))
#creating shapefiles for each population, turning sf of all points into sfc
LM_fixed_field_data_processed_sf <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "LM") %>%
st_as_sf()
LC_fixed_field_data_processed_sf <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "LC") %>%
st_as_sf()
SD_fixed_field_data_processed_sf <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "SD") %>%
st_as_sf()
#plotting points with river shapefiles
#LM
ggplot()+
geom_sf(data = river_LM_trans)+
geom_sf(data = LM_fixed_field_data_processed_sf)
#LC
ggplot()+
geom_sf(data = river_LC_trans)+
geom_sf(data = LC_fixed_field_data_processed_sf)
#SD
ggplot()+
geom_sf(data = river_SD_trans)+
geom_sf(data = SD_fixed_field_data_processed_sf)+
theme_light()
#creating buffers around the rivers
river_buffer_LM <- st_buffer(river_LM_trans, 100) #100 m buffer
ggplot()+
geom_sf(data = river_buffer_LM)+
geom_sf(data = river_LM_trans)+
geom_sf(data = LM_fixed_field_data_processed_sf)
river_buffer_LC<- st_buffer(river_LC_trans, 100) #230 m buffer
ggplot()+
geom_sf(data = river_buffer_LC)+
geom_sf(data = river_LC_trans)+
geom_sf(data = LC_fixed_field_data_processed_sf)
river_buffer_SD<- st_buffer(river_SD_trans, 70) #70 m buffer
ggplot()+
geom_sf(data = river_buffer_SD)+
geom_sf(data = river_SD_trans)+
geom_sf(data = SD_fixed_field_data_processed_sf)
#### Creating fixed_field_data_processed dataframes for each population ####
LM_fixed_field_data_processed <- fixed_field_data_processed %>%
filter(Locality == "LM")
LC_fixed_field_data_processed <- fixed_field_data_processed %>%
filter(Locality == "LC")
SD_fixed_field_data_processed <- fixed_field_data_processed %>%
filter(Locality == "SD")
#### Importing Shapefile ####
#turn the BCS polygon into a shapefile and visualize its outline
BCS_polygon <- read_sf("./data/Shapefiles/BCS_Polygon/bcs_entidad.shp")
BCS_polygon <- st_as_sf(BCS_polygon)
plot(BCS_polygon$geometry)
#creating a shapefile of all points with lat lon coordinates in WGS 1984
fixed_field_data_processed_sf <- st_as_sf(fixed_field_data_processed,
coords = c("long", "lat"), crs = 4326)
#creating a transformed shapefile with UTM 12 N an equal area projection
fixed_field_data_processed_sf_transformed <- st_transform(fixed_field_data_processed_sf, crs = 26912)
#### Plot the Baja Polygons and Creating Shapefiles for Each Populations ####
#finding minimum and maximum lat and long values
min_all_locality_long <- min(fixed_field_data_processed$long)*1.0002
max_all_locality_long <- max(fixed_field_data_processed$long) - (max(fixed_field_data_processed$long) *.0002)
min_all_locality_lat <- min(fixed_field_data_processed$lat)*1.02
max_all_locality_lat <- max(fixed_field_data_processed$lat) - (max(fixed_field_data_processed$lat)*.02)
#plotting the BCS polygon with the tree points
ggplot(data = BCS_polygon) +
geom_sf() +
geom_sf(data = fixed_field_data_processed_sf, aes(color = Locality)) +
coord_sf(xlim = c(min_all_locality_long, max_all_locality_long),
ylim = c(min_all_locality_lat, max_all_locality_lat))+
theme_classic()
#creating BCS boundary shapefile, turning sf of all points into sfc
fixed_field_data_processed_sf <- fixed_field_data_processed_sf_transformed %>%
st_as_sfc()
#creating a boundry box with the UTM 12 N min and max lat lon values and then turning it into a simple feature geometry
fixed_field_data_processed_box <- fixed_field_data_processed_sf_transformed %>%
st_bbox %>%
st_as_sfc()
#finding minimum and maximum lat and long values for LM
LM_min_all_locality_long <- min(LM_fixed_field_data_processed$long)#*1.0002
LM_max_all_locality_long <- max(LM_fixed_field_data_processed$long)# - (max(LM_fixed_field_data_processed$long) *.0002)
LM_min_all_locality_lat <- min(LM_fixed_field_data_processed$lat)#*1.002
LM_max_all_locality_lat <- max(LM_fixed_field_data_processed$lat) #- (max(LM_fixed_field_data_processed$lat)*.002)
#plotting the BCS LM polygon with the tree points
ggplot(data = BCS_polygon) +
geom_sf() +
geom_sf(data = fixed_field_data_processed_sf, aes(color = Locality)) +
coord_sf(xlim = c(LM_min_all_locality_long, LM_max_all_locality_long),
ylim = c(LM_min_all_locality_lat, LM_max_all_locality_lat))+
theme_classic()
#creating LM boundary shapefile, turning sf of all points into sfc
LM_fixed_field_data_processed_sf <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "LM") %>%
st_as_sfc()
#creating a boundry box of LM with the UTM 12 N min and max lat lon values and then turning it into a simple feature geometry
LM_fixed_field_data_processed_box <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "LM") %>%
st_bbox %>%
st_as_sfc()
#finding minimum and maximum lat and long values for LC
LC_min_all_locality_long <- min(LC_fixed_field_data_processed$long)#*1.0002
LC_max_all_locality_long <- max(LC_fixed_field_data_processed$long)# - (max(LM_fixed_field_data_processed$long) *.0002)
LC_min_all_locality_lat <- min(LC_fixed_field_data_processed$lat)#*1.002
LC_max_all_locality_lat <- max(LC_fixed_field_data_processed$lat) #- (max(LM_fixed_field_data_processed$lat)*.002)
#plotting the BCS LC polygon with the tree points
ggplot(data = BCS_polygon) +
geom_sf() +
geom_sf(data = fixed_field_data_processed_sf, aes(color = Locality)) +
coord_sf(xlim = c(LC_min_all_locality_long, LC_max_all_locality_long),
ylim = c(LC_min_all_locality_lat, LC_max_all_locality_lat))+
theme_classic()
#creating LC boundary shapefile, turning sf of all points into sfc
LC_fixed_field_data_processed_sf <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "LC") %>%
st_as_sfc()
#creating a boundry box of LC with the UTM 12 N min and max lat lon values and then turning it into a simple feature geometry
LC_fixed_field_data_processed_box <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "LC") %>%
st_bbox %>%
st_as_sfc()
#finding minimum and maximum lat and long values for SD
SD_min_all_locality_long <- min(SD_fixed_field_data_processed$long)#*1.0002
SD_max_all_locality_long <- max(SD_fixed_field_data_processed$long)# - (max(LM_fixed_field_data_processed$long) *.0002)
SD_min_all_locality_lat <- min(SD_fixed_field_data_processed$lat)#*1.002
SD_max_all_locality_lat <- max(SD_fixed_field_data_processed$lat) #- (max(LM_fixed_field_data_processed$lat)*.002)
#plotting the BCS SD polygon with the tree points
ggplot(data = BCS_polygon) +
geom_sf() +
geom_sf(data = fixed_field_data_processed_sf, aes(color = Locality)) +
coord_sf(xlim = c(SD_min_all_locality_long, SD_max_all_locality_long),
ylim = c(SD_min_all_locality_lat, SD_max_all_locality_lat))+
theme_classic()
#creating SD boundary shapefile, turning sf of all points into sfc
SD_fixed_field_data_processed_sf <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "SD") %>%
st_as_sfc()
#creating a boundry box of SD with the UTM 12 N min and max lat lon values and then turning it into a simple feature geometry
SD_fixed_field_data_processed_box <- fixed_field_data_processed_sf_transformed %>%
filter(Locality == "SD") %>%
st_bbox %>%
st_as_sfc()
#### Creating Convex Hulls using tree points of each population ####
river_LM_convex_hull <- st_convex_hull(st_union(LM_fixed_field_data_processed_sf)) #LM_fixed_field_data_processed_sf
ggplot(river_LM_convex_hull)+
geom_sf()
river_LC_convex_hull <- st_convex_hull(st_union(LC_fixed_field_data_processed_sf)) #LM_fixed_field_data_processed_sf
ggplot(river_LC_convex_hull)+
geom_sf()
river_SD_convex_hull <- st_convex_hull(st_union(SD_fixed_field_data_processed_sf)) #LM_fixed_field_data_processed_sf
ggplot(river_SD_convex_hull)+
geom_sf()
#### Ripley's K Analysis (version with box, convex hull, and 20 m buffer around river) ####
#Ripley's K for all points
win <- as.owin(fixed_field_data_processed_box) #turning the box into a window
ppp <- as.ppp(st_coordinates(fixed_field_data_processed_sf), W = win) #creating the poisson point pattern for lm
plot(ppp, pch = 16, cex = 0.5)
K <- Kest(ppp, correction = "Ripley") #Ripley's K function
plot(K, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
#Ripley's K for LM
LM_win <- as.owin(LM_fixed_field_data_processed_box) #turning the box into a window
LM_ppp <- as.ppp(st_coordinates(LM_fixed_field_data_processed_sf), W = LM_win) #creating the poisson point pattern for lm
plot(LM_ppp, pch = 16, cex = 0.5)
LM_k <- Kest(LM_ppp, correction = "Ripley") #Ripley's K function
plot(LM_k, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
#Ripley's K for LM with Convex Hull
LM_win_convex <- as.owin(river_LM_convex_hull) #turning the convex hull into a window
LM_ppp_convex <- as.ppp(st_coordinates(LM_fixed_field_data_processed_sf), W = LM_win_convex) #creating the poisson point pattern for lm
plot(LM_ppp_convex, pch = 16, cex = 0.5)
LM_k_convex <- Kest(LM_ppp_convex, correction = "Ripley") #Ripley's K function
plot(LM_k_convex, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
#Ripley's K for LM with Buffer River
LM_win_buffer <- as.owin(river_buffer_LM) #turning the buffer into a window
LM_ppp_buffer <- as.ppp(st_coordinates(LM_fixed_field_data_processed_sf), W = LM_win_buffer) #creating the poisson point pattern for lm
plot(LM_ppp_buffer, pch = 16, cex = 0.5)
LM_k_buffer <- Kest(LM_ppp_buffer, correction = "Ripley") #Ripley's K function
plot(LM_k_buffer, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
plot(LM_k_buffer, main=NULL, las=1, ylab = "", xlab = "", legendargs=list(cex=0.8, xpd=TRUE),
yaxp = c(0, 220000, 10))#legend inside of the plot
par(mar = c(6,6,6,6))
title(ylab = bquote(italic("K(r), Las Matancitas trees")), cex = 1.2, line = 4)
title(xlab = bquote(italic("r (m)")),)
#Ripley's K for LC
LC_win <- as.owin(LC_fixed_field_data_processed_box) #turning the box into a window
LC_ppp <- as.ppp(st_coordinates(LC_fixed_field_data_processed_sf), W = LC_win) #creating the poisson point pattern for lm
plot(LC_ppp, pch = 16, cex = 0.5)
LC_k <- Kest(LC_ppp, correction = "Ripley") #Ripley's K function
plot(LC_k, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
#Ripley's K for LC with Convex Hull
LC_win_convex <- as.owin(river_LC_convex_hull) #turning the convex hull into a window
LC_ppp <- as.ppp(st_coordinates(LC_fixed_field_data_processed_sf), W = LC_win_convex) #creating the poisson point pattern for lm
plot(LC_ppp, pch = 16, cex = 0.5)
LC_k <- Kest(LC_ppp, correction = "Ripley") #Ripley's K function
plot(LC_k, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
#Ripley's K for LC with Buffer River
LC_win_buffer <- as.owin(river_buffer_LC) #turning the buffer into a window
LC_ppp_buffer <- as.ppp(st_coordinates(LC_fixed_field_data_processed_sf), W = LC_win_buffer) #creating the poisson point pattern for lm
plot(LC_ppp_buffer, pch = 16, cex = 0.5)
LC_k_buffer <- Kest(LC_ppp_buffer, correction = "Ripley") #Ripley's K function
plot(LC_k_buffer, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
plot(LC_k_buffer, main=NULL, las=1, ylab = "", xlab = "", legendargs=list(cex=0.8, xpd=TRUE),
yaxp = c(0, 120000, 10))#legend inside of the plot
par(mar = c(6,6,6,6))
title(ylab = bquote(italic("K(r), La Cobriza trees")), cex = 1.2, line = 4)
title(xlab = bquote(italic("r (m)")),)
#Ripley's K for SD
SD_win <- as.owin(SD_fixed_field_data_processed_box) #turning the box into a window
SD_ppp <- as.ppp(st_coordinates(SD_fixed_field_data_processed_sf), W = SD_win) #creating the poisson point pattern for lm
plot(SD_ppp, pch = 16, cex = 0.5)
SD_k <- Kest(SD_ppp, correction = "Ripley") #Ripley's K function
plot(SD_k, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
#Ripley's K for SD with Convex Hull
SD_win_convex <- as.owin(river_SD_convex_hull) #turning the convex hull into a window
SD_ppp <- as.ppp(st_coordinates(SD_fixed_field_data_processed_sf), W = SD_win_convex) #creating the poisson point pattern for lm
plot(SD_ppp, pch = 16, cex = 0.5)
SD_k <- Kest(SD_ppp, correction = "Ripley") #Ripley's K function
plot(SD_k, main=NULL, las=1, legendargs=list(cex=0.8, xpd=TRUE)) #legend inside of the plot
#Ripley's K for SD with Buffer River
SD_win_buffer <- as.owin(river_buffer_SD) #turning the buffer into a window
SD_ppp_buffer <- as.ppp(st_coordinates(SD_fixed_field_data_processed_sf), W = SD_win_buffer) #creating the poisson point pattern for lm
plot(SD_ppp_buffer, pch = 16, cex = 0.5)
SD_k_buffer <- Kest(SD_ppp_buffer, correction = "Ripley") #Ripley's K function
plot(SD_k_buffer, main=NULL, las=1, ylab = "", xlab = "", legendargs=list(cex=0.8, xpd=TRUE),
yaxp = c(0, 300000, 10), cex.axis = 1.1)#legend inside of the plot
par(mar = c(8,8,8,8))
title(ylab = bquote(italic("K(r), San Dionisio trees")), cex.lab = 1.5, line = 4)
title(xlab = bquote(italic("r (m)")), cex.lab = 1.5)
#### ANN Analysis (test for clustering/dispersion) ####
#test for LM
#assigning average nearest neighbor values for the entire population of trees
ann.p_LM <- mean(nndist(LM_ppp, k=1))
ann.p_LM
#simulation to create a list of ANN from randomly placed points
n <- 566L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(LM_fixed_field_data_processed_sf), win = river_LM_convex_hull) #river_buffer_LM #river_LM_trans. #river_LM_convex_hull
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the convex hull window
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and the river
ggplot()+
geom_sf(data=river_LM_trans)+ #plotting the river
geom_sf(data=LM_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#creating a histogram of the ANN Simulation Results
as_tibble(ann.r) %>% #turns the list of ann values from the simulations of random points and turns it into a tibble/dataframe
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_LM, ann.r)) + #sets the limit of the xaxis to encompass the ANN for our trees and histogram of ANNs from the simulation
geom_vline(xintercept=ann.p_LM, col = "red") + #adds a verticle line of our tree'\s' ANN
xlab("ANN")+
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){ #loop that adds 1 to the value total if the simulated ANN value is less than our average value for our trees
if (ann.r[i] < ann.p_LM){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
#Test for LC
#assigning average nearest neighbor values for the entire population of trees
ann.p_LC <- mean(nndist(LC_ppp, k=1))
ann.p_LC
#simulation to create a list of ANN from randomly placed points
n <- 566L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(LC_fixed_field_data_processed_sf), win = river_LC_convex_hull) #river_buffer_LM #river_LM_trans. #river_LM_convex_hull
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the convex hull window
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and the river
ggplot()+
geom_sf(data=river_LC_trans)+ #plotting the river
geom_sf(data=LC_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#creating a histogram of the ANN Simulation Results
as_tibble(ann.r) %>% #turns the list of ann values from the simulations of random points and turns it into a tibble/dataframe
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_LC, ann.r)) + #sets the limit of the xaxis to encompass the ANN for our trees and histogram of ANNs from the simulation
geom_vline(xintercept=ann.p_LC, col = "red") + #adds a verticle line of our tree'\s' ANN
xlab("ANN")+
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){ #loop that adds 1 to the value total if the simulated ANN value is less than our average value for our trees
if (ann.r[i] < ann.p){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
#Test for SD
#assigning average nearest neighbor values for the entire population of trees
ann.p_SD <- mean(nndist(SD_ppp, k=1))
ann.p_SD
#simulation to create a list of ANN from randomly placed points
n <- 566L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(SD_fixed_field_data_processed_sf), win = river_SD_convex_hull) #river_buffer_LM #river_LM_trans. #river_LM_convex_hull
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the convex hull window
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and the river
ggplot()+
geom_sf(data=river_SD_trans)+ #plotting the river edge raster
geom_sf(data=SD_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#creating a histogram of the ANN Simulation Results
as_tibble(ann.r) %>% #turns the list of ann values from the simulations of random points and turns it into a tibble/dataframe
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_SD, ann.r)) + #sets the limit of the xaxis to encompass the ANN for our trees and histogram of ANNs from the simulation
geom_vline(xintercept=ann.p_SD, col = "red") + #adds a verticle line of our tree'\s' ANN
xlab("ANN")+
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){ #loop that adds 1 to the value total if the simulated ANN value is less than our average value for our trees
if (ann.r[i] < ann.p){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
#### ANN Analysis (test for clustering/dispersion) while controlling for the river ####
###Test for LM
#turning river polygon into multipoints and then into a raster for using them to calculate the distances
river_LM_trans_outline <- st_cast(river_LM_trans, "LINESTRING") #turns the polyline of the river into a multipoint object
river_LM_trans_point_raster <- st_rasterize(river_LM_trans_points) #create raster of lake edge points
plot(river_LM_trans_point_raster)
river_LM_buffer_trans_outline <- st_cast(river_buffer_LM, "LINESTRING") #turns the polyline of the river into a multipoint object
river_buffer_LM_point_raster <- st_rasterize(river_LM_buffer_trans_outline) #create raster of lake edge points
plot(river_buffer_LM_point_raster)
#making a stars object of the distances of each cell in the buffer raster from the river edge points
river_buffer_LM_point_raster[is.na(river_buffer_LM_point_raster[])] <- 0 #making sure the points that are not the river buffer have a 0 value
dist_near_river_buffer_LM <- dist_to_nearest(river_buffer_LM_point_raster, river_LM_trans_points, progress = T) #creating a raster of the distances of each cell in the buffer raster to the multipoints on the river polygon, this took an hour to run
dist_near_river_buffer_LM_inverse <- 1/dist_near_river_buffer_LM #creating the inverse of the distance raster so that the higher values are closer to the river and the values are between 0-1
plot(dist_near_river_buffer_LM_inverse)
#creating a raster with assigned values of 1 to cells within 30 m of the river edge and 1/distance to the cells outside to turn the distances into values 0-1
dist_near_river_buffer_LM_inverse <- dist_near_river_buffer_LM %>% #creating a new stars object with new defined values for distance
st_as_sf() %>% #converting the stars to a shapefile
mutate(d = case_when(d <= 20 ~ 1,
d > 1 ~ 1/d)) %>% #assigning cells less than 30 m away from rivers edge with value of 1 and taking 1/distance for all other cells
st_rasterize() #convert the shapefile into a raster
plot(dist_near_river_buffer_LM_inverse)
## Version of ANN analysis controlling for the river with just the river multipoint
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(LM_fixed_field_data_processed_sf), f = as.im(river_LM_trans_point_raster))
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point on top of the river's edge while controlling for the river's edge
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and probability/distance raster
ggplot()+
geom_stars(data=river_LM_trans_point_raster)+ #plotting the river edge raster
geom_sf(data=LM_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#graphing the histogram of simulated ANN values and the mean ANN from our trees
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_LM, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_LM, col = "red") + #plotting our tree's mean ANN
xlab("ANN") +
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] < ann.p_LM){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
## Version of ANN analysis controlling for the river with inside, on, and outside the river
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(LM_fixed_field_data_processed_sf),
f = as.im(dist_near_river_buffer_LM_inverse)) #dist_near_river_buffer_LM_inverse #forcewin = T, win=as.owin(river_LM_convex_hull)
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the distance raster while controlling for distance to the river's edge
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and probability/distance raster
ggplot()+
geom_stars(data=dist_near_river_buffer_LM_inverse)+ #plotting the distance inverse raster
geom_sf(data=LM_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#graphing the histogram of simulated ANN values and the mean ANN from our trees
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_LM, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_LM, col = "red") + #plotting our tree's mean ANN
xlab("ANN") +
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] < ann.p_LM){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
## Version of ANN analysis controlling for the river with on and inside the river
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(LM_fixed_field_data_processed_sf), f = as.im(st_rasterize(river_LM_trans))) #dist_near_river_buffer_LM_inverse
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the raster while controlling for the river
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and river raster
ggplot()+
geom_stars(data=st_rasterize(river_LM_trans))+ #plotting the river raster
geom_sf(data=LM_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#graphing the histogram of simulated ANN values and the mean ANN from our trees
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_LM, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_LM, col = "red") + #plotting our tree's mean ANN
xlab("ANN") +
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] < ann.p){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
###test for LC
#turning river polygon into multipoints and then into a raster for using them to calculate the distances
river_LC_trans_points <- st_cast(river_LC_trans, "LINESTRING") #turns the polyline of the river into a multipoint object
river_LC_trans_point_raster <- st_rasterize(river_LC_trans_points) #create raster of lake edge points
plot(river_LC_trans_point_raster)
river_buffer_LC_points <- st_cast(river_buffer_LC, "LINESTRING") #turns the polyline of the river buffer into a multipoint object in stars
river_buffer_LC_point_raster <- st_rasterize(river_buffer_LC_points) #create raster of lake edge points
plot(river_buffer_LC_point_raster)
#making a stars object of the distances of each cell in the buffer raster from the river edge points
river_buffer_LC_point_raster[is.na(river_buffer_LC_point_raster[])] <- 0 #making sure the points that are not the river buffer have a 0 value
dist_near_river_buffer_LC <- dist_to_nearest(river_buffer_LC_point_raster, river_LC_trans_points, progress = T) #creating a raster of the distances of each cell in the buffer raster to the multipoints on the river polygon, this took an hour to run
dist_near_river_buffer_LC_inverse <- 1/dist_near_river_buffer_LC #creating the inverse of the distance raster so that the higher values are closer to the river and the values are between 0-1
plot(dist_near_river_buffer_LC_inverse)
#creating a raster with assigned values of 1 to cells within 30 m of the river edge and 1/distance to the cells outside to turn the distances into values 0-1
dist_near_river_buffer_LC_inverse <- dist_near_river_buffer_LC %>% #creating a new stars object with new defined values for distance
st_as_sf() %>% #converting the stars to a shapefile
mutate(d = case_when(d <= 20 ~ 1,
d > 1 ~ 1/d)) %>% #assigning cells less than 20 m away from rivers edge with value of 1 and taking 1/distance for all other cells
st_rasterize() #convert the shapefile into a raster
plot(dist_near_river_buffer_LC_inverse)
## Version of ANN analysis controlling for the river with just the river multipoint
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(LC_fixed_field_data_processed_sf), f = as.im(river_LC_trans_point_raster))
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point on top of the river's edge while controlling for the river's edge
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and probability/distance raster
ggplot()+
geom_stars(data=river_LC_trans_point_raster)+ #plotting the river edge raster
geom_sf(data=LC_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#graphing the histogram of simulated ANN values and the mean ANN from our trees
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_LC, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_LC, col = "red") + #plotting our tree's mean ANN
xlab("ANN") +
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] < ann.p){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
## Version of ANN analysis controlling for the river with inside, on, and outside the river
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(LC_fixed_field_data_processed_sf),
f = as.im(dist_near_river_buffer_LC_inverse)) #dist_near_river_buffer_LM_inverse #forcewin = T, win=as.owin(river_LM_convex_hull)
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the distance raster while controlling for distance to the river's edge
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and probability/distance raster
ggplot()+
geom_stars(data=dist_near_river_buffer_LC_inverse)+ #plotting the distance inverse raster
geom_sf(data=LC_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#graphing the histogram of simulated ANN values and the mean ANN from our trees
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_LC, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_LC, col = "red") + #plotting our tree's mean ANN
xlab("ANN") +
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] < ann.p){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
## Version of ANN analysis controlling for the river with on and inside the river
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(LC_fixed_field_data_processed_sf), f = as.im(st_rasterize(river_LC_trans))) #dist_near_river_buffer_LM_inverse
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the raster while controlling for the river
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and river raster
ggplot()+
geom_stars(data=st_rasterize(river_LC_trans))+ #plotting the river raster
geom_sf(data=LC_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#graphing the histogram of simulated ANN values and the mean ANN from our trees
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "skyblue", color = "black", bins = 50) +
xlim(range(ann.p_LC, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_LC, col = "red", size = 1.2) + #plotting our tree's mean ANN
xlab("Average Nearest Neighbor (ANN)") +
theme_classic()+
theme(axis.text=element_text(size=15), axis.title.x =element_text(size= 15),
axis.title.y =element_text(size= 15))
#calculating pseudo p-value for the disperse mean ANN value
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] > ann.p){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
###test for SD
#turning river polygon into multipoints and then into a raster for using them to calculate the distances
river_SD_trans_points <- st_cast(river_SD_trans, "LINESTRING") #turns the polyline of the river into a multipoint object
river_SD_trans_point_raster <- st_rasterize(river_SD_trans_points) #create raster of lake edge points
plot(river_SD_trans_point_raster)
river_buffer_SD_points <- st_cast(river_buffer_SD, "LINESTRING") #turns the polyline of the river buffer into a multipoint object
river_buffer_SD_point_raster <- st_rasterize(river_buffer_SD_points) #create raster of lake edge points
plot(river_buffer_SD_point_raster)
#making a stars object of the distances of each cell in the buffer raster from the river edge points
river_buffer_SD_point_raster[is.na(river_buffer_SD_point_raster[])] <- 0 #making sure the points that are not the river buffer have a 0 value
dist_near_river_buffer_SD <- dist_to_nearest(river_buffer_SD_point_raster, river_SD_trans_points, progress = T) #creating a raster of the distances of each cell in the buffer raster to the multipoints on the river polygon, this took an hour to run
dist_near_river_buffer_SD_inverse <- 1/dist_near_river_buffer_SD #creating the inverse of the distance raster so that the higher values are closer to the river and the values are between 0-1
plot(dist_near_river_buffer_SD_inverse)
#creating a raster with assigned values of 1 to cells within 30 m of the river edge and 1/distance to the cells outside to turn the distances into values 0-1
dist_near_river_buffer_SD_inverse <- dist_near_river_buffer_SD %>% #creating a new stars object with new defined values for distance
st_as_sf() %>% #converting the stars to a shapefile
mutate(d = case_when(d <= 80 ~ 1,
d > 1 ~ 1/d)) %>% #assigning cells less than 60 m away from rivers edge with value of 1 and taking 1/distance for all other cells
st_rasterize() #convert the shapefile into a raster
plot(dist_near_river_buffer_SD_inverse)
## Version of ANN analysis controlling for the river with just the river multipoint
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(SD_fixed_field_data_processed_sf), f = as.im(river_SD_trans_point_raster))
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point on top of the river's edge while controlling for the river's edge
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and probability/distance raster
ggplot()+
geom_stars(data=river_SD_trans_point_raster)+ #plotting the river edge raster
geom_sf(data=SD_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
#graphing the histogram of simulated ANN values and the mean ANN from our trees
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_SD, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_SD, col = "red") + #plotting our tree's mean ANN
xlab("ANN") +
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] < ann.p_SD){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
## Version of ANN analysis controlling for the river with inside, on, and outside the river
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(SD_fixed_field_data_processed_sf),
f = as.im(dist_near_river_buffer_SD_inverse)) #dist_near_river_buffer_LM_inverse #forcewin = T, win=as.owin(river_LM_convex_hull)
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the distance raster while controlling for distance to the river's edge
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and probability/distance raster
ggplot()+
geom_stars(data=dist_near_river_buffer_SD_inverse)+ #plotting the distance inverse raster
geom_sf(data=SD_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA)+ #plotting the random points
labs(color = "Trees", fill = "Inverse Distance (m)")
#graphing the histogram of simulated ANN values and the mean ANN from our trees
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "dodgerblue1", color = "black", bins = 50) +
xlim(range(ann.p_SD, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_SD, col = "red") + #plotting our tree's mean ANN
xlab("ANN") +
theme_classic()
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] < ann.p){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
## Version of ANN analysis controlling for the river with on and inside the river
#ANN analysis controlling for river
n <- 599L #defines the number of simulations
ann.r <- vector(length = n) #creates the empty object that we can store ANN values in
for (i in 1:n){
rand.p <- rpoint(n=length(SD_fixed_field_data_processed_sf), f = as.im(st_rasterize(river_SD_trans))) #assigns a random point for the number of trees in SD favoring placements in the river raster
ann.r[i] <- mean(nndist(rand.p, k=1)) #for each simulated random distribution of points it calculates the mean ANN across all of the trees
} #for the length of the number of points at LM, it assigns a random point within the raster while controlling for the river
plot(rand.p)
#adding the UTM 12 crs to rand.p
rand.p.crs <- rand.p %>%
st_as_sf()%>%
st_set_crs(26912)
#plotting the randomly generated points, tree points, and river raster
ggplot()+
geom_stars(data=st_rasterize(river_SD_trans))+ #plotting the river raster
geom_sf(data=SD_fixed_field_data_processed_sf, aes(col = "red"))+ #plotting the tree points
geom_sf(data=rand.p.crs, fill = NA) #plotting the random points
as_tibble(ann.r) %>% #turning the ann.r vector as a tibble
ggplot()+
geom_histogram(aes(x = value), fill = "skyblue", color = "black", bins = 50) +
xlim(range(ann.p_SD, ann.r)) + #setting the range of the graph to include both the simulated ANN and our tree's mean ANN
geom_vline(xintercept=ann.p_SD, col = "red", size = 1.2) + #plotting our tree's mean ANN
xlab("Average Nearest Neighbor (ANN)") +
theme_classic()+
theme(axis.text=element_text(size=15), axis.title.x =element_text(size= 15),
axis.title.y =element_text(size= 15))
#calculating pseudo p-value for
total = 0 #set empty vaue
for (i in 1:length(ann.r)){
if (ann.r[i] < ann.p){
total = total + 1
}
} #add number of values of in the random set of ANN values that are less than our mean ANN
(total / length(ann.r)) #the proportion of random ANNs that are less than our ANN
#### PPM analysis ####
#Test for LM
#creating the image of the distance to river stars
dist_near_river_buffer_LM_inverse_im <- as.im(dist_near_river_buffer_LM_inverse)
#Alternative hypothesis, seeing if the distance to the river's edge influences the tree point placement
PPM1 <- ppm(Q = as.ppp(LM_fixed_field_data_processed_sf) ~ dist_near_river_buffer_LM_inverse_im) #as.im(dist_near_river_buffer_LM_inverse))
PPM1
#null hypothesis, no change in the trend of the points
PPM0 <- ppm(as.ppp(LM_fixed_field_data_processed_sf) ~ 1)
PPM0
#using a likelihood ratio test to compare the alternative and null models
anova(PPM0, PPM1, test="LRT")
#plotting the alternative model
plot(effectfun(PPM1, "dist_near_river_buffer_LM_inverse_im", se.fit = TRUE), main = "Distance to River of Las Matancitas",
ylab = "Quercus brandegeei Trees", xlab = "Inverse Distance to River", legend = FALSE)
#Test for LC
#creating the image of the distance to river stars
dist_near_river_buffer_LC_inverse_im <- as.im(dist_near_river_buffer_LC_inverse)
#Alternative hypothesis, seeing if the distance to the river's edge influences the tree point placement
PPM1 <- ppm(Q = as.ppp(LC_fixed_field_data_processed_sf) ~ dist_near_river_buffer_LC_inverse_im)
PPM1
#null hypothesis, no change in the trend of the points
PPM0 <- ppm(as.ppp(LC_fixed_field_data_processed_sf) ~ 1)
PPM0
#using a likelihood ratio test to compare the alternative and null models
anova(PPM0, PPM1, test="LRT")
#plotting the alternative model
plot(effectfun(PPM1, "dist_near_river_buffer_LC_inverse_im", se.fit = TRUE), main = "Distance to River of La Cobriza",
ylab = "Quercus brandegeei Trees", xlab = "Inverse Distance to River", legend = FALSE)
#Test for SD
#creating the image of the distance to river stars
dist_near_river_buffer_SD_inverse_im <- as.im(dist_near_river_buffer_SD_inverse)
#Alternative hypothesis, seeing if the distance to the river's edge influences the tree point placement
PPM1 <- ppm(Q = as.ppp(SD_fixed_field_data_processed_sf) ~ dist_near_river_buffer_SD_inverse_im)
PPM1
#null hypothesis, no change in the trend of the points
PPM0 <- ppm(as.ppp(SD_fixed_field_data_processed_sf) ~ 1)
PPM0
#using a likelihood ratio test to compare the alternative and null models
anova(PPM0, PPM1, test="LRT")
#plotting the alternative model
plot(effectfun(PPM1, "dist_near_river_buffer_SD_inverse_im", se.fit = TRUE), main = "Distance to River of San Dionisio",
ylab = "Quercus brandegeei Trees", xlab = "Inverse Distance to River", legend = FALSE)
# making examples of random point distributions vs. points only along the river's edge for presentation
points_box = sf::st_sample(SD_box_sf, size=50) #randomizing points onlu in population bbox
points_river = sf::st_sample(river_SD_trans_points, size=50) #randomizing points along river's edge
points <- st_as_sf(points, crs = 26912) #projecting the points
#plotting the randomized box points
ggplot()+
geom_sf(data = river_SD_trans)+
#geom_sf(data = SD_box_sf)+
geom_sf(data = points_box, size = 2)+
theme_classic()