-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR codes for sdm.txt
More file actions
199 lines (158 loc) · 6.34 KB
/
Copy pathR codes for sdm.txt
File metadata and controls
199 lines (158 loc) · 6.34 KB
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
#load raw data
mydata_bm<-read.csv('stlucia_obs.csv')
str(mydata_bm)
# remove erroneous coordinates, where either the latitude or
# longitude is missing
Data_nullbm <- subset(mydata_bm, (!is.na(latitude)) & (!is.na(longitude))) # '!' means the opposite logic value
cat(nrow(mydata_bm) - nrow(Data_nullbm), "records are removed")
# remove duplicated data based on latitude and longitude
dup_bm <- duplicated(Data_nullbm[c("latitude", "longitude")])
data_unique_bm <- Data_nullbm[!dup_bm, ]
cat(nrow(Data_nullbm) - nrow(data_unique_bm), "records are removed")
#add a new row with 1 to indicate presence
data_unique_bm$Presence<-'1'
#select only presence, latitude and longitude columns
Data_new_bm<-select(data_unique_bm, Presence, latitude, longitude)
write.csv(Data_new_bm, 'cleanData.csv')
#convert to spatial dataframe
coordinates(Data_new_bm)<-~longitude+latitude
#subset to training and test set(70/30)
smp_size_bm<- floor(0.7 *nrow(Data_new_bm))
train.data.bm<- sample(seq_len(nrow(Data_new_bm)), size = smp_size_bm)
Train_data_bm<-Data_new_bm[train.data.bm,]
Test_data_bm<-Data_new_bm[-train.data.bm,]
write.csv(Test_data_bm, 'TestData.csv')
write.csv(Train_data_bm, 'TrainData.csv')
#load tct sentinel developed in GEE
SentinelTCT<-stack('Sentinel_TCT_STLucia.tif')
#import canopy height data
CanopyLucia<-raster('Canopyheight.tif')
#separate the files into individual bands
Brightness<- SentinelTCT[[1]]
Greenness<- SentinelTCT[[2]]
Wetness<- SentinelTCT[[3]]
#resample canopy height raster
CanopyLucia1<-resample(CanopyLucia, Bright)
#export rasters
writeRaster(CanopyLucia1, 'Resample_canopy.tif')
writeRaster(Brightness, 'Brightness1.tif')
writeRaster(Greenness, 'Greenness1.tif')
writeRaster(Wetness, 'Wetness1.tif')
#stack all rasters to be used for prediction
Variables<-stack(CanopyLucia1, Greenness, Brightness, Wetness)
#extract values from raster to training data
presvals_bm <- extract(Variables, Train_data_bm)
#generate random points(absence) and extract raster values to points
backgr_bm <- randomPoints(Variables, 1500)
absvals_bm <- extract(Variables, backgr_bm)
#merge the presence ad absence dataframes
pb_bm <- c(rep(1, nrow(presvals_bm)), rep(0, nrow(absvals_bm)))
sdmdata_bm <- data.frame(cbind(pb_bm, rbind(presvals_bm, absvals_bm)))
head(sdmdata_bm)
summary(sdmdata_bm)
pairs(sdmdata_bm[,2:5], cex=0.1, fig=TRUE)
#set seeed so as make sure that we get the same results for randomization
set.seed(0)
group_bm <- kfold(Train_data_bm, 5)
pres_train_bm <- Train_data_bm[group_bm != 1, ]
pres_test_bm <- Train_data_bm[group_bm == 1, ]
set.seed(10)
backg_bm<- randomPoints(Variables[[1]], n=1700, extf = 1.25)
colnames(backg_bm) = c('lon', 'lat')
group_bm <- kfold(backg_bm, 5)
backg_train_bm <- backg_bm[group_bm != 1, ]
backg_test_bm <- backg_bm[group_bm == 1, ]
#remove null values
sdmdata_bmclean <- sdmdata_bm[complete.cases(sdmdata_bm),]
# Check for correlation
correlation = cor(sdmdata_bmclean[, c("brightness", "greenness", "wetness",
"Lucia_Canopyheight")], sdmdata_bmclean[, "pb_bm"])
install.packages('corrplot')
library(corrplot)
#check correlation between variables
corrplot (cor(sdmdata_bmclean[,c("brightness", "greenness", "wetness",
"Lucia_Canopyheight")]),
method = "number",
#type = "upper" # show only upper side
)
library(devtools)
devtools::install_git("https://gitup.uni-potsdam.de/macroecology/mecofun.git", force = TRUE)
library(mecofun)
# Names of our variables:
pred <- c('brightness', 'greenness', 'wetness', 'Lucia_Canopyheight')
#plot response curves
install.packages('spatialRF')
par(mfrow=c(1,4))
plot_response_curves(model = RF_bm)
response(RF_bm,predictors=pred,data=sdmdata_bmclean)#, cex.lab=1.5, col="black")
#Modelling
library(randomForest)
model_bm <- pb_bm ~ brightness + greenness + wetness + Lucia_Canopyheight
RF_bm <- randomForest(model_bm, data=sdmdata_bmclean, na.action=na.roughfix)
print(RF_bm)
importance(RF_bm)
varImpPlot(RF_bm)
#generate dataframes for testing
testpres_bm <- data.frame( extract(Variables, pres_test_bm))
testbackg_bm <- data.frame( extract(Variables, backg_test_bm) )
#outline accuracy results
erf_bm <- evaluate(testpres_bm, testbackg_bm, RF_bm)
erf_bm
threshold(erf_bm)
plot(erf_bm, 'ROC')
par(mfrow=c(1, 1))
density(erf_bm)
boxplot(erf_bm, col=c('blue', 'red'))
#map prediction
pr_bm <- predict(Variables, RF_bm)
writeRaster(pr_bm, 'RandomForest.tif')
plot(pr_bm, main='Random Forest regression for Butterfly and Moth')
#generate random points for all study areas
#Generate rando points from protected, nonprotected
#and agroforested area
nonfrstpts<-randomPoints(protKK, n=100)
nonfrstpts11<-data.frame(raster::extract(Kk_CS, nonfrstpts))
#reproduce the code and combine all dataframes
#combine all data frames
wholedatanew1<-rbind(
nonfrstdataonly11,
agrodataonly11,
Alldataonly11,
noncbsdmfi1,
agrocbsdmfi12,
frstcbsdmfi11)
#compute summary statistics by groups
library(dplyr)
group_by(wholedatanew1, Group) %>%
summarise(
#count = n(),
#mean = mean(RasterVals, na.rm = TRUE),
sd = sd(RasterVals, na.rm = TRUE),
median = median(RasterVals, na.rm = TRUE),
IQR = IQR(RasterVals, na.rm = TRUE),
Q1 = quantile(RasterVals,0.25, na.rm=TRUE),
Q3 = quantile(RasterVals,0.75, na.rm=TRUE))
#plot mean plots to show different classes
library(ggpubr)
ggboxplot(wholedatanew1, x = "Group", y = "RasterVals",
color = "Group", palette = c('#000000','#000000','#000000',
'#000000','#000000'),
add = c("mean_se", "jitter"),
order = c('NP_KK',
AF_KK',
'P_KK',
'SBNP_KK',
'SBAF_KK',
'SBP_KK',
'NP_SA',
'P_SA',
'DFNP_SA',
'DFP_SA'),
ylab = "Insect biodiversity status(0-1)", xlab = "")
#perform kruskas wallis test
#data is a non normal distribution
kruskal.test(RasterVals ~ Group, data = (wholedatanew1)
#Perform post hoc test to identify classes that
#have significant differences
pairwise.wilcox.test((wholedatanew1$RasterVals, (wholedatanew1$Group,
p.adjust.method = "BH")