-
Notifications
You must be signed in to change notification settings - Fork 0
/
step4.R
205 lines (154 loc) · 5.53 KB
/
step4.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
# report of the data in area of applicability and DI
# just one csv
library(CAST)
library(caret)
library(sf)
library(devtools)
library(raster)
library(viridis)
library(ggplot2)
library(tidyverse)
library(terra)
library(geodata)
setwd("C:/Users/rocio/Desktop/PHD/1 year/Abruzzo")
# upload
sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa <- rast("sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa.tif")
plot(sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa)
# select aoa from the stack
sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa
### AOA
aoa_null <- sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa$AOA_null
aoa_biased <- sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa$AOA_biased
aoa_all <- sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa$AOA_all
# common pixels between null and biased
common_pixels <- sum(aoa_null[] == 1 & aoa_biased[] == 1, na.rm = TRUE)
dev.off()
par(mfrow=c(2,1))
plot(aoa_null)
plot(aoa_biased)
# exclusive null
exclusive_null <- sum(aoa_null[] == 1 & aoa_biased[] == 0, na.rm = TRUE)
# exclusive biased
exclusive_biased <- sum(aoa_null[] == 0 & aoa_biased[] == 1, na.rm = TRUE)
# pixels of all
count_all <- sum(aoa_all[] == 1, na.rm = TRUE)
# results
common_pixels
exclusive_null
exclusive_biased
count_all
# function for pixel stat
analyze_AOA <- function(raster_stack, id) {
# raster AOA
aoa_null <- raster_stack$AOA_null
aoa_biased <- raster_stack$AOA_biased
aoa_all <- raster_stack$AOA_all
# pixels
common_pixels <- sum(aoa_null[] == 1 & aoa_biased[] == 1, na.rm = TRUE)
exclusive_null <- sum(aoa_null[] == 1 & aoa_biased[] == 0, na.rm = TRUE)
exclusive_biased <- sum(aoa_null[] == 0 & aoa_biased[] == 1, na.rm = TRUE)
count_all <- sum(aoa_all[] == 1, na.rm = TRUE)
# data frame
result <- data.frame(
ID = id,
Common_Pixels = common_pixels,
Exclusive_Null = exclusive_null,
Exclusive_Biased = exclusive_biased,
Count_All = count_all
)
return(result)
}
# stats
aoa_analysis <- analyze_AOA(sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa, id = "sp2_0.3_0.9_350")
print(aoa_analysis)
### DI
di_null <- sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa$DI_null
di_biased <- sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa$DI_biased
par(mfrow=c(2,1))
plot(di_null)
plot(di_biased)
# DI difference
difference_DI <- di_null - di_biased
# negative values: DI_biased > DI_null
summary(difference_DI)
# dataframe
difference_DI_df <- as.data.frame(difference_DI, xy = TRUE)
# plot
ggplot(difference_DI_df, aes(x = x, y = y, fill = DI_null)) +
geom_tile() +
scale_fill_gradient2(
low = "red", high = "blue", mid = "white", midpoint = 0,
limits = c(min(difference_DI[]), max(difference_DI[])),
name = "Difference in DI"
) +
theme_minimal() +
coord_equal() +
labs(title = "Difference in DI between AOA_null and AOA_biased")
# DI stats
analyze_DI <- function(raster_stack, species_id) {
# DI null DI biased
di_null <- raster_stack$DI_null
di_biased <- raster_stack$DI_biased
# difference
difference_DI <- di_null - di_biased
# na.remove
total_pixels <- sum(!is.na(values(difference_DI)))
# DI null > DI biased
pixels_null_greater <- sum(values(di_null) > values(di_biased), na.rm = TRUE)
# DI_null < DI_biased
pixels_biased_greater <- sum(values(di_null) < values(di_biased), na.rm = TRUE)
# Pixel invariati (DI_null == DI_biased)
unchanged_pixels <- sum(values(di_null) == values(di_biased), na.rm = TRUE)
# %
percent_null_greater <- (pixels_null_greater / total_pixels) * 100
percent_biased_greater <- (pixels_biased_greater / total_pixels) * 100
percent_unchanged <- (unchanged_pixels / total_pixels) * 100
# mean and sd
mean_null <- mean(values(di_null), na.rm = TRUE)
mean_biased <- mean(values(di_biased), na.rm = TRUE)
sd_null <- sd(values(di_null), na.rm = TRUE)
sd_biased <- sd(values(di_biased), na.rm = TRUE)
# data frame
result <- data.frame(
Species_ID = species_id,
Mean_DI_null = mean_null,
SD_DI_null = sd_null,
Mean_DI_biased = mean_biased,
SD_DI_biased = sd_biased,
Pixels_null_greater = pixels_null_greater,
Percent_null_greater = percent_null_greater,
Pixels_biased_greater = pixels_biased_greater,
Percent_biased_greater = percent_biased_greater,
Unchanged_pixels = unchanged_pixels,
Percent_unchanged = percent_unchanged
)
return(result)
}
# example
species_id <- "sp2_0.3_0.9_350" # ID della specie
di_analysis <- analyze_DI(sp2_sp_prev0.3_sample_prev0.9_nocc350_aoa, species_id)
print(di_analysis)
# csv
write.csv(di_analysis, "DI_analysis_species.csv", row.names = FALSE)
# georg lm
coords <- difference_DI_df %>% dplyr::select(Longitude = x, Latitude = y)
data.wgs <- SpatialPointsDataFrame(
coords = coords,
data = difference_DI_df,
proj4string = CRS("+proj=eck4")
)
mod0 = mgcv::gam(DI_null ~ 1 + s(Longitude, Latitude, bs = "sos"), method = "REML", data = data.wgs)
summary(mod0)xx
# https://jakubnowosad.com/posts/2024-10-13-spatcomp-bp1/
# from raster to vector: how to show the dissimilarity info
mod = lm(values(di_biased, na.rm = TRUE) ~ values(di_null, na.rm = TRUE))
summary(mod)
ggplot() +
geom_point(aes(x = values(di_biased, na.rm = TRUE), y = values(di_null, na.rm = TRUE))) +
geom_smooth(aes(x = values(di_biased, na.rm = TRUE), y = values(di_biased, na.rm = TRUE) - values(di_null, na.rm = TRUE)),
method = "lm", color = "darkgreen") +
geom_smooth(aes(x = values(di_biased, na.rm = TRUE), y = values(di_null, na.rm = TRUE)),
method = "lm") +
geom_smooth(aes(x = c(0:7), y = c(0:7)), color = "red", method = "lm") +
theme_minimal() +
labs(x = "DI biased", y = "DI null")