-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03c_sourceable_vel_hf_clim_analysis.R
More file actions
172 lines (138 loc) · 6.63 KB
/
Copy path03c_sourceable_vel_hf_clim_analysis.R
File metadata and controls
172 lines (138 loc) · 6.63 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
## 03c_sp_sourceable_hf_clim_analysis
# setwd("/Users/macbookpro/Library/CloudStorage/OneDrive-Personal/PhD/spatialpattern_climate_humanfootprint")
# source("03c_sourceable_hf_clim_analysis.R")
print("sourcing hf and climate models script")
source("00_sp_functions.R")
## metrics ~ human footprint, climate change velocity and climate data --------
## read in and handle data------------------------------------------------------------------------------------------------
metrics <- readRDS("Data_metrics_for_hf_analysis.rds")
clean_tree <- read.tree("Data_sp_clean_tree.tre")
names(metrics) <- c("species", "total.area", "range.size", "effective.mesh.size", "prop.landscape", "mean.shape.index", "perimeter.area.frac.dim",
"hf_mean", "vel_mean", "mat_mean", "mat_var_mean", "map_mean", "map_var_mean")
## prep analysis ---------------------------------
mcmc_data <- metrics
mcmc_data$animal <- mcmc_data$species
## create comparative dataset
comp_data <- clean.data(mcmc_data, clean_tree, data.col = "animal")
# # priors----------------------------------------------------------------------------------------
print("this is a parameter expanded prior")
a <- 1000
b <- 1
prior<- list(R = list(V=1, nu=0.002),
G = list(G1 = list(V = diag(b), nu =0.002, alpha.mu = 0, alpha.V = diag(b)*a)))
## parameters-------------------------------------------------------------------------------------
nitt <- c(240000) #no. of interations
burnin <- nitt/6 #length of burnin
thin <- c(20) #amount of thinning
eff_ss <- (nitt-burnin)/thin
print(c("effect size will be:", eff_ss))
## hf and climate model --------------
# print("prep hf and climate model")
# ## formula ------------------
# ## set the formula for each spatial pattern metric
# f <- list()
# f[["total.area"]] <- total.area ~ hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
# f[["range.size"]] <- range.size ~ hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
# f[["effective.mesh.size"]] <- effective.mesh.size ~ hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
# f[["prop.landscape"]] <- prop.landscape ~ hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
# f[["mean.shape.index"]] <- mean.shape.index ~ hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
# f[["perimeter.area.frac.dim"]] <- perimeter.area.frac.dim ~ hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
#
#
# print("hf and climate forumal set")
#
# print("start running hf and climate models")
#
# ## model ---------------
# m_metric <- list()
#
# for(j in names(comp_data[["data"]][which(names(comp_data[["data"]]) %in% c("total.area", "range.size", "effective.mesh.size", "prop.landscape", "mean.shape.index",
# "perimeter.area.frac.dim"))])){
# formula <- f[[j]]
#
# print(j)
# m_metric[[j]][["hf"]] <- mod_list <- mclapply(1:2, function(i) {
# MCMCglmm(fixed = formula,
# random = ~ animal,
# rcov = ~ units,
# family= "gaussian",
# pedigree = comp_data$tree,
# data = comp_data$data,
# nitt = nitt,
# burnin = burnin,
# thin = thin,
# prior = prior)
# }, mc.cores=2)
# Sys.sleep(20)}
#
# print("saving models")
# saveRDS(m_metric, "m_metric_hf_clim.rds")
## vel and climate model --------------
print("prep vel and climate model")
## formula ------------------
## set the formula for each spatial pattern metric
f <- list()
f[["total.area"]] <- total.area ~ vel_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["range.size"]] <- range.size ~ vel_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["effective.mesh.size"]] <- effective.mesh.size ~ vel_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["prop.landscape"]] <- prop.landscape ~ vel_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["mean.shape.index"]] <- mean.shape.index ~ vel_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["perimeter.area.frac.dim"]] <- perimeter.area.frac.dim ~ vel_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
print("vel and climate forumal set")
print("start running vel and climate models")
## model ---------------
m_metric <- list()
for(j in names(comp_data[["data"]][which(names(comp_data[["data"]]) %in% c("total.area", "range.size", "effective.mesh.size", "prop.landscape", "mean.shape.index",
"perimeter.area.frac.dim"))])){
formula <- f[[j]]
print(j)
m_metric[[j]][["hf"]] <- mod_list <- mclapply(1:2, function(i) {
MCMCglmm(fixed = formula,
random = ~ animal,
rcov = ~ units,
family= "gaussian",
pedigree = comp_data$tree,
data = comp_data$data,
nitt = nitt,
burnin = burnin,
thin = thin,
prior = prior)
}, mc.cores=2)
Sys.sleep(20)}
print("saving models")
saveRDS(m_metric, "m_metric_vel_clim.rds")
## vel, hf and climate model --------------
print("prep vel, hf and climate model")
## formula ------------------
## set the formula for each spatial pattern metric
f <- list()
f[["total.area"]] <- total.area ~ vel_mean*hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["range.size"]] <- range.size ~ vel_mean*hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["effective.mesh.size"]] <- effective.mesh.size ~ vel_mean*hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["prop.landscape"]] <- prop.landscape ~ vel_mean*hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["mean.shape.index"]] <- mean.shape.index ~ vel_mean*hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
f[["perimeter.area.frac.dim"]] <- perimeter.area.frac.dim ~ vel_mean*hf_mean*map_mean*mat_mean*map_var_mean*mat_var_mean
print("vel, hf and climate forumal set")
print("start running vel, hf and climate models")
## model ---------------
m_metric <- list()
for(j in names(comp_data[["data"]][which(names(comp_data[["data"]]) %in% c("total.area", "range.size", "effective.mesh.size", "prop.landscape", "mean.shape.index",
"perimeter.area.frac.dim"))])){
formula <- f[[j]]
print(j)
m_metric[[j]][["hf"]] <- mod_list <- mclapply(1:2, function(i) {
MCMCglmm(fixed = formula,
random = ~ animal,
rcov = ~ units,
family= "gaussian",
pedigree = comp_data$tree,
data = comp_data$data,
nitt = nitt,
burnin = burnin,
thin = thin,
prior = prior)
}, mc.cores=2)
Sys.sleep(20)}
print("saving models")
saveRDS(m_metric, "m_metric_vel_hf_clim.rds")
print("end")