forked from jesusNPL/LargeScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
389 lines (389 loc) · 15.4 KB
/
.Rhistory
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
require(maptools)
require(rgdal)
require(sp)
require(rangeBuilder)
library(spdep)
library(spdep)
library(ncf)
require(geiger)
require(dismo)
library(letsR)
oaks <- read.table("Data/live.oaks.txt", header = TRUE)
oaks <- read.table("Data/OCC/live.oaks.txt", header = TRUE)
head(oaks)
tail(oaks)
data(wrld_simpl)
{plot(oaks[c(2:3)], col = "blue", pch = 19)
plot(wrld_simpl, add = TRUE)}
unique(oaks$Species)
que_vir <- subset(oaks, oaks$Species == "Quercus_virginiana")
{plot(que_vir$Longitude, que_vir$Latitude, pch = 15)
plot(wrld_simpl, add = TRUE)}
oaks_dups <- duplicated(oaks[, c(2:3)])
### NOTE: the function "duplicated" returns the results of a logical test (e.g. TRUE or FALSE)
# How many are duplicates?
length(which(oaks_dups == TRUE))
# How many are NOT duplicates?
length(which(oaks_dups == FALSE))
# Keep only those lines that are not duplicates
oaks_dups_row <- which(oaks_dups == TRUE)
# What's the size? That is, how many points are duplicates
length(oaks_dups_row)
# Create another object withoyt the duplicate records
oaks_nodups <- oaks[-oaks_dups_row,]
# What are the dimensions of the new object?
dim(oaks_nodups)
# Take a look at the first rows of data
head(oaks_nodups)
{plot(oaks$Longitude, oaks$Latitude, pch = 19, col = "red", cex = 2)
points(oaks_nodups$Longitude, oaks_nodups$Latitude, pch = 16, col = "black")}
# create a polygon around the species' records
oaks_hull <- convHull(oaks_nodups[, c(2:3)])
# create a polygon around the species' records
oaks_hull <- convHull(oaks_nodups[, c(2:3)])
# Plot the created polygon
{plot(oaks_hull)
points(oaks_nodups$Longitude, oaks_nodups$Latitude, pch = 16, col = "black")}
que_vir <- subset(oaks_nodups, oaks_nodups$Species == "Quercus_virginiana")
que_vir_hull <- convHull(que_vir[, c(2:3)])
{plot(que_vir_hull)
points(que_vir$Longitude, que_vir$Latitude, pch = 16, col = "black")}
# Plot all live oaks
{plot(oaks_hull)
points(oaks_nodups$Longitude, oaks_nodups$Latitude, pch = 16, col = "black")
# Plot only Quecus virginiana
plot(que_vir_hull, add = TRUE)
points(que_vir$Longitude, que_vir$Latitude, pch = 16, col = "red")
# Add world maps
plot(wrld_simpl, add = TRUE)}
que_vir_alphahull <- getDynamicAlphaHull(que_vir, fraction = 0.95,
coordHeaders = c("Longitude", "Latitude"),
clipToCoast = 'no')[[1]]
require(rangeBuilder)
install.packages("rangeBuilder")
require(rangeBuilder)
library(spdep)
library(ncf)
require(geiger)
require(dismo)
library(letsR)
que_vir_alphahull <- getDynamicAlphaHull(que_vir, fraction = 0.95,
coordHeaders = c("Longitude", "Latitude"),
clipToCoast = 'no')[[1]]
{plot(que_vir_alphahull, lwd = 2, col = "red")
plot(que_vir_hull, add = TRUE, lwd = 2, lty = 2)
points(que_vir$Longitude, que_vir$Latitude, pch = 16, col = "green")
plot(wrld_simpl, add = TRUE, lwd = 2)}
?readOGR
franges <- readOGR(ddsn = "Data/Franges", layer = "Furnarii_ranges_geo")
franges <- readOGR(dsn = "Data/Franges", layer = "Furnarii_ranges_geo")
franges <- readOGR(dsn = "Data/Franges", layer = "Furnarii_ranges_geo")
str(franges)
franges <- readOGR(dsn = "Data/Franges", layer = "Furnarii_ranges_geo")
head(franges@data)
neo_ras <- raster()
# Set the raster "extent"
extent(neo_ras) <- extent(franges)
res(neo_ras) <- 1
neo_ras
values(neo_ras) <- 0
f_sr_raster <- rasterize(franges, neo_ras, field = "SCINAME",
fun = function(x,...){length(unique(na.omit(x)))})
library(rworldmap)
data(wrld_simpl)
{plot(oaks[c(2:3)], col = "blue", pch = 19)
plot(countriesCoarse, add = TRUE)}
{plot(que_vir$Longitude, que_vir$Latitude, pch = 15)
plot(countriesCoarse, add = TRUE)}
# Plot all live oaks
{plot(oaks_hull)
points(oaks_nodups$Longitude, oaks_nodups$Latitude, pch = 16, col = "black")
# Plot only Quecus virginiana
plot(que_vir_hull, add = TRUE)
points(que_vir$Longitude, que_vir$Latitude, pch = 16, col = "red")
# Add world maps
plot(countriesCoarse add = TRUE)}
# Plot all live oaks
{plot(oaks_hull)
points(oaks_nodups$Longitude, oaks_nodups$Latitude, pch = 16, col = "black")
# Plot only Quecus virginiana
plot(que_vir_hull, add = TRUE)
points(que_vir$Longitude, que_vir$Latitude, pch = 16, col = "red")
# Add world maps
plot(countriesCoarse, add = TRUE)}
{plot(que_vir_alphahull, lwd = 2, col = "red")
plot(que_vir_hull, add = TRUE, lwd = 2, lty = 2)
points(que_vir$Longitude, que_vir$Latitude, pch = 16, col = "green")
plot(countriesCoarse, add = TRUE, lwd = 2)}
{plot(f_sr_raster)
plot(countriesCoarse, add = T)}
#change the color scale
colfuncYellows <- colorRampPalette(c("#d7191c", "#fdae61", "#ffffbf", "#abd9e9",
"#2c7bb6"))
{plot(f_sr_raster, col = rev(colfuncYellows(100)), axes = FALSE, box = FALSE,
zlim = c(minValue(f_sr_raster), maxValue(f_sr_raster)),
xlab = "Furnariides richness", legend.width = 2)
plot(countriesCoarse, add = T)}
head(franges@data)
f_MRD_raster <- rasterize(franges, neo_ras, field = "RD", fun = mean)
{plot(f_MRD_raster)
plot(countriesCoarse, add = T)}
{plot(f_MRD_raster, col = rev(colfuncYellows(100)), axes = FALSE, box = FALSE,
zlim = c(minValue(f_MRD_raster), maxValue(f_MRD_raster)),
xlab = "Furnariides mean root distance", legend.width = 2)
plot(countriesCoarse, add = T)}
{par(mfrow = c(1, 2))
plot(f_sr_raster, col = rev(colfuncYellows(100)), axes = FALSE, box = FALSE,
zlim = c(minValue(f_sr_raster), maxValue(f_sr_raster)),
xlab = "Furnariides richness", legend.width = 2)
plot(f_MRD_raster, col = rev(colfuncYellows(100)), axes = FALSE, box = FALSE,
zlim = c(minValue(f_MRD_raster), maxValue(f_MRD_raster)),
xlab = "Furnariides mean root distance", legend.width = 2)}
cor.test(values(f_sr_raster), values(f_MRD_raster))
cor.test(values(f_sr_raster), values(f_MRD_raster))
plot(values(f_sr_raster), values(f_MRD_raster))
plot(log(values(f_sr_raster)), log(values(f_MRD_raster)), xlab = "SR", ylab = "MRD")
plot(values(f_sr_raster), values(f_MRD_raster), xlab = "SR", ylab = "MRD")
{plot(values(f_sr_raster) ~ values(f_MRD_raster), xlab = "SR", ylab = "MRD")
abline()}
obj <- lm(values(f_MRD_raster) ~ values(f_sr_raster))
cor.test(values(f_sr_raster), values(f_MRD_raster))
{plot(values(f_sr_raster) ~ values(f_MRD_raster), xlab = "SR", ylab = "MRD")
abline(obj)}
{plot(values(f_MRD_raster) ~ values(f_sr_raster), xlab = "SR", ylab = "MRD")
abline(obj)}
{plot(values(f_MRD_raster) ~ values(f_sr_raster), xlab = "SR", ylab = "MRD")
abline(obj, col = "red", lwd = 2)}
# 2º degrees
neo_ras_2dg <- raster()
# Set the raster "extent"
extent(neo_ras_2dg) <- extent(franges)
res(neo_ras_2dg) <- 2
neo_ras_2dg
values(neo_ras_2dg) <- 0
# 4º degrees
neo_ras_4dg <- raster()
# Set the raster "extent"
extent(neo_ras_4dg) <- extent(franges)
res(neo_ras_4dg) <- 4
neo_ras_4dg
values(neo_ras_4dg) <- 0
# Furnariides at 2º of long-lat
f_sr_2dg_raster <- rasterize(franges, neo_ras_2dg, field = "SCINAME", fun = function(x,...){length(unique(na.omit(x)))})
# Furnariides at 4º of long-lat
f_sr_4dg_raster <- rasterize(franges, neo_ras_4dg, field = "SCINAME", fun = function(x,...){length(unique(na.omit(x)))})
{par(mfrow = c (1, 3))
plot(f_sr_raster, main = "Furnariides richness 1dg")
plot(countriesCoarse, add = T)
plot(f_sr_2dg_raster, main = "Furnariides richness 2dg")
plot(countriesCoarse, add = T)
plot(f_sr_4dg_raster, main = "Furnariides richness 4dg")
plot(countriesCoarse, add = T)}
bio1 <- raster("Data/Envi/bio1.bil")
bio1
bio12 <- raster("Data/Envi/bio12.bil")
bio12
{par(mfrow = c(2, 1))
plot(bio1)
plot(bio12)}
bio1_neo <- crop(bio1, extent(franges))
bio12_neo <- crop(bio12, extent(franges))
{par(mfrow = c(1, 2))
plot(bio1_neo, main = "Annual Mean Temperature")
plot(bio12_neo, main = "Annual Precipitation")}
f_ras_coords <- xyFromCell(f_sr_raster, 1:length(values(f_sr_raster)))
f_ras_bio1 <- extract(bio1_neo, f_ras_coords)
f_ras_bio12 <- extract(bio12_neo, f_ras_coords)
f_ras_rich <- values(f_sr_raster)
fdata <- na.omit(data.frame(f_ras_coords, f_ras_rich, f_ras_bio1, f_ras_bio12))
#f_ras_rich_noNA <- ifelse(is.na(f_ras_rich), 0, f_ras_rich)
#f_ras_bio1_noNA <- ifelse(is.na(f_ras_bio1), 0, f_ras_bio1)
#f_ras_bio12_noNA <- ifelse(is.na(f_ras_bio12), 0, f_ras_bio12)
#fdata_noNA <- data.frame(f_ras_coords, f_ras_rich_noNA, f_ras_bio1_noNA,
# f_ras_bio12_noNA)
cor.test(fdata$f_ras_rich, fdata$f_ras_bio1)
cor.test(fdata$f_ras_rich, fdata$f_ras_bio12)
{par(mfrow = c(1, 2))
plot(fdata$f_ras_bio1, fdata$f_ras_rich, xlab = "Bio 1", ylab = "Richness")
plot(fdata$f_ras_bio12, fdata$f_ras_rich, xlab = "Bio 12", ylab = "Richness")}
fols <- lm(f_ras_rich ~ f_ras_bio1 + f_ras_bio12, data = fdata)
summary(fols)
fols2 <- lm(f_ras_rich ~ f_ras_bio1 + f_ras_bio12 + values(f_MRD_raster), data = fdata)
f_sr_raster
f_MRD_raster
f_ras_mrd <- values(f_MRD_raster)
f_ras_bio1 <- extract(bio1_neo, f_ras_coords)
f_ras_bio12 <- extract(bio12_neo, f_ras_coords)
f_ras_rich <- values(f_sr_raster)
f_ras_mrd <- values(f_MRD_raster)
fdata <- na.omit(data.frame(f_ras_coords, f_ras_rich, f_ras_mrd, f_ras_bio1, f_ras_bio12))
#f_ras_rich_noNA <- ifelse(is.na(f_ras_rich), 0, f_ras_rich)
#f_ras_bio1_noNA <- ifelse(is.na(f_ras_bio1), 0, f_ras_bio1)
#f_ras_bio12_noNA <- ifelse(is.na(f_ras_bio12), 0, f_ras_bio12)
#fdata_noNA <- data.frame(f_ras_coords, f_ras_rich_noNA, f_ras_bio1_noNA,
# f_ras_bio12_noNA)
fols2 <- lm(f_ras_rich ~ f_ras_bio1 + f_ras_bio12 + f_ras_mrd, data = fdata)
summary(fols2)
autocor_SR <- ncf::correlog(fdata$x, fdata$y, z = fdata$f_ras_rich, na.rm = T,
increment = 1, resamp = 1)
{plot(autocor_SR$correlation[1:50], type = "b", pch = 1, cex = 1.2, lwd = 1.5,
ylim = c(-1, 1), xlab = "Distance class", ylab = "Moran's I", cex.lab = 1.2,
cex.axis = 1.2)
abline(h = 0)}
coords <- fdata[1:2]
coords <- as.matrix(coords)
nb1.5 <- dnearneigh(coords, 0, 1.5)
nb1.5.w <- nb2listw(nb1.5, glist = NULL, style = "W", zero.policy = TRUE)
residuals_ols <- residuals(fols2)
plot(residuals_ols)
autocor_ols_res <- correlog(fdata$x, fdata$y, z = residuals(fols),
increment = 1, resamp = 1)
{plot(autocor_ols_res$correlation[1:50], type = "b", pch = 1, cex = 1.2, lwd = 1.5,
ylim = c(-0.5, 1), xlab = "distance", ylab = "Moran's I", cex.lab = 1.5,
cex.axis = 1.2)
abline(h = 0)
title(main = "OLS residuals", cex = 1.5)}
{par(mfrow = c(2, 1))
plot(autocor_SR$correlation[1:50], type = "b", pch = 1, cex = 1.2, lwd = 1.5,
ylim = c(-1, 1), xlab = "Distance class", ylab = "Moran's I", cex.lab = 1.2,
cex.axis = 1.2)
abline(h = 0)
title(main = "OLS model", cex = 1.5)
plot(autocor_ols_res$correlation[1:50], type = "b", pch = 1, cex = 1.2, lwd = 1.5,
ylim = c(-0.5, 1), xlab = "Distance class", ylab = "Moran's I", cex.lab = 1.5,
cex.axis = 1.2)
abline(h = 0)
title(main = "OLS residuals", cex = 1.5)}
sar_nb1.5.w <- errorsarlm(fols, listw = nb1.5.w, data = fdata, quiet = FALSE,
zero.policy = TRUE)
summary(sar_nb1.5.w)
residuals_sar_nb1.5.w <- residuals(sar_nb1.5.w)
install.packages("spatialreg")
sar_nb1.5.w <- errorsarlm(fols, listw = nb1.5.w, data = fdata, quiet = FALSE,
zero.policy = TRUE)
?errorsarlm
sar_nb1.5.w <- errorsarlm(fols, listw = nb1.5.w, data = fdata, quiet = FALSE,
zero.policy = TRUE, na.action = na.exclude)
summary(sar_nb1.5.w)
residuals_sar_nb1.5.w <- residuals(sar_nb1.5.w)
require(spatialreg)
autocor_sar_nb1.5.w <- correlog(fdata$x, fdata$y, z = residuals(sar_nb1.5.w),
na.rm = T, increment = 1, resamp = 1)
{plot(autocor_sar_nb1.5.w$correlation[1:50], type = "b", pch = 4, cex = 1.2, lwd = 1.5,
ylim = c(-0.5, 1), xlab = "distance", ylab = "Moran's I", cex.lab = 1.5,
cex.axis = 1.2)
abline(h = 0)
title(main = "Correlogram SARerr", cex = 1.5)}
{par(mfrow = c(2, 1))
plot(autocor_ols_res$correlation[1:50], type = "b", pch = 1, cex = 1.2, lwd = 1.5,
ylim = c(-0.5, 1), xlab = "distance", ylab = "Moran's I", cex.lab = 1.5,
cex.axis = 1.2)
abline(h = 0)
title(main = "OLS residuals", cex = 1.5)
plot(autocor_sar_nb1.5.w$correlation[1:50], type = "b", pch = 4, cex = 1.2, lwd = 1.5,
ylim = c(-0.5, 1), xlab = "distance", ylab = "Moran's I", cex.lab = 1.5,
cex.axis = 1.2)
abline(h = 0)
title(main = "Correlogram SARerr", cex = 1.5)}
summary(sar_nb1.5.w)
sar_nb1.5.w <- errorsarlm(fols2, listw = nb1.5.w, data = fdata, quiet = FALSE,
zero.policy = TRUE, na.action = na.exclude)
summary(sar_nb1.5.w)
residuals_sar_nb1.5.w <- residuals(sar_nb1.5.w)
autocor_sar_nb1.5.w <- correlog(fdata$x, fdata$y, z = residuals(sar_nb1.5.w),
na.rm = T, increment = 1, resamp = 1)
{plot(autocor_sar_nb1.5.w$correlation[1:50], type = "b", pch = 4, cex = 1.2, lwd = 1.5,
ylim = c(-0.5, 1), xlab = "distance", ylab = "Moran's I", cex.lab = 1.5,
cex.axis = 1.2)
abline(h = 0)
title(main = "Correlogram SARerr", cex = 1.5)}
{par(mfrow = c(2, 1))
plot(autocor_ols_res$correlation[1:50], type = "b", pch = 1, cex = 1.2, lwd = 1.5,
ylim = c(-0.5, 1), xlab = "distance", ylab = "Moran's I", cex.lab = 1.5,
cex.axis = 1.2)
abline(h = 0)
title(main = "OLS residuals", cex = 1.5)
plot(autocor_sar_nb1.5.w$correlation[1:50], type = "b", pch = 4, cex = 1.2, lwd = 1.5,
ylim = c(-0.5, 1), xlab = "distance", ylab = "Moran's I", cex.lab = 1.5,
cex.axis = 1.2)
abline(h = 0)
title(main = "Correlogram SARerr", cex = 1.5)}
summary(sar_nb1.5.w)
summary(fols2)
source("https://raw.githubusercontent.com/jesusNPL/BetaDivNA/master/SARr2.R")
SARr2(Lfull = sar_nb1.5.w$LL, Lnull = sar_nb1.5.w$logLik_lm.model, N = nrow(fdata))
require(picante)
require(ape)
ftree <- read.nexus("Data/Phylo/furnariiMCC.nex")
length(ftree$tip.label)
head(treeB$tip.label)
length(ftree$tip.label)
head(ftree$tip.label)
extent(franges)
?lets.presab
fpam <- lets.presab(franges, xmn = -110.0817, xmx = -34.78897, ymn = -55.98222, ymx = 29.0965 remove.cells = TRUE, remove.sp = TRUE, cover = 0.5, resol = 1, count = TRUE)
fpam <- lets.presab(franges, xmn = -110.0817, xmx = -34.78897, ymn = -55.98222, ymx = 29.0965, remove.cells = TRUE, remove.sp = TRUE, cover = 0.5, resol = 1, count = TRUE)
fpam <- lets.presab(franges, xmn = -110.0817, xmx = -34.78897, ymn = -55.98222, ymx = 29.0965, remove.cells = TRUE, remove.sp = TRUE, cover = 0.5, resol = 1, count = FALSE)
fpam$Presence_and_Absence_Matrix[1:10, 1:10]
class(fpam)
fpam$Presence_and_Absence_Matrix[1:10, 1:10]
class(fpam)
class(fpam)
fpam$Presence_and_Absence_Matrix[1:10, 1:10]
fpam$Species_name
fcom <- fpam$Presence_and_Absence_Matrix
fpam$Species_name
fcom <- fpam$Presence_and_Absence_Matrix
sppNames <- fpam$Species_name
sppNames <- gsub(" ", "_", sppNames)
sppNames
names(fcom) <- c("long", "lat", sppNames)
fcom[1:10, 1:10]
colnames(fcom) <- c("long", "lat", sppNames)
fcom[1:10, 1:10]
dim(fcom)
fcom <- fpam$Presence_and_Absence_Matrix
dim(fcom)
fpam <- lets.presab(franges, xmn = -110.0817, xmx = -34.78897, ymn = -55.98222, ymx = 29.0965, remove.cells = TRUE, remove.sp = TRUE, resol = 1, count = FALSE)
class(fpam)
fpam$Presence_and_Absence_Matrix[1:10, 1:10]
dim(fpam$Presence_and_Absence_Matrix)
fcom <- fpam$Presence_and_Absence_Matrix
dim(fcom)
sppNames <- fpam$Species_name
sppNames <- gsub(" ", "_", sppNames)
colnames(fcom) <- c("long", "lat", sppNames)
fcom[1:10, 1:10]
dim(fcom)
matched <- match.phylo.comm(phy = ftree, comm = fcom[, 3:654])
matched$phy
dim(matched$comm)
matched$phy
dim(matched$comm)
Fpd <- pd(matched$comm, matched$phy, include.root = FALSE) # Faith's PD
head(Fpd)
cor.test(Fpd$SR, Fpd$PD)
plot(Fpd$SR, Fpd$PD)
Fmpd <- mpd(matched$comm, cophenetic(matched$phy)) # Faith's PD
head(Fmpd)
Fmpd <- mpd(matched$comm, cophenetic(matched$phy)) # Faith's PD
head(Fmpd)
cophenetic(matched$phy)
Fmpd <- mpd(matched$comm, cophenetic(matched$phy)) # Faith's PD
head(Fmpd)
Fmpd
Fmntd <- mntd(matched$comm, cophenetic(matched$phy))
head(fmntd)
head(Fmntd)
head(Fmpd, 10)
head(Fmntd, 10)
cor.test(Fmpd$SR, Fmntd$PD)
cor.test(Fmpd, Fmntd)
plot(Fmpd, Fmntd)
# SESMPD
Fsesmpd <- ses.mpd(matched$comm, cophenetic(matched$phy), runs = 99)
?ses.mpd
head(Fsesmpd, 15)
# SESMNTD
Fsesmntd <- ses.mntd(matched$comm, cophenetic(matched$phy), runs = 99)
head(Fsesmntd, 15)