Models = c("Model0", "Model1",
"Model2", "Model3",
"Model4", "Model5",
"Model6")
Fixed = c("W (Course spatial structure only) ",
"f(NN) + f(mTcm) + Pop + CTI + Pwq + W",
"f(NN) + mTcm + Pop + CTI + Pwq + W",
"NN + mTcm + Pop + CTI + Pwq + W",
"f(mTcm) + Pop + CTI + Pwq + W",
"f(NN) + f(mTcm) + Pop + CTI + Pwq + CoH + W",
"NN + mTcm + Pop + CTI + Pwq + CoH")
#Deviance information criteria
DICs = c(Model0$dic$dic, Model1$dic$dic,
Model2$dic$dic, Model3$dic$dic,
Model4$dic$dic, Model5$dic$dic,
Model6$dic$dic)
#Watanabe-Akaike information criteria
WAICs = c(Model0$waic$waic, Model1$waic$waic,
Model2$waic$waic, Model3$waic$waic,
Model4$waic$waic, Model5$waic$waic,
Model6$waic$waic)
#log Conditional Predictive Ordnance
LCPOs = c(-mean(log(Model0$cpo$cpo)), -mean(log(Model1$cpo$cpo)),
-mean(log(Model2$cpo$cpo)), -mean(log(Model3$cpo$cpo)),
-mean(log(Model4$cpo$cpo)), -mean(log(Model5$cpo$cpo)),
-mean(log(Model6$cpo$cpo)))
Model_mets = as.data.frame(cbind(Model = Models,
DIC = round(DICs, 2),
WAIC = round(WAICs, 2),
LCPO = round(LCPOs, 3),
COVARIATES = Fixed))
print.data.frame(Model_mets, right = FALSE)
## Model DIC WAIC LCPO COVARIATES
## 1 Model0 2288.54 2272.87 0.044 W (Course spatial structure only)
## 2 Model1 1952.85 1946.8 0.037 f(NN) + f(mTcm) + Pop + CTI + Pwq + W
## 3 Model2 1968.62 1963.33 0.038 f(NN) + mTcm + Pop + CTI + Pwq + W
## 4 Model3 2061.03 2064.13 0.04 NN + mTcm + Pop + CTI + Pwq + W
## 5 Model4 2064.75 2052.97 0.039 f(mTcm) + Pop + CTI + Pwq + W
## 6 Model5 1931.42 1927.74 0.037 f(NN) + f(mTcm) + Pop + CTI + Pwq + CoH + W
## 7 Model6 2201.34 2205.32 0.042 NN + mTcm + Pop + CTI + Pwq + CoH
#ModComp = xtable(Model_mets)
#print(ModComp, include.rownames = FALSE,
# floating.environment = "sidewaystable")
SelMod.tab = as.data.frame(Model5$summary.fixed)
SelMod.tab2 = cbind(SelMod.tab[,1:3], SelMod.tab[,5])
names(SelMod.tab2) = c("Mean", "sd", "2.5% Q", "97.5% Q")
print.data.frame(SelMod.tab2, right = FALSE)
## Mean sd 2.5% Q 97.5% Q
## intercept0 -9.2260725 1.53523445 -12.29672012 -6.2572756
## Pop 0.3327789 0.15558055 0.04405673 0.6555362
## CTI -0.8210867 0.32813052 -1.46920598 -0.1807039
## Pwq 0.9616265 0.23321119 0.50039125 1.4169286
## CoH 0.7396538 0.09516221 0.55686433 0.9308740
#SelMod.tab2 = xtable(SelMod.tab2)
#print(SelMod.tab2, include.rownames = TRUE)
proj = inla.mesh.projector(mesh1,
dims=c(400, 800))
plotdata = inla.mesh.project(proj, Model0$summary.random$field0[,"mean"])
plotdata2 = inla.mesh.project(proj, Model5$summary.random$field0[,"mean"])
R600 = raster(nrows = 400, ncols = 800)
R601 = R600
values(R600) = plotdata
values(R601) = plotdata2
M600 = as.matrix(R600)
M601 = as.matrix(R601)
M0.spm = rotate90.matrix(M600) #rotation before rasterization
M0.spmr = raster(M0.spm)
extent(M0.spmr) = c(-180, 180, -90, 90)
proj4string(M0.spmr) = CRS(proj4string(Domain.r))
M1.spm = rotate90.matrix(M601) #rotation before rasterization
M1.spmr = raster(M1.spm)
extent(M1.spmr) = c(-180, 180, -90, 90)
proj4string(M1.spmr) = CRS(proj4string(Domain.r))
RandStack = stack(M0.spmr, M1.spmr)
names(RandStack) = c("A", "B")
rng = seq(-4, 13, 0.01)
cr = colorRampPalette(c("darkblue", "blue", "lightblue",
"yellow", "orange", "red", "darkred"),
bias = 1.5, space = "rgb")
Cp = levelplot(RandStack,
margin = FALSE,
#sub = "Density of Random Effects",
xlab = NULL, ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(-5, 13, 0.01),
labels=list(at=c(-4, 0, 4, 8, 12)),
labels=c("-4", "0", "4", "8", "12")),
panel = panel.levelplot, space = "right", par.strip.text = list(fontface='bold'),
col = cr, par.settings = list(axis.line = list(col = "black"),
strip.background = list(col = 'transparent'),
strip.border = list(col = 'transparent')),
scales = list(col = "black", cex = 2))
Cp +
latticeExtra::layer(sp.polygons(Countries, col = "black", lwd = 1))
plotdata = inla.mesh.project(proj, Model0$summary.random$field0[,"sd"])
plotdata2 = inla.mesh.project(proj, Model5$summary.random$field0[,"sd"])
R600 = raster(nrows = 400, ncols = 800)
R601 = R600
values(R600) = plotdata
values(R601) = plotdata2
M600 = as.matrix(R600)
M601 = as.matrix(R601)
M0.spm = rotate90.matrix(M600) #rotation before rasterization
M0.spmr = raster(M0.spm)
extent(M0.spmr) = c(-180, 180, -90, 90)
proj4string(M0.spmr) = CRS(proj4string(Domain.r))
M1.spm = rotate90.matrix(M601) #rotation before rasterization
M1.spmr = raster(M1.spm)
extent(M1.spmr) = c(-180, 180, -90, 90)
proj4string(M1.spmr) = CRS(proj4string(Domain.r))
RandStack = stack(M0.spmr, M1.spmr)
names(RandStack) = c("A", "B")
rng = seq(0, 4.4, 0.01)
cr = colorRampPalette(c("lightblue", "yellow", "darkred"),
space = "rgb")
Cp = levelplot(RandStack,
margin = FALSE,
#sub = "Standard Deviation",
xlab = NULL, ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(0, 4.4, 0.01),
labels=list(at=c(0, 1, 2, 3, 4)),
labels=c("0", "1", "2", "3", "4")),
panel = panel.levelplot, space = "right", par.strip.text = list(fontface='bold'),
col = cr, par.settings = list(axis.line = list(col = "black"),
strip.background = list(col = 'transparent'),
strip.border = list(col = 'transparent')),
scales = list(col = "black", cex = 2))
Cp +
latticeExtra::layer(sp.polygons(Countries, col = "black", lwd = 1))
Reduced survival in cold temperatures.
mic.df = as.data.frame(Model5$summary.random$mTcm[,1:6])
names(mic.df) = c("ID", "Mean", "sd", "Q025", "Q50", "Q975")
ggplot(mic.df, aes(ID*10, Mean)) +
geom_smooth(col = "black",
linetype= "solid",
method = "loess",
span = 0.25,
se = FALSE,
lwd = 1) +
geom_smooth(data = mic.df, aes(ID*10, Q025),
col = "grey",
method = "loess",
span = 0.25,
se = FALSE,
linetype= "dashed") +
geom_smooth(data = mic.df, aes(ID*10, Q975),
col = "grey",
method = "loess",
span = 0.25,
se = FALSE,
linetype= "dashed") +
geom_hline(yintercept = 0,
linetype = "dotted",
col = "red",
size = 1) +
geom_vline(xintercept = 0,
linetype = "dotted",
col = "red",
size = 1) +
xlab(expression("Limiting Temperature ("*~degree*C*" )")) +
ylab("Logit") +
theme_classic() +
theme(axis.text=element_text(size=16),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 20, vjust=-2))
Point at which logit becomes positive.
FindZero = function(X.df) {
Cross = which(diff(sign(X.df$Mean))!=0)
mTemp = (X.df$ID[Cross] + X.df$ID[Cross+1])/2
return(mTemp)
}
###Temperature Limit (degrees C) #3.85
FindZero(mic.df)[2]*10
## [1] 3.85
Accounting for the observed clustering pattern (possible related to dispersal ability).
mic.df = as.data.frame(Model5$summary.random$NN[,1:6])
names(mic.df) = c("ID", "Mean", "sd", "Q025", "Q50", "Q975")
Full.p = ggplot(mic.df, aes(ID*1000, Mean)) +
geom_smooth(method = "loess",
se = FALSE, col = "black",
linetype= "solid") +
geom_smooth(data = mic.df, aes(ID*1000, Q025),
method = "loess",
se = FALSE, col = "grey",
linetype= "dashed") +
geom_smooth(data = mic.df, aes(ID*1000, Q975),
method = "loess",
se = FALSE, col = "grey",
linetype= "dashed") +
geom_hline(yintercept = 0,
linetype = "dotted",
col = "red",
size = 1) +
geom_vline(xintercept = 0,
linetype = "dotted",
col = "red",
size = 1) +
xlim(c(0,28000)) +
xlab(NULL) +
ylab("Logit") +
ggtitle("A") +
theme_classic() +
theme(axis.text=element_text(size=16),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 20, vjust=-2),
plot.title = element_text(hjust = 0.5))
###Zoom in some
mic.df = as.data.frame(Model5$summary.random$NN[,1:6])
names(mic.df) = c("ID", "Mean", "sd", "Q025", "Q50", "Q975")
mic.df = mic.df %>%
filter(ID <= 2)
Zoom.p = ggplot(mic.df, aes(ID*1000, Mean)) +
geom_smooth(method = "loess",
se = FALSE, col = "black",
linetype= "solid") +
geom_smooth(data = mic.df, aes(ID*1000, Q025),
method = "loess",
se = FALSE, col = "grey",
linetype= "dashed") +
geom_smooth(data = mic.df, aes(ID*1000, Q975),
method = "loess",
se = FALSE, col = "grey",
linetype= "dashed") +
geom_hline(yintercept = 0,
linetype = "dotted",
col = "red",
size = 1) +
geom_vline(xintercept = 0,
linetype = "dotted",
col = "red",
size = 1) +
xlim(c(0,2000)) +
xlab("Distance to Nearest Neighbor (km)") +
ylab("Logit") +
ggtitle("B") +
theme_classic() +
theme(axis.text=element_text(size=16),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 20, vjust=-2),
plot.title = element_text(hjust = 0.5))
grid.arrange(Full.p, Zoom.p, ncol=1)
###Max Distance for dispersal #1585
FindZero(mic.df)[1]*1000
## [1] 1585
Function for plotting the density of the marginal terms.
ggplotmargin = function(x, type, effect, xlab, ylab = "Posterior Density",
int.value = c(value = 0, 5, 95),
color = c("red", "gray", "gray")){
xx = as.data.frame(inla.smarginal(x[[paste("marginals", type, sep=".")]][[effect]]))
out = ggplot(xx, aes(x, y)) + geom_line(size = 1) + ylab(ylab) + xlab(xlab) + theme_classic() + theme(axis.text=element_text(size=16),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 20, vjust=-2),
plot.title = element_text(hjust = 0.5)) +
if(length(int.value) == 0) int.value = 0
int.value = lapply(int.value, function(x) if(is.character(x))
type.convert(x, as.is = TRUE) else x)
int.value = lapply(int.value, function(x) if(is.character(x))
lapply(strsplit(x, "=")[[1]], type.convert, as.is = TRUE) else x)
nx = names(int.value)
if(!is.null(nx))
for(i in which(nx != "")) int.value[[i]] = list(nx[i], int.value[[i]])
int.value = sapply(int.value, function(x) {
if(is.numeric(x)) xx$x[which.max(cumsum(xx$y)/sum(xx$y) >= as.numeric(x/100))]
else switch(x[[1]],
mean = sum(xx$y*xx$x)/sum(xx$y),
median = xx$x[which.max(cumsum(xx$y)/sum(xx$y) >=.5)],
mode = xx$x[which.max(xx$y)],
value = x[[2]],
zero = 0)})
if(length(color) <= length(int.value)) color = rep(color, length = length(int.value))
for(i in 1:length(int.value)) out = out + geom_vline(xintercept = int.value[i], color = color[i])
out
}
Posterior marginal distribution of intercept, posterior marginal distribution to nominal variance of the random field, and posterior marginal distribution of the practical range.
plotI = ggplotmargin(Model5, type = "fixed",
effect = "intercept0", xlab = "Intercept")
mRF.df = as.data.frame(Model5$marginals.hy[[1]])
plotH1 = ggplot(mRF.df, aes(x, y)) +
geom_smooth(method = "loess",
se = FALSE, col = "black",
span = 0.15,
linetype= "solid") +
xlab(expression(phi)) +
ylab("Posterior Density") +
theme_classic() +
theme(axis.title.y = element_text(size = 18),
axis.title.x = element_text(size = 18))
result.field = inla.spde.result(Model5, "field0", spde)
mRF.df = as.data.frame(result.field$marginals.variance.nominal[[1]])
plotVN = ggplot(mRF.df, aes(x, y)) +
geom_smooth(method = "loess",
se = FALSE, col = "black",
span = 0.05,
linetype= "solid") +
xlab(expression(sigma[x]^2)) +
ylab("Posterior Density") +
theme_classic() +
theme(axis.title.y = element_text(size = 18),
axis.title.x = element_text(size = 18))
mRF.df = as.data.frame(result.field$marginals.range.nominal[[1]])
plotRng = ggplot(mRF.df, aes(x, y)) +
geom_smooth(method = "loess",
se = FALSE, col = "black",
span = 0.05,
linetype= "solid") +
xlab("Practical Range") +
ylab("Posterior Density") +
theme_classic() +
theme(axis.title.y = element_text(size = 18),
axis.title.x = element_text(size = 18))
grid.arrange(plotI, plotH1, plotVN, plotRng, ncol = 2)
Point estimates and 95% credible intervals for the course random spatial effect.
mRF.df = as.data.frame(Model0$summary.random$field0)[,1:6]
names(mRF.df) = c("ID", "Mean", "sd", "Q025", "Q50", "Q975")
mRF.df = arrange(mRF.df, Mean)
mRF.df$Index = 1:nrow(mRF.df)
SpCor1 = ggplot(mRF.df, aes(Index, Mean)) +
geom_smooth(method = "loess",
col = "black",
span = 0.25,
se = FALSE) +
geom_line(data = mRF.df,
aes(Index, Q025),
col = "grey") +
geom_line(data = mRF.df,
aes(Index, Q975),
col = "grey") +
geom_hline(yintercept = 0,
linetype = "dotted",
col = "red",
size = 1) +
scale_y_continuous(breaks=c(-15,-10,-5, 0, 5, 10, 15),
labels=c("-15","-10","-5", "0", "5", "10", "15")) +
#ylim(-16, 16) +
xlab(NULL) +
ylab("Spatial Field") +
ggtitle("A") +
theme_classic() +
theme(axis.text=element_text(size=16),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 20, vjust=-2),
plot.title = element_text(hjust = 0.5))
mRF.df = as.data.frame(Model5$summary.random$field0)[,1:6]
names(mRF.df) = c("ID", "Mean", "sd", "Q025", "Q50", "Q975")
mRF.df = arrange(mRF.df, Mean)
mRF.df$Index = 1:nrow(mRF.df)
SpCor2 = ggplot(mRF.df, aes(Index, Mean)) +
geom_smooth(method = "loess",
col = "black",
span = 0.25,
se = FALSE) +
geom_line(data = mRF.df,
aes(Index, Q025),
col = "grey") +
geom_line(data = mRF.df,
aes(Index, Q975),
col = "grey") +
geom_hline(yintercept = 0,
linetype = "dotted",
col = "red",
size = 1) +
xlab("Index") +
ylab("Spatial Field") +
ggtitle("B") +
theme_classic() +
theme(axis.text=element_text(size=16),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 20, vjust=-2),
plot.title = element_text(hjust = 0.5))
grid.arrange(SpCor1, SpCor2, ncol=1)
results = Model5
results$marginals.fixed$Pop[, 1] = results$marginals.fixed$Pop[, 1]
results$marginals.fixed$CTI[, 1] = results$marginals.fixed$CTI[, 1]
results$marginals.fixed$Pwq[, 1] = results$marginals.fixed$Pwq[, 1]
results$marginals.fixed$CoH[, 1] = results$marginals.fixed$CoH[, 1]
plotI = ggplotmargin(Model5, type = "fixed",
effect = "intercept0", xlab = "Intercept")
plot1 = ggplotmargin(results, type = "fixed",
effect = "Pop", xlab = "Pd")
plot2 = ggplotmargin(results, type = "fixed",
effect = "CTI", xlab = "CTI")
plot3 = ggplotmargin(results, type = "fixed",
effect = "Pwq", xlab = "Pq")
plot4 = ggplotmargin(results, type = "fixed",
effect = "CoH", xlab = "Ic")
Top.grd = grid.arrange(plot1, plot2, plot3, plot4, ncol=2)
Predicting the probability of occurrence globally using “Model1”.
Model.pred = inla(Frm5, #Formula for selcted model
data = inla.stack.data(CStack.global, spde=spde), #
family = "binomial",
verbose = TRUE,
control.fixed = list(prec = 0.1, prec.intercept = 0.1),
control.predictor = list(
A = inla.stack.A(CStack.global),
compute = TRUE,
link = 1),
control.mode = list(restart = TRUE, theta = theta5),
control.results = list(return.marginals.random = FALSE, #Turning off un-needed calculations
return.marginals.predictor = FALSE),
control.compute=list(dic = FALSE, cpo = FALSE, waic = FALSE))
summary(Model.pred)
##
## Call:
## c("inla(formula = Frm5, family = \"binomial\", data = inla.stack.data(CStack.global, ", " spde = spde), verbose = TRUE, control.compute = list(dic = FALSE, ", " cpo = FALSE, waic = FALSE), control.predictor = list(A = inla.stack.A(CStack.global), ", " compute = TRUE, link = 1), control.results = list(return.marginals.random = FALSE, ", " return.marginals.predictor = FALSE), control.fixed = list(prec = 0.1, ", " prec.intercept = 0.1), control.mode = list(restart = TRUE, ", " theta = theta5))")
##
## Time used:
## Pre-processing Running inla Post-processing Total
## 1.4447 35909.4810 1.6790 35912.6047
##
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## intercept0 -9.2230 1.5337 -12.2905 -9.2053 -6.2571 -9.1709 0
## Pop 0.3328 0.1556 0.0441 0.3266 0.6555 0.3138 0
## CTI -0.8209 0.3281 -1.4689 -0.8196 -0.1806 -0.8170 0
## Pwq 0.9616 0.2332 0.5004 0.9626 1.4168 0.9645 0
## CoH 0.7396 0.0951 0.5569 0.7381 0.9307 0.7353 0
##
## Random effects:
## Name Model
## field0 SPDE2 model
## mTcm RW1 model
## NN RW1 model
##
## Model hyperparameters:
## mean sd 0.025quant 0.5quant 0.975quant mode
## Range for field0 0.4347 0.1536 0.2160 0.4072 0.8102 0.3578
## Stdev for field0 0.9570 0.1937 0.6376 0.9361 1.3949 0.8949
## Precision for mTcm 0.8272 0.4519 0.2684 0.7250 1.9844 0.5591
## Precision for NN 0.5369 0.1186 0.3453 0.5226 0.8089 0.4946
##
## Expected number of effective parameters(std dev): 62.94(8.886)
## Number of equivalent replicates : 413.99
##
## Marginal log-Likelihood: -9393.83
## Posterior marginals for linear predictor and fitted values computed
Under Current Conditions(1950 - 2000)
I0 = inla.stack.index(CStack.global, "pred0") #Index to pull results
E0 = Model.pred$summary.linear.predictor[I0$data,] #pull results
Pred.pnts = LygPA
Pred.pnts$Cp = plogis(E0[,"mean"]) #exponentiate
M0p = rasterize(Pred.pnts, Domain.r, "Cp", #Rasterize predicted values
background = NA)
M0p = sum(M0p, Domain.r)
rng = seq(0, 1, 0.001)
cr0 = rev(colorRampPalette(c("maroon", "yellow", "dodgerblue"))(n = 299))
cr = colorRampPalette(c("tan", cr0),
bias = 0.6, space = "rgb")
Cp = levelplot(M0p,
margin = FALSE,
#sub = " Probability of Occurrence",
xlab = " ", ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(0, 1, 0.001),
labels=list(cex=1.5),
labels=list(at=c(0, 0.25, 0.5, 0.75, 1),
labels=c("0%", "25%", "50%", "75%", "100%")),
space = "bottom"),
par.settings = list(axis.line = list(col = "black"),
strip.background = list(col = 'transparent'),
strip.border = list(col = 'transparent')),
scales = list(cex = 2))
Cp +
latticeExtra::layer(sp.polygons(Countries, col = "dimgray", cex=2, lwd = 1))
As an alternative to “analytic” prediction, we explore results from a simple sum of linear components, to include the spatial random field and non-linear random walk results.
Pred.pnts = LygPA #Copy Points
pLoc = cbind(Pred.pnts$Long, Pred.pnts$Lat)
pLoc = inla.mesh.map(pLoc,
projection = "longlat",
inverse = TRUE)
Ap = inla.spde.make.A(mesh1, loc=pLoc) #project new points to mesh to obtain random field
Pred.pnts$RF = drop(Ap %*% Model5$summary.random$field0$mean) #Record the random field at each location
#Get the intercept
Pred.pnts$Int = Model5$summary.fix[1,1]
mydf = as.data.frame(Model5$summary.fixed) #Write results to data frame
#Find closest match for the mTcm RW1 results
Rw.lu = as.numeric(Model5$summary.random$mTcm[,"ID"])
Pred.pnts$RW = sapply(Pred.pnts$mTcm, function(x)which.min(abs(x - Rw.lu)))
#Write rw1 value to look up table and note rank/position
Rw.lu = as.data.frame(Model5$summary.random$mTcm[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 766, 1))
#Use rank/position to pull nearest closest effect size
Pred.pnts$mTcm_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RW, POS)]))
#Same as above, but for the NN RW1 result
Rw.lu = as.numeric(Model5$summary.random$NN[,"ID"])
Pred.pnts$RW = sapply(Pred.pnts$NN, function(x)which.min(abs(x - Rw.lu)))
#Write rw1 value to look up table and note rank/position
Rw.lu = as.data.frame(Model5$summary.random$NN[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 2189, 1))
#Use rank/position to pull nearest neighbor effect size
Pred.pnts$NN_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RW, POS)]))
#Sum intercept, random effect, random walk variables, and fixed effects.
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + mTcm_lu + NN_lu +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
LP = ifelse(is.na(LP), 0, LP),
LPe = plogis(LP))
Analytic and Summed Linear Component predictions are compared. First visually, then by correlation test.
Interpretation: Results are comparable. Use of the Summed Linear Components is a reasonable alternative to analytic computation, which can be computationally expensive and prone to numerical issues (because of combined spatial and dispersal random effects).
Global_LP = rasterize(Pred.pnts, Domain.r, "LPe",
background = NA)
Global_LP = sum(Global_LP, Domain.r)
CompPred.stk = stack(M0p, Global_LP)
names(CompPred.stk) = c("A", "B")
rng = seq(0, 1, 0.001)
cr0 = rev(colorRampPalette(c("maroon", "yellow", "dodgerblue"))(n = 299))
cr = colorRampPalette(c("tan", cr0),
bias = 0.7, space = "rgb")
Cp = levelplot(M0p,
margin = FALSE,
sub = " Probability of Occurrence",
xlab = NULL, ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(0, 1, 0.001),
labels=list(at=c(0, 0.25, 0.5, 0.75, 1),
labels=c("0%", "25%", "50%", "75%", "100%")),
panel = panel.levelplot, space = "right",
col = cr, par.settings = list(fontsize = list(text = 18))))
Cp +
latticeExtra::layer(sp.polygons(Countries, col = "black", lwd = 1))
Testing for correlation between prediction methods.
cor.test(values(M0p), values(Global_LP))
##
## Pearson's product-moment correlation
##
## data: values(M0p) and values(Global_LP)
## t = 8220.1, df = 24019, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.9998178 0.9998268
## sample estimates:
## cor
## 0.9998223
Pred.pnts = SECP.pnts #Copy Points
pLoc = cbind(Pred.pnts$Long, Pred.pnts$Lat)
pLoc = inla.mesh.map(pLoc,
projection = "longlat",
inverse = TRUE)
Ap = inla.spde.make.A(mesh1, loc=pLoc)
Pred.pnts$RF = drop(Ap %*% Model5$summary.random$field0$mean)
#Get CoH covariate
Pred.pnts$CoH = drop(Ap %*% ModelR$summary.random$fieldR$mean) #RElative density of Invasive Cohort Richness
Pred.pnts$Int = Model5$summary.fix[1,1]
mydf = as.data.frame(Model5$summary.fixed)
Rw.lu = as.numeric(Model5$summary.random$mTcm[,"ID"])
Pred.pnts$RW = sapply(Pred.pnts$mTcm, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$mTcm[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 766, 1))
Pred.pnts$mTcm_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RW, POS)]))
Rw.lu = as.numeric(Model5$summary.random$NN[,"ID"])
Pred.pnts$RW = sapply(Pred.pnts$NN, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$NN[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 2189, 1))
Pred.pnts$NN_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RW, POS)]))
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + mTcm_lu + NN_lu +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
LP = ifelse(is.na(LP), 0, LP),
LPe = plogis(LP))
#Unconstrained Dispersal
UnDisp = Model5$summary.random$NN[1, "mean"]
#Cuurent condition, unconstrained dispersal
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + mTcm_lu + UnDisp +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
LP = ifelse(is.na(LP), 0, LP),
LPeC = plogis(LP))
#Changing to Project Climate (Changing mTcm and Pwq)
Rw.lu = as.numeric(Model5$summary.random$mTcm[,"ID"])
Pred.pnts$RWF = sapply(Pred.pnts$mTcm70, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$mTcm[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 766, 1))
Pred.pnts$FmTcm_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RWF, POS)]))
#Constrained Dispersal
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + FmTcm_lu + NN_lu +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq70*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
FLP = ifelse(is.na(LP), 0, LP),
FLPe = plogis(LP))
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + FmTcm_lu + UnDisp +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq70*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
FLP = ifelse(is.na(LP), 0, LP),
FLPeD = plogis(LP))
Current Conditions 1950-2000.
SEUS_LP = rasterize(Pred.pnts, SEStates.r, "LPe",
background = NA)
SEUS_LP = sum(SEUS_LP, SEStates.r)
SEUS_LPb = rasterize(Pred.pnts, SEStates.r, "LPeC",
background = NA)
SEUS_LPb = sum(SEUS_LPb, SEStates.r)
SEUS8570 = rasterize(Pred.pnts, SEStates.r, "FLPe",
background = NA)
SEUS8570 = sum(SEUS8570, SEStates.r)
SEUS8570b = rasterize(Pred.pnts, SEStates.r, "FLPeD",
background = NA)
SEUS8570b = sum(SEUS8570b, SEStates.r)
SEPred.stk = stack(SEUS_LP, SEUS8570, SEUS_LPb, SEUS8570b)
names(SEPred.stk) = c("A", "B", "C", "D")
rng = seq(0, 1, 0.1)
cr0 = rev(colorRampPalette(c("maroon", "yellow", "dodgerblue"))(n = 299))
cr = colorRampPalette(c("tan", cr0),
space = "rgb")
Cp = levelplot(SEPred.stk,
margin = FALSE,
layout=c(2, 2),
#sub = " Probability of Occurrence",
xlab = " ", ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(0, 1, 0.001),
labels=list(cex=1.5),
labels=list(at=c(0, 0.25, 0.5, 0.75, 1),
labels=c("0%", "25%", "50%", "75%", "100%")),
space = "bottom"),
par.settings = list(axis.line = list(col = "black"),
strip.background = list(col = 'transparent'),
strip.border = list(col = 'transparent')),
scales = list(cex = 1.5))
Cp +
latticeExtra::layer(sp.polygons(StatesP, col = "dimgray", lwd = 1))
SE.obs = subset(LygPA, OBS == 1)
Cp.us = levelplot(SEUS_LP,
margin = FALSE,
#sub = " Probability of Occurrence",
xlab = " ", ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(0, 1, 0.001),
labels=list(cex=1.5),
labels=list(at=c(0, 0.25, 0.5, 0.75, 1),
labels=c("0%", "25%", "50%", "75%", "100%")),
space = "bottom"),
par.settings = list(axis.line = list(col = "black"),
strip.background = list(col = 'transparent'),
strip.border = list(col = 'transparent')),
scales = list(cex = 1.5)) +
latticeExtra::layer(sp.polygons(StatesP, col = "dimgray", lwd = 1)) +
latticeExtra::layer(sp.polygons(SE.obs, col = "black", pch = 16, cex = 0.5))
Cp.us
Pred.pnts = SEAsia.pnts #Copy Points
pLoc = cbind(Pred.pnts$Long, Pred.pnts$Lat)
pLoc = inla.mesh.map(pLoc,
projection = "longlat",
inverse = TRUE)
Ap = inla.spde.make.A(mesh1, loc=pLoc)
Pred.pnts$RF = drop(Ap %*% Model5$summary.random$field0$mean)
#Get CoH covariate
Pred.pnts$CoH = drop(Ap %*% ModelR$summary.random$fieldR$mean)
Pred.pnts$Int = Model5$summary.fix[1,1]
mydf = as.data.frame(Model5$summary.fixed)
Rw.lu = as.numeric(Model5$summary.random$mTcm[,"ID"])
Pred.pnts$RW = sapply(Pred.pnts$mTcm, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$mTcm[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 766, 1))
Pred.pnts$mTcm_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RW, POS)]))
Rw.lu = as.numeric(Model5$summary.random$NN[,"ID"])
Pred.pnts$RW = sapply(Pred.pnts$NN, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$NN[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 2189, 1))
Pred.pnts$NN_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RW, POS)]))
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + mTcm_lu + NN_lu +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
LP = ifelse(is.na(LP), 0, LP),
LPe = plogis(LP))
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + mTcm_lu + UnDisp +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
LP = ifelse(is.na(LP), 0, LP),
LPeC = plogis(LP))
#Changing to Project Climate (Changing mTcm and Pwq)
Rw.lu = as.numeric(Model5$summary.random$mTcm[,"ID"])
Pred.pnts$RWF = sapply(Pred.pnts$mTcm70, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$mTcm[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 766, 1))
Pred.pnts$FmTcm_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RWF, POS)]))
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + FmTcm_lu + NN_lu +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq70*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
FLP = ifelse(is.na(LP), 0, LP),
FLPe = plogis(LP))
#Unconstrained dispersal
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + FmTcm_lu + UnDisp +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq70*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
FLP = ifelse(is.na(LP), 0, LP),
FLPeD = plogis(LP))
SEA_LP = rasterize(Pred.pnts, SEAsia.r, "LPe",
background = NA)
SEA_LP = sum(SEA_LP, SEAsia.r)
SEA_LPb = rasterize(Pred.pnts, SEAsia.r, "LPeC",
background = NA)
SEA_LPb = sum(SEA_LPb, SEAsia.r)
SEA8570 = rasterize(Pred.pnts, SEAsia.r, "FLPe",
background = NA)
SEA8570 = sum(SEA8570, SEAsia.r)
SEA8570b = rasterize(Pred.pnts, SEAsia.r, "FLPeD",
background = NA)
SEA8570b = sum(SEA8570b, SEAsia.r)
SEAPred.stk = stack(SEA_LP, SEA8570, SEA_LPb, SEA8570b)
names(SEAPred.stk) = c("A", "B", "C", "D")
rng = seq(0, 1, 0.1)
cr0 = rev(colorRampPalette(c("maroon", "yellow", "dodgerblue"))(n = 299))
cr = colorRampPalette(c("tan", cr0),
space = "rgb")
Cp = levelplot(SEAPred.stk,
margin = FALSE,
layout=c(2, 2),
#sub = " Probability of Occurrence",
xlab = " ", ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(0, 1, 0.001),
labels=list(cex=1.5),
labels=list(at=c(0, 0.25, 0.5, 0.75, 1),
labels=c("0%", "25%", "50%", "75%", "100%")),
space = "bottom"),
par.settings = list(axis.line = list(col = "black"),
strip.background = list(col = 'transparent'),
strip.border = list(col = 'transparent')),
scales = list(cex = 1.25))
Cp +
latticeExtra::layer(sp.polygons(SEAsia.sp, col = "dimgray", lwd = 1))
SE.obs = subset(LygPA, OBS == 1)
Cp.A = levelplot(SEA_LP,
margin = FALSE,
#sub = " Probability of Occurrence",
xlab = " ", ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(0, 1, 0.001),
labels=list(cex=1.5),
labels=list(at=c(0, 0.25, 0.5, 0.75, 1),
labels=c("0%", "25%", "50%", "75%", "100%")),
space = "bottom"),
par.settings = list(axis.line = list(col = "black"),
strip.background = list(col = 'transparent'),
strip.border = list(col = 'transparent')),
scales = list(cex = 1.5)) +
latticeExtra::layer(sp.polygons(SEAsia.sp, col = "dimgray", lwd = 1)) +
latticeExtra::layer(sp.polygons(SE.obs, col = "black", pch = 16, cex = 0.5))
Cp.A
Pred.pnts = MC.pnts #Copy Points
pLoc = cbind(Pred.pnts$Long, Pred.pnts$Lat)
pLoc = inla.mesh.map(pLoc,
projection = "longlat",
inverse = TRUE)
Ap = inla.spde.make.A(mesh1, loc=pLoc)
Pred.pnts$RF = drop(Ap %*% Model5$summary.random$field0$mean)
#Get CoH covariate
Pred.pnts$CoH = drop(Ap %*% ModelR$summary.random$fieldR$mean)
Pred.pnts$Int = Model5$summary.fix[1,1]
mydf = as.data.frame(Model5$summary.fixed)
Rw.lu = as.numeric(Model5$summary.random$mTcm[,"ID"])
Pred.pnts$RW = sapply(Pred.pnts$mTcm, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$mTcm[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 766, 1))
Pred.pnts$mTcm_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RW, POS)]))
Rw.lu = as.numeric(Model5$summary.random$NN[,"ID"])
Pred.pnts$RW = sapply(Pred.pnts$NN, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$NN[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 2189, 1))
Pred.pnts$NN_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RW, POS)]))
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + mTcm_lu + NN_lu +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
LP = ifelse(is.na(LP), 0, LP),
LPe = plogis(LP))
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + mTcm_lu + UnDisp +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
LP = ifelse(is.na(LP), 0, LP),
LPeC = plogis(LP))
#Changing to Project Climate (Changing mTcm and Pwq)
Rw.lu = as.numeric(Model5$summary.random$mTcm[,"ID"])
Pred.pnts$RWF = sapply(Pred.pnts$mTcm70, function(x)which.min(abs(x - Rw.lu)))
Rw.lu = as.data.frame(Model5$summary.random$mTcm[,"mean"])
names(Rw.lu) = "Mean"
Rw.lu$POS = as.integer(seq(1, 766, 1))
Pred.pnts$FmTcm_lu = as.numeric(with(Rw.lu,
Mean[match(Pred.pnts$RWF, POS)]))
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + FmTcm_lu + NN_lu +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq70*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
FLP = ifelse(is.na(LP), 0, LP),
FLPe = plogis(LP))
#Unconstrained dispersal
Pred.pnts@data = Pred.pnts@data %>%
mutate(LP = Int + RF + FmTcm_lu + UnDisp +
Pop*Model5$summary.fix[2,1] +
CTI*Model5$summary.fix[3,1] +
Pwq70*Model5$summary.fix[4,1] +
CoH*Model5$summary.fix[5,1],
FLP = ifelse(is.na(LP), 0, LP),
FLPeD = plogis(LP))
MC_LP = rasterize(Pred.pnts, MC.r, "LPe",
background = NA)
MC_LP = sum(MC_LP, MC.r)
MC_LPb = rasterize(Pred.pnts, MC.r, "LPeC",
background = NA)
MC_LPb = sum(MC_LPb, MC.r)
MC8570 = rasterize(Pred.pnts, MC.r, "FLPe",
background = NA)
MC8570 = sum(MC8570, MC.r)
MC8570b = rasterize(Pred.pnts, MC.r, "FLPeD",
background = NA)
MC8570b = sum(MC8570b, MC.r)
MCPred.stk = stack(MC_LP, MC8570, MC_LPb, MC8570b)
names(MCPred.stk) = c("A", "B", "C", "D")
rng = seq(0, 1, 0.1)
cr0 = rev(colorRampPalette(c("maroon", "yellow", "dodgerblue"))(n = 299))
cr = colorRampPalette(c("tan", cr0),
space = "rgb")
Cp = levelplot(MCPred.stk,
margin = FALSE,
layout=c(2, 2),
#sub = " Probability of Occurrence",
xlab = " ", ylab = NULL,
col.regions = cr, at = rng,
colorkey = list(at=seq(0, 1, 0.001),
labels=list(cex=1.5),
labels=list(at=c(0, 0.25, 0.5, 0.75, 1),
labels=c("0%", "25%", "50%", "75%", "100%")),
space = "bottom"),
par.settings = list(axis.line = list(col = "black"),
strip.background = list(col = 'transparent'),
strip.border = list(col = 'transparent')),
scales = list(cex = 1.25))
Cp +
latticeExtra::layer(sp.polygons(MC.sp, col = "dimgray", lwd = 1))
sessionInfo()
## R version 3.3.2 (2016-10-31)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 8.1 x64 (build 9600)
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] xtable_1.8-2 threejs_0.2.2 dplyr_0.5.0
## [4] dismo_1.1-1 gridExtra_2.2.1 GISTools_0.7-4
## [7] MASS_7.3-45 ggplot2_2.2.0 spdep_0.6-8
## [10] mapproj_1.2-4 maptools_0.8-40 Thermimage_2.2.3
## [13] rgeos_0.3-21 rasterVis_0.41 latticeExtra_0.6-28
## [16] RColorBrewer_1.1-2 lattice_0.20-34 fields_8.4-1
## [19] maps_3.1.1 spam_1.4-0 raster_2.5-8
## [22] reshape2_1.4.2 rgdal_1.2-4 INLA_0.0-1482340637
## [25] Matrix_1.2-7.1 sp_1.2-3 rgl_0.96.0
## [28] knitr_1.15.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.8 deldir_0.1-12 zoo_1.7-13
## [4] gtools_3.5.0 assertthat_0.1 rprojroot_1.1
## [7] digest_0.6.10 mime_0.5 R6_2.2.0
## [10] plyr_1.8.4 backports_1.0.4 evaluate_0.10
## [13] coda_0.19-1 lazyeval_0.2.0 gdata_2.17.0
## [16] hexbin_1.27.1 gmodels_2.16.2 rmarkdown_1.2
## [19] labeling_0.3 splines_3.3.2 stringr_1.1.0
## [22] foreign_0.8-67 htmlwidgets_0.8 munsell_0.4.3
## [25] shiny_0.14.2 numDeriv_2016.8-1 httpuv_1.3.3
## [28] base64enc_0.1-3 htmltools_0.3.5 tibble_1.2
## [31] viridisLite_0.1.3 nlme_3.1-128 jsonlite_1.1
## [34] gtable_0.2.0 DBI_0.5-1 magrittr_1.5
## [37] scales_0.4.1 stringi_1.1.2 LearnBayes_2.15
## [40] boot_1.3-18 tools_3.3.2 markdown_0.7.7
## [43] yaml_2.1.14 parallel_3.3.2 colorspace_1.3-2