diff options
author | Ken Kellner <ken@kenkellner.com> | 2024-01-14 22:08:13 -0500 |
---|---|---|
committer | Ken Kellner <ken@kenkellner.com> | 2024-01-14 22:08:13 -0500 |
commit | f77003443cb16eb37e4a112ed4720a2f9225572d (patch) | |
tree | 0eb009e2d222b12e570b4ef35a9d07e33c57b908 | |
parent | 2a4b0ac80d723bd2b75346b224ac9ff53530b4cb (diff) |
Move fields, sp, spdep to suggests
-rw-r--r-- | DESCRIPTION | 4 | ||||
-rw-r--r-- | NAMESPACE | 4 | ||||
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | R/plot-Nmix-resi&map-Nmix-resi_AHM1_6-9-3_plotting.R | 4 | ||||
-rw-r--r-- | R/simDynoccSpatial_AHM2_9.R | 8 | ||||
-rw-r--r-- | R/simExpCorrRF.R | 7 | ||||
-rw-r--r-- | R/simPPe_AHM2_10.R | 7 | ||||
-rw-r--r-- | inst/RandomFieldsSupport/simDynoccSpatial.R | 29 | ||||
-rw-r--r-- | inst/RandomFieldsSupport/simExpCorrRF.R | 9 | ||||
-rw-r--r-- | inst/RandomFieldsSupport/simPPe.R | 13 |
10 files changed, 55 insertions, 34 deletions
diff --git a/DESCRIPTION b/DESCRIPTION index 02a58d4..8e54eeb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -4,8 +4,8 @@ Title: Functions and Data for the Book 'Applied Hierarchical Modeling in Ecology Version: 0.2.11 Date: 2024-01-14 Depends: R (>= 2.10) -Imports: grDevices, graphics, methods, stats, utils, sp, fields, coda, unmarked (>= 0.12.2), mvtnorm, spdep -Suggests: raster +Imports: grDevices, graphics, methods, stats, utils, coda, unmarked (>= 0.12.2), mvtnorm +Suggests: fields, raster, sp, spdep Authors@R: c( person("Marc", "Kéry", role="aut"), person("Andy", "Royle", role="aut"), @@ -18,8 +18,8 @@ importFrom("unmarked", "unmarkedFrameOccu", "unmarkedFramePCount", "unmarkedFram "occuRN", "bup", "ranef", "coef", "fitted", "residuals", "vcov", "confint") # importFrom("plotrix", "draw.circle") # Now in file draw_circle.R # importFrom("RandomFields", "RFoptions", "RFsimulate", "RMexp") -importFrom("fields", "circulantEmbeddingSetup", "circulantEmbedding") -importFrom("sp", "bubble", "coordinates<-") +#importFrom("fields", "circulantEmbeddingSetup", "circulantEmbedding") +#importFrom("sp", "bubble", "coordinates<-") importFrom("coda", "crosscorr") importFrom("mvtnorm", "dmvnorm") @@ -1,3 +1,7 @@ +CHANGES in 0.2.10 (2023-11-04) + + * Remove plotrix from dependencies + CHANGES in 0.2.9 (2023-08-21) * Ken Kellner takes over as Maintainer diff --git a/R/plot-Nmix-resi&map-Nmix-resi_AHM1_6-9-3_plotting.R b/R/plot-Nmix-resi&map-Nmix-resi_AHM1_6-9-3_plotting.R index b213993..6fa1e18 100644 --- a/R/plot-Nmix-resi&map-Nmix-resi_AHM1_6-9-3_plotting.R +++ b/R/plot-Nmix-resi&map-Nmix-resi_AHM1_6-9-3_plotting.R @@ -51,6 +51,10 @@ map.Nmix.resi <- function(fm, x, y){ # object named fm, which was fit by function pcount in unmarked # Function arguments are the fitted model object and the x and y coordinates # of every site + +# Load required package +checkNamespace("sp") + mean.resi <- apply(residuals(fm), 1, mean, na.rm = TRUE) mean.resi[mean.resi == "NaN"] <- mean(mean.resi, na.rm = TRUE) spdata <- data.frame(residuals = mean.resi, x = x, y = y) diff --git a/R/simDynoccSpatial_AHM2_9.R b/R/simDynoccSpatial_AHM2_9.R index bc9a3c1..c48d9e0 100644 --- a/R/simDynoccSpatial_AHM2_9.R +++ b/R/simDynoccSpatial_AHM2_9.R @@ -103,8 +103,10 @@ simDynoccSpatial <- function(side = 50, nyears = 10, nsurveys = 3, if(FALSE) {x <- NULL; rm(x)} # Stops R CMD check choking on 'curve'. - # Load raster package------------------------------------------ + # Load required packages -------------------------------------- checkNamespace("raster") + checkNamespace("fields") + checkNamespace("spdep") # Checks and fixes for input data ----------------------------- side <- round(side[1]) @@ -167,8 +169,8 @@ simDynoccSpatial <- function(side = 50, nyears = 10, nsurveys = 3, message("Using package 'fields' instead of 'RandomFields'; see help(simDynoccSpatial).") if(!is.na(seed.XAC)) set.seed(seed.XAC) - obj <- circulantEmbeddingSetup(grid=list(x=xcoord, y=ycoord), Covariance="Exponential", aRange=theta.XAC) - tmp <- try(circulantEmbedding(obj), silent=TRUE) + obj <- fields::circulantEmbeddingSetup(grid=list(x=xcoord, y=ycoord), Covariance="Exponential", aRange=theta.XAC) + tmp <- try(fields::circulantEmbedding(obj), silent=TRUE) if(inherits(tmp, "try-error")) stop("Simulation of random field failed.\nTry with smaller values for 'side' or 'theta.XAC'.") XAC <- matrix(tmp, ncol = side, byrow = TRUE) diff --git a/R/simExpCorrRF.R b/R/simExpCorrRF.R index 70bb909..f687950 100644 --- a/R/simExpCorrRF.R +++ b/R/simExpCorrRF.R @@ -30,8 +30,9 @@ simExpCorrRF <- function(variance = 1, theta = 1, size = 50, seed = NA, show.plo # show.plot: if TRUE, plots of the data will be displayed; # set to FALSE if you are running simulations or use inside of other fct's. -# Load raster package +# Load required packages checkNamespace("raster") +checkNamespace("fields") # Generate correlated random variables in a square step <- 1 @@ -50,8 +51,8 @@ grid <- cbind(x = rep(x, each=size), y = y) message("Using package 'fields' instead of 'RandomFields'; see help(simExpCorrRF).") if(!is.na(seed)) set.seed(seed) # Only for compatibility with RandomFields, better to set seed before calling simExpCommRF - obj <- circulantEmbeddingSetup(grid=list(x=x, y=y), Covariance="Exponential", aRange=theta) - tmp <- try(circulantEmbedding(obj), silent=TRUE) + obj <- fields::circulantEmbeddingSetup(grid=list(x=x, y=y), Covariance="Exponential", aRange=theta) + tmp <- try(fields::circulantEmbedding(obj), silent=TRUE) if(inherits(tmp, "try-error")) stop("Simulation of random field failed.\nTry with smaller values for 'size' or 'theta'.") field <- as.vector(tmp * sqrt(variance)) diff --git a/R/simPPe_AHM2_10.R b/R/simPPe_AHM2_10.R index 0187e45..1023fdb 100644 --- a/R/simPPe_AHM2_10.R +++ b/R/simPPe_AHM2_10.R @@ -33,8 +33,9 @@ simPPe <- function(lscape.size = 150, buffer.width = 25, variance.X = 1, theta.X # (this is the parameter of the gridding process # and determines the size of the quadrats) - # -------------- Load raster package ----------------------- + # -------------- Load required packages -------------------- checkNamespace("raster") + checkNamespace("fields") # -------------- Check and fix input ----------------------- buffer.width <- round(buffer.width[1]) @@ -84,8 +85,8 @@ simPPe <- function(lscape.size = 150, buffer.width = 25, variance.X = 1, theta.X # x=x, y=y, grid=TRUE)@data$variable1, ncol = lscape.size) # MVN r.v. with spatial correlation #} else { message("Using package 'fields' instead of 'RandomFields'; see help(simPPe).") - obj <- circulantEmbeddingSetup(grid=list(x=x, y=y), Covariance="Exponential", aRange=theta.X) - tmp <- try(circulantEmbedding(obj), silent=TRUE) + obj <- fields::circulantEmbeddingSetup(grid=list(x=x, y=y), Covariance="Exponential", aRange=theta.X) + tmp <- try(fields::circulantEmbedding(obj), silent=TRUE) if(inherits(tmp, "try-error")) stop("Simulation of random field failed.\nTry with smaller values for 'lscape.size' or 'theta.X'.") field <- matrix(tmp * sqrt(variance.X), ncol = lscape.size) diff --git a/inst/RandomFieldsSupport/simDynoccSpatial.R b/inst/RandomFieldsSupport/simDynoccSpatial.R index 4bb53af..01fa6b1 100644 --- a/inst/RandomFieldsSupport/simDynoccSpatial.R +++ b/inst/RandomFieldsSupport/simDynoccSpatial.R @@ -103,6 +103,9 @@ simDynoccSpatial <- function(side = 50, nyears = 10, nsurveys = 3, if(FALSE) {x <- NULL; rm(x)} # Stops R CMD check choking on 'curve'. + checkNamespace("raster") + checkNamespace("fields") + # Checks and fixes for input data ----------------------------- side <- round(side[1]) nyears <- round(nyears[1]) @@ -164,8 +167,8 @@ simDynoccSpatial <- function(side = 50, nyears = 10, nsurveys = 3, message("Using package 'fields' instead of 'RandomFields'; see help(simDynoccSpatial).") if(!is.na(seed.XAC)) set.seed(seed.XAC) - obj <- circulantEmbeddingSetup(grid=list(x=xcoord, y=ycoord), Covariance="Exponential", aRange=theta.XAC) - tmp <- try(circulantEmbedding(obj), silent=TRUE) + obj <- fields::circulantEmbeddingSetup(grid=list(x=xcoord, y=ycoord), Covariance="Exponential", aRange=theta.XAC) + tmp <- try(fields::circulantEmbedding(obj), silent=TRUE) if(inherits(tmp, "try-error")) stop("Simulation of random field failed.\nTry with smaller values for 'side' or 'theta.XAC'.") XAC <- matrix(tmp, ncol = side, byrow = TRUE) @@ -228,19 +231,19 @@ simDynoccSpatial <- function(side = 50, nyears = 10, nsurveys = 3, # Plot random field covariate XAC # rows are in x, columns in y direction - image(1:side, 1:side, XAC, col=topo.colors(100), + raster::image(1:side, 1:side, XAC, col=topo.colors(100), main = paste("Gaussian random field XAC with \n neg. exponential correlation (range =", theta.XAC, ")"), xlab = 'x', ylab = 'y') - image(1:side, 1:side, psi[,,1], col=topo.colors(100), + raster::image(1:side, 1:side, psi[,,1], col=topo.colors(100), main = paste("Initial occupancy probability"), xlab = 'x', ylab = 'y') - image(1:side, 1:side, z[,,1], col=c("white", "black"), + raster::image(1:side, 1:side, z[,,1], col=c("white", "black"), main = paste("Initial presence/absence (true system state z):\n black = occupied, white = unoccupied"), xlab = 'x', ylab = 'y') abline(h = 0:side+0.5, v = 0:side+0.5, col = "lightgrey") - image(1:side, 1:side, Xauto[,,1], col=topo.colors(100), + raster::image(1:side, 1:side, Xauto[,,1], col=topo.colors(100), main = "Autocovariate between year 1 and year 2", xlab = 'x', ylab = 'y') }, silent = TRUE) if(inherits(tryPlot, "try-error")) { @@ -272,15 +275,15 @@ simDynoccSpatial <- function(side = 50, nyears = 10, nsurveys = 3, # -------------------- if(show.plots) { tryPlot <- try( { - image(1:side, 1:side, phi[,,k-1], col=topo.colors(100), + raster::image(1:side, 1:side, phi[,,k-1], col=topo.colors(100), main = paste("Persistence between year", k-1, "and year", k), xlab = 'x', ylab = 'y') - image(1:side, 1:side, gamma[,,k-1], col=topo.colors(100), + raster::image(1:side, 1:side, gamma[,,k-1], col=topo.colors(100), main = paste("Colonization between year", k-1, "and year", k), xlab = 'x', ylab = 'y') - image(1:side, 1:side, z[,,k], col=c("white", "black"), + raster::image(1:side, 1:side, z[,,k], col=c("white", "black"), main = paste('Presence/absence (z) in year', k, ':\n black = occupied, white = unoccupied'), xlab = 'x', ylab = 'y') abline(h = 0:side+0.5, v = 0:side+0.5, col = "lightgrey") - image(1:side, 1:side, Xauto[,,k], col=topo.colors(100), + raster::image(1:side, 1:side, Xauto[,,k], col=topo.colors(100), main = paste("Autocovariate between year", k, "and year", k+1), xlab = 'x', ylab = 'y') }, silent = TRUE) if(inherits(tryPlot, "try-error")) { @@ -312,7 +315,7 @@ simDynoccSpatial <- function(side = 50, nyears = 10, nsurveys = 3, for(j in 1:nsurveys){ # Loop over replicates prob <- z[,,k] * p[,,j,k] # zero out p for unoccupied sites y[,,j,k] <- rbinom(M, 1, prob) - # image(1:side, 1:side, y[,,j,k], main = paste("Year", k, "and rep", j)) + # raster::image(1:side, 1:side, y[,,j,k], main = paste("Year", k, "and rep", j)) # Look at clumped pattern in y } } @@ -345,9 +348,9 @@ simDynoccSpatial <- function(side = 50, nyears = 10, nsurveys = 3, # Plots comparing true and observed latent states par(mfrow = c(2,2), mar = c(5,4,5,2), cex.main = 1.3, cex.lab = 1.5, cex.axis = 1.2) for(k in 1:nyears){ - image(1:side, 1:side, z[,,k], col=c("white", "black"), main = paste('Presence/absence (z) in year', k), xlab = 'x', ylab = 'y') + raster::image(1:side, 1:side, z[,,k], col=c("white", "black"), main = paste('Presence/absence (z) in year', k), xlab = 'x', ylab = 'y') abline(h = 0:side+0.5, v = 0:side+0.5, col = "lightgrey") - image(1:side, 1:side, zobs[,,k], col=c("white", "black"), main = paste('Ever_detected (zobs) in year', k), xlab = 'x', ylab = 'y') + raster::image(1:side, 1:side, zobs[,,k], col=c("white", "black"), main = paste('Ever_detected (zobs) in year', k), xlab = 'x', ylab = 'y') abline(h = 0:side+0.5, v = 0:side+0.5, col = "lightgrey") } }, silent = TRUE) diff --git a/inst/RandomFieldsSupport/simExpCorrRF.R b/inst/RandomFieldsSupport/simExpCorrRF.R index 225c263..72249cf 100644 --- a/inst/RandomFieldsSupport/simExpCorrRF.R +++ b/inst/RandomFieldsSupport/simExpCorrRF.R @@ -29,6 +29,9 @@ simExpCorrRF <- function(variance = 1, theta = 1, size = 50, seed = NA, show.plo # show.plot: if TRUE, plots of the data will be displayed; # set to FALSE if you are running simulations or use inside of other fct's. +checkNamespace("raster") +checkNamespace("fields") + # Generate correlated random variables in a square step <- 1 x <- seq(1, size, step) @@ -45,8 +48,8 @@ if(requireNamespace("RandomFields", quietly=TRUE)) { message("Using package 'fields' instead of 'RandomFields'; see help(simExpCorrRF).") if(!is.na(seed)) set.seed(seed) # Only for compatibility with RandomFields, better to set seed before calling simExpCommRF - obj <- circulantEmbeddingSetup(grid=list(x=x, y=y), Covariance="Exponential", aRange=theta) - tmp <- try(circulantEmbedding(obj), silent=TRUE) + obj <- fields::circulantEmbeddingSetup(grid=list(x=x, y=y), Covariance="Exponential", aRange=theta) + tmp <- try(fields::circulantEmbedding(obj), silent=TRUE) if(inherits(tmp, "try-error")) stop("Simulation of random field failed.\nTry with smaller values for 'size' or 'theta'.") field <- as.vector(tmp * sqrt(variance)) @@ -65,7 +68,7 @@ if(show.plots){ # Random field # image(x, y, field,col=topo.colors(20), main = paste("Gaussian random field with \n negative exponential correlation (theta =", theta, ")"), cex.main = 1) par(mar = c(3,2,5,1)) - raster::plot(rasterFromXYZ(cbind(grid, field)), col=topo.colors(20), + raster::plot(raster::rasterFromXYZ(cbind(grid, field)), col=topo.colors(20), main = paste("Gaussian random field with \n negative exponential correlation (theta =", theta, ")"), cex.main = 1, legend=FALSE, box=FALSE) box() }, silent = TRUE) diff --git a/inst/RandomFieldsSupport/simPPe.R b/inst/RandomFieldsSupport/simPPe.R index cd25af4..f8ddb8a 100644 --- a/inst/RandomFieldsSupport/simPPe.R +++ b/inst/RandomFieldsSupport/simPPe.R @@ -32,6 +32,9 @@ simPPe <- function(lscape.size = 150, buffer.width = 25, variance.X = 1, theta.X # quads.along.side: number of quadrats along the side of the core area # (this is the parameter of the gridding process # and determines the size of the quadrats) + + checkNamespace("raster") + checkNamespace("field") # -------------- Check and fix input ----------------------- buffer.width <- round(buffer.width[1]) @@ -81,8 +84,8 @@ simPPe <- function(lscape.size = 150, buffer.width = 25, variance.X = 1, theta.X x=x, y=y, grid=TRUE)@data$variable1, ncol = lscape.size) # MVN r.v. with spatial correlation } else { message("Using package 'fields' instead of 'RandomFields'; see help(simPPe).") - obj <- circulantEmbeddingSetup(grid=list(x=x, y=y), Covariance="Exponential", aRange=theta.X) - tmp <- try(circulantEmbedding(obj), silent=TRUE) + obj <- fields::circulantEmbeddingSetup(grid=list(x=x, y=y), Covariance="Exponential", aRange=theta.X) + tmp <- try(fields::circulantEmbedding(obj), silent=TRUE) if(inherits(tmp, "try-error")) stop("Simulation of random field failed.\nTry with smaller values for 'lscape.size' or 'theta.X'.") field <- matrix(tmp * sqrt(variance.X), ncol = lscape.size) @@ -125,7 +128,7 @@ simPPe <- function(lscape.size = 150, buffer.width = 25, variance.X = 1, theta.X tryPlot <- try( { # *** Fig. 1: Original point pattern # Random field of X with activity-centers overlaid - image(rasterFromXYZ(cbind(grid, c(field))), col=topo.colors(10), + raster::image(raster::rasterFromXYZ(cbind(grid, c(field))), col=topo.colors(10), main = "Point pattern with\ncore and buffer area", xlab = "", ylab = "", axes = FALSE, asp = 1) mtext(paste("Mean intensity (lambda) =", round(lambda_pp, 5)), side=1) @@ -138,7 +141,7 @@ simPPe <- function(lscape.size = 150, buffer.width = 25, variance.X = 1, theta.X # *** Fig. 2: Show abundance and presence/absence in each quadrat on original landscape *** # Covariate 1: the Gaussian random field with autocorrelation # Reproduce random field with activity centers - image(rasterFromXYZ(cbind(grid, c(field))), col=topo.colors(10), main = "Abundance, N", + raster::image(raster::rasterFromXYZ(cbind(grid, c(field))), col=topo.colors(10), main = "Abundance, N", xlab = "", ylab = "", axes = FALSE, asp = 1) mtext(paste0("Mean(N) = ", E_N, ", var(N) = ", round(var(c(Nac)), 2)), side=1) polygon(c(buffer.width, size.core+buffer.width, size.core+buffer.width, buffer.width), @@ -161,7 +164,7 @@ simPPe <- function(lscape.size = 150, buffer.width = 25, variance.X = 1, theta.X # Figure 3 for presence/absence of activity centers (= distribution) # Reproduce random field with activity centers - image(rasterFromXYZ(cbind(grid, c(field))), col=topo.colors(10), main = "Occurrence, z", + raster::image(raster::rasterFromXYZ(cbind(grid, c(field))), col=topo.colors(10), main = "Occurrence, z", xlab = "", ylab = "", axes = FALSE, asp = 1) mtext(paste("Mean(z) =", E_z), side=1) polygon(c(buffer.width, size.core+buffer.width, size.core+buffer.width, buffer.width), |