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
|
sp <- c("Abelia biflora", "Abelia uniflora","Acer davidii","Acer davidii subsp. grosseri","Acer griseum", "Acer pictum subsp. mono","Acer truncatum","Ailanthus altissima","Aralia elata", "Berberis circumserrata","Berberis mitifolia","Betula chinensis","Betula luminifera", "Betula platyphylla","Buckleya graebneriana","Carpinus cordata","Carpinus turczaninowii", "Cerasus clarofolia","Cerasus polytricha","Cerasus serrulata","Cercidiphyllum japonicum", "Cornus controversa","Cornus kousa subsp. chinensis","Cornus macrophylla", "Cornus schindleri subsp. poliophylla","Cornus walteri","Corylus chinensis", "Corylus heterophylla","Cotoneaster acutifolius","Cotoneaster multiflorus", "Crataegus hupehensis","Crataegus pinnatifida","Deutzia parviflora", "Diospyros lotus","Elaeagnus umbellata","Euonymus maackii", "Euonymus phellomanus","Euonymus schensianus","Euptelea pleiosperma", "Evodia daniellii","Evodia fargesii","Forsythia suspensa","Fraxinus chinensis", "Fraxinus mandshurica","Fraxinus paxiana","Juglans cathayensis", "Koelreuteria paniculata","Lespedeza bicolor","Lespedeza buergeri", "Lindera obtusiloba","Litsea tsinlingensis","Lonicera hispida", "Lonicera microphylla","Lonicera tatarinowii","Maddenia hypoleuca", "Malus baccata","Malus honanensis","Malus hupehensis","Malus kansuensis", "Meliosma flexuosa","Meliosma veitchiorum","Ostrya japonica","Padus buergeriana", "Philadelphus incanus","Pinus armandii","Pinus tabuliformis","Populus davidiana", "Quercus aliena var. acutiserrata","Quercus serrata var. brevipetiolata", "Quercus variabilis","Rhododendron micranthum","Ribes pachysandroides", "Salix floderusii","Sambucus williamsii","Sinowilsonia henryi", "Sorbaria sorbifolia","Sorbus alnifolia","Sorbus hupehensis","Spiraea hirsuta", "Stachyurus chinensis","Styrax hemsleyanus","Styrax obassis","Symplocos paniculata", "Tilia japonica","Tilia paucicostata","Toxicodendron vernicifluum","Ulmus davidiana", "Viburnum betulifolium","Viburnum opulus var. sargentii","Yulania denudata")
set.seed(12345) library(sads)
library(stringr) library(vegan)
library(coefplot)
library(knitr) library(lme4)
library(broom)
spcode <- toupper(make.cepnames(sp))
nsp <- length(spcode) nind <- 12700
temp <- rls(n = nsp, N = 10000, alpha = 6)
sppool <- rep(spcode, temp)
species <- sample(sppool, nind, replace = TRUE)
table(species)
sum(table(species))
gx <- round(runif(nind)*250, digits = 2)
gy <- round(runif(nind)*200, digits = 2)
xlab20 <- cut(gx, breaks = seq(0, 260, by = 20), labels = 1:13) ylab20 <- cut(gy, breaks = seq(0, 220, by = 20), labels = 1:11)
sort(unique(ylab20))
sort(unique(xlab20))
quadrat20 <- paste(str_pad(xlab20, 2, pad ="0"), str_pad(ylab20, 2, pad ="0"), sep = "")
dbh = dbh <-round(rexp(nind,rate =2)*50+1, 1)
tag <- paste("BYS", str_pad(1:nind, 6, pad ="0"), sep ="")
status <- rbinom(nind, size = 1, prob = 0.01)
bys <- data.frame(tag, species, gx, gy, quadrat20, dbh, status)
nindividual <- table(bys$species)
sp_to_select <- names(nindividual[nindividual >= 30])
sub_bys <- subset(bys, subset = species %in% sp_to_select)
sub_bys_test <- sub_bys[1:2000,]
compute_neighbour_area <- function (dat, r = NULL) { if(any(is.na(dat))){ dat <- na.omit(dat) warning("NAs detected in the data and the entries containing NAs have been omitted.") } dat$ID <- 1:nrow(dat) if(is.null(dat$gx)){ stop("Column gx is missing") } if(is.null(dat$gy)){ stop("Column gy is missing") } if(is.null(dat$species)){ stop("Column species is missing") } if(is.null(dat$dbh)){ stop("Column species is missing") } if(is.null(r)){ stop("r is not specified") } gx <- dat$gx gy <- dat$gy dat$species <- as.character(dat$species) species_each_id <- dat$species area_of_neighbours_same_species <- rep(NA, nrow(dat)) area_of_neighbours_diff_species <- rep(NA, nrow(dat)) print(paste(nrow(dat), "rows to process.")) for (i in 1:nrow(dat)){ if(i %% 100 == 0 | i == 1){ print(paste("Computing neighbourhood area for row:", i)) } x <- gx[i] y <- gy[i] dat_sub <- dat[(gx < x + r) & (gx > x - r) & (gy < y + r) & (gy > y - r), ] dat_sub <- dat_sub[dat_sub$ID != i, ] if(nrow(dat_sub) > 0){ sp <- rep(NA, nrow(dat_sub)) for(j in 1:nrow(dat_sub)){ mat <- data.frame(c(x, dat_sub$gx[j]), c(y, dat_sub$gy[j])) sp[j] <- (dist(mat) <= r) } area_of_neighbours <- pi*(dat_sub$dbh[sp]/2)^2 if(length(area_of_neighbours[dat_sub$species[sp] %in% species_each_id[i]]) != 0 ){ area_of_neighbours_same_species[i] <- sum(area_of_neighbours[dat_sub$species[sp] %in% species_each_id[i]]) } if(length(area_of_neighbours[!dat_sub$species[sp] %in% species_each_id[i]]) != 0){ area_of_neighbours_diff_species[i] <- sum(area_of_neighbours[!dat_sub$species[sp] %in% species_each_id[i]]) } } else { area_of_neighbours_same_species[i] <- NA area_of_neighbours_diff_species[i] <- NA } } res <- data.frame(area_of_neighbours_same_species, area_of_neighbours_diff_species) colnames(res) <- paste(c("area_of_neighbours_same_species", "area_of_neighbours_diff_species"), "_r_", r, sep = "") return(res) }
neighbour_area_res10 <- compute_neighbour_area(sub_bys_test, r = 10)
sub_bys_test <- cbind(sub_bys_test, neighbour_area_res10)
head(sub_bys_test)
sub_bys_test$dbh <- scale(sub_bys_test$dbh) sub_bys_test$area_of_neighbours_same_species_r_10 <- scale( sub_bys_test$area_of_neighbours_same_species_r_10)
sub_bys_test$area_of_neighbours_diff_species_r_10 <- scale (sub_bys_test$area_of_neighbours_diff_species_r_10) glmm_model_10m = glmer(status ~ dbh + area_of_neighbours_same_species_r_10 + area_of_neighbours_diff_species_r_10 + (1+dbh|species)+(1|quadrat20), data=sub_bys_test, family = binomial(link="cloglog"))
tidy(glmm_model_10m)
glance(glmm_model_10m)
coefplot(glmm_model_10m)
|