Skip to content

Commit

Permalink
Merge pull request #1 from rubak/master
Browse files Browse the repository at this point in the history
Adapt to new spatstat.random package.
  • Loading branch information
Ian D Buller committed Feb 2, 2022
2 parents 3ee98b0 + 6bbdea5 commit 4df5d85
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 50 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Depends:
R (>= 3.5.0),
spatstat.geom,
spatstat.core,
spatstat.linnet,
spatstat (>= 2.0-0)
Imports:
doFuture,
doRNG,
Expand All @@ -55,8 +51,12 @@ Imports:
raster,
sp,
sparr,
spatstat.geom,
spatstat.random,
stats
Suggests:
spatstat.data,
spatstat (>= 2.0-0),
testthat,
spelling,
R.rsp
Expand Down
11 changes: 4 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ export(pval_correct)
export(spatial_data)
export(spatial_plots)
export(spatial_power)
import(spatstat)
import(spatstat.core)
import(spatstat.geom)
import(spatstat.linnet)
importFrom(doFuture,registerDoFuture)
importFrom(doRNG,"%dorng%")
importFrom(fields,image.plot)
Expand All @@ -33,10 +30,6 @@ importFrom(raster,values)
importFrom(sp,coordinates)
importFrom(sp,gridded)
importFrom(sparr,risk)
importFrom(spatstat.core,rNeymanScott)
importFrom(spatstat.core,rpoispp)
importFrom(spatstat.core,runifdisc)
importFrom(spatstat.core,runifpoint)
importFrom(spatstat.geom,as.solist)
importFrom(spatstat.geom,disc)
importFrom(spatstat.geom,marks)
Expand All @@ -47,6 +40,10 @@ importFrom(spatstat.geom,rsyst)
importFrom(spatstat.geom,shift)
importFrom(spatstat.geom,superimpose)
importFrom(spatstat.geom,unit.square)
importFrom(spatstat.random,rNeymanScott)
importFrom(spatstat.random,rpoispp)
importFrom(spatstat.random,runifdisc)
importFrom(spatstat.random,runifpoint)
importFrom(stats,na.omit)
importFrom(stats,rnorm)
importFrom(stats,sd)
8 changes: 4 additions & 4 deletions R/jitter_power.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#' @importFrom future multisession plan
#' @importFrom lifecycle badge deprecate_warn deprecated is_present
#' @importFrom sparr risk
#' @importFrom spatstat.core rpoispp runifpoint
#' @importFrom spatstat.random rpoispp runifpoint
#' @importFrom spatstat.geom marks ppp superimpose
#' @importFrom stats sd
#' @export
Expand All @@ -65,7 +65,7 @@
#'
#' @examples
#' # Using the 'chorley' data set from 'spatstat.data' package
#' data(chorley)
#' data(chorley, package="spatstat.data")
#' f1 <- jitter_power(obs_data = unique(chorley),
#' samp_control = "CSR",
#' verbose = FALSE)
Expand Down Expand Up @@ -107,13 +107,13 @@ jitter_power <- function(obs_data,
rcluster_control <- function(n, l, win, s, types = "control", ...) {
if (samp_control == "uniform") {
repeat {
x <- spatstat.core::runifpoint(n = n, win = win, ...)
x <- spatstat.random::runifpoint(n = n, win = win, ...)
if (x$n == n) break
}
}

if (samp_control == "CSR") {
x <- spatstat.core::rpoispp(lambda = l, win = win, ...)
x <- spatstat.random::rpoispp(lambda = l, win = win, ...)
}

if (samp_control == "MVN") {
Expand Down
5 changes: 1 addition & 4 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' @aliases sparrpowR-package sparrpowR
#' @docType package
#'
#' @section Dependencies: The 'sparrpowR' package relies heavily upon \code{\link{sparr}}, \code{\link{spatstat.core}}, \code{\link{spatstat.geom}}, and \code{\link{raster}} for computing the statistical power and visualizing the output. Computation can be performed in parallel using \code{\link{doFuture}}, \code{\link[future]{multisession}}, \code{\link{doRNG}}, and \code{\link[foreach]{foreach}}. Basic visualizations rely on the \code{\link[spatstat.geom]{plot.ppp}} and \code{\link[fields]{image.plot}} functions.
#' @section Dependencies: The 'sparrpowR' package relies heavily upon \code{\link{sparr}}, \code{\link{spatstat.random}}, \code{\link{spatstat.geom}}, and \code{\link{raster}} for computing the statistical power and visualizing the output. Computation can be performed in parallel using \code{\link{doFuture}}, \code{\link[future]{multisession}}, \code{\link{doRNG}}, and \code{\link[foreach]{foreach}}. Basic visualizations rely on the \code{\link[spatstat.geom]{plot.ppp}} and \code{\link[fields]{image.plot}} functions.
#'
#' @author Ian D. Buller\cr \emph{Occupational and Environmental Epidemiology Branch, Division of Cancer Epidemiology and Genetics, National Cancer Institute, National Institutes of Health, Rockville, Maryland, USA.} \cr\cr
#' Derek W. Brown\cr \emph{Integrative Tumor Epidemiology Branch, Division of Cancer Epidemiology and Genetics, National Cancer Institute, National Institutes of Health, Rockville, Maryland, USA.}
Expand All @@ -50,7 +50,4 @@ NULL
#' @importFrom sparr risk
#' @importFrom stats na.omit rnorm sd
#' @import spatstat.geom
#' @import spatstat.core
#' @import spatstat.linnet
#' @import spatstat
NULL
24 changes: 12 additions & 12 deletions R/spatial_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#' @param l_case Optional. A single positive number, a vector of positive numbers, a function(x,y, ...), or a pixel image. Intensity of the Poisson process for case clusters. Ignored if \code{samp_control != "IPP"}.
#' @param l_control Optional. A single positive number, a vector of positive numbers, a function(x,y, ...), or a pixel image. Intensity of the Poisson process for control clusters. Ignored if \code{samp_control = "uniform"}, \code{samp_control = "systematic"}, \code{samp_control = "MVN"}, or \code{samp_control = "CSR"}.
#' @param e_control Optional. A single non-negative number for the size of the expansion of the simulation window for generating parent points. Ignored if \code{samp_control != "clustered"}.
#' @param ... Arguments passed to \code{\link[spatstat.core]{runifdisc}}, \code{\link[spatstat.geom]{disc}}, \code{\link[spatstat.core]{rpoispp}}, \code{\link[spatstat.geom]{rsyst}}, or \code{\link[spatstat.core]{rNeymanScott}} depending on \code{samp_control} or \code{samp_control}.
#' @param ... Arguments passed to \code{\link[spatstat.random]{runifdisc}}, \code{\link[spatstat.geom]{disc}}, \code{\link[spatstat.random]{rpoispp}}, \code{\link[spatstat.geom]{rsyst}}, or \code{\link[spatstat.random]{rNeymanScott}} depending on \code{samp_control} or \code{samp_control}.
#'
#' @details This function generates random data for a spatial relative risk function (nonparametric estimate of relative risk by kernel smoothing) using various random point pattern generators from the \code{\link{spatstat.core}} package to generate data.
#' @details This function generates random data for a spatial relative risk function (nonparametric estimate of relative risk by kernel smoothing) using various random point pattern generators from the \code{\link{spatstat.random}} package to generate data.
#'
#' If \code{samp_case = "uniform"} the case locations are randomly generated uniformly within a disc of radius \code{r_case} (or discs of radii \code{r_case}) centered at coordinates (\code{x_case}, \code{y_case}).
#'
Expand All @@ -47,11 +47,11 @@
#' @return An object of class "ppplist". This is a list of marked point patterns that have a single mark with two levels: case and control.
#'
#' @importFrom stats rnorm
#' @importFrom spatstat.core rNeymanScott rpoispp runifdisc runifpoint
#' @importFrom spatstat.random rNeymanScott rpoispp runifdisc runifpoint
#' @importFrom spatstat.geom as.solist disc marks ppp rsyst shift superimpose unit.square
#' @export
#'
#' @seealso \code{\link[spatstat.core]{runifdisc}}, \code{\link[spatstat.geom]{disc}}, \code{\link[spatstat.core]{rpoispp}}, \code{\link[spatstat.geom]{rsyst}}, or \code{\link[spatstat.core]{rNeymanScott}} for additional arguments for random point pattern generation.
#' @seealso \code{\link[spatstat.random]{runifdisc}}, \code{\link[spatstat.geom]{disc}}, \code{\link[spatstat.random]{rpoispp}}, \code{\link[spatstat.geom]{rsyst}}, or \code{\link[spatstat.random]{rNeymanScott}} for additional arguments for random point pattern generation.
#'
#' @examples
#' spatial_data(x_case = c(0.25, 0.5, 0.75),
Expand Down Expand Up @@ -139,7 +139,7 @@ spatial_data <- function(win = spatstat.geom::unit.square(),
rcluster_case <- function(x0, y0, rad, n, scalar, lamb, wind, types = "case", ...) {

if (samp_case == "uniform"){
x <- spatstat.core::runifdisc(n = n, radius = rad, centre = c(x0, y0), win = wind, ...)
x <- spatstat.random::runifdisc(n = n, radius = rad, centre = c(x0, y0), win = wind, ...)
}

if (samp_case == "MVN"){
Expand All @@ -153,7 +153,7 @@ spatial_data <- function(win = spatstat.geom::unit.square(),
if (samp_case == "CSR"){
win_case <- spatstat.geom::disc(radius = rad, centre = c(0.5, 0.5), ...)
l <- n / (diff(win_case$xrange)*diff(win_case$yrange))
x <- spatstat.core::rpoispp(lambda = l, win = win_case, ...)
x <- spatstat.random::rpoispp(lambda = l, win = win_case, ...)
x <- spatstat.geom::shift(x, c(x0 - 0.5, y0 - 0.5))
}

Expand All @@ -162,7 +162,7 @@ spatial_data <- function(win = spatstat.geom::unit.square(),
stop("The argument 'l_case' should be an intensity function")
}
win_case <- spatstat.geom::disc(radius = rad, centre = c(0.5, 0.5), ...)
x <- spatstat.core::rpoispp(lambda = lamb, win = win_case, ...)
x <- spatstat.random::rpoispp(lambda = lamb, win = win_case, ...)
x <- spatstat.geom::shift(x, c(x0 - 0.5, y0 - 0.5))
}

Expand All @@ -173,7 +173,7 @@ spatial_data <- function(win = spatstat.geom::unit.square(),
# marked uniform ppp for controls
rcluster_control <- function(x0, y0, scalar, n, lamb, ex, nclust, rad, types = "control", wind, ...) {
if (samp_control == "uniform"){
x <- spatstat.core::runifpoint(n, win = wind, ...)
x <- spatstat.random::runifpoint(n, win = wind, ...)
}

if (samp_control == "systematic") {
Expand All @@ -190,22 +190,22 @@ spatial_data <- function(win = spatstat.geom::unit.square(),

if (samp_control == "CSR") {
l <- n / (diff(wind$xrange)*diff(wind$yrange))
x <- spatstat.core::rpoispp(lambda = l, win = wind, ...)
x <- spatstat.random::rpoispp(lambda = l, win = wind, ...)
}

if (samp_control == "IPP") {
if (class(lamb) != "function") {
stop("The argument 'l_control' should be an intensity function")
}
x <- spatstat.core::rpoispp(lambda = lamb, win = wind, ...)
x <- spatstat.random::rpoispp(lambda = lamb, win = wind, ...)
}

if (samp_control == "clustered") {
control_clustering <- function(x0, y0, radius, n) {
X <- spatstat.core::runifdisc(n, radius, centre = c(x0, y0))
X <- spatstat.random::runifdisc(n, radius, centre = c(x0, y0))
return(X)
}
x <- spatstat.core::rNeymanScott(kappa = lamb,
x <- spatstat.random::rNeymanScott(kappa = lamb,
expand = ex,
rcluster = control_clustering,
n = nclust,
Expand Down
22 changes: 11 additions & 11 deletions R/spatial_power.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
#' @param parallel Logical. If TRUE, will execute the function in parallel. If FALSE (the default), will not execute the function in parallel.
#' @param n_core Optional. Integer specifying the number of CPU cores on current host to use for parallelization (the default is 2 cores).
#' @param verbose Logical. If TRUE (the default), will print function progress during execution. If FALSE, will not print.
#' @param ... Arguments passed to \code{\link[spatstat.core]{runifdisc}}, \code{\link[spatstat.geom]{disc}}, \code{\link[spatstat.core]{rpoispp}}, \code{\link[spatstat.geom]{rsyst}}, or \code{\link[spatstat.core]{rNeymanScott}} depending on \code{samp_control} or \code{samp_control}. Arguments also passed to \code{\link[sparr]{risk}} to select bandwidth, edge correction, and resolution.
#' @param ... Arguments passed to \code{\link[spatstat.random]{runifdisc}}, \code{\link[spatstat.geom]{disc}}, \code{\link[spatstat.random]{rpoispp}}, \code{\link[spatstat.geom]{rsyst}}, or \code{\link[spatstat.random]{rNeymanScott}} depending on \code{samp_control} or \code{samp_control}. Arguments also passed to \code{\link[sparr]{risk}} to select bandwidth, edge correction, and resolution.
#' @param cascon `r lifecycle::badge("deprecated")` \code{cascon} is no longer supported and this function will output power for case-only and case/control clustering. This argument has been moved to \code{spatial_plots} function.
#' @param lower_tail `r lifecycle::badge("deprecated")` \code{lower_tail} is no longer supported; this function uses \code{alpha} to set the critical p-value.
#' @param upper_tail `r lifecycle::badge("deprecated")` \code{lupper_tail} is no longer supported; this function uses \code{alpha} to set the critical p-value.
#'
#' @details This function computes the statistical power of the spatial relative risk function (nonparametric estimate of relative risk by kernel smoothing) for randomly generated data using various random point pattern generators from the \code{\link{spatstat.core}} package.
#' @details This function computes the statistical power of the spatial relative risk function (nonparametric estimate of relative risk by kernel smoothing) for randomly generated data using various random point pattern generators from the \code{\link{spatstat.random}} package.
#'
#' The function uses the \code{\link[sparr]{risk}} function to estimate the spatial relative risk function and forces the \code{tolerate} argument to be TRUE in order to calculate asymptotic p-values.
#'
Expand Down Expand Up @@ -86,7 +86,7 @@
#' @importFrom iterators icount
#' @importFrom lifecycle badge deprecate_warn deprecated is_present
#' @importFrom sparr risk
#' @importFrom spatstat.core rNeymanScott rpoispp runifdisc runifpoint
#' @importFrom spatstat.random rNeymanScott rpoispp runifdisc runifpoint
#' @importFrom spatstat.geom as.solist disc marks ppp rsyst shift superimpose unit.square
#' @importFrom stats rnorm sd
#' @export
Expand Down Expand Up @@ -198,7 +198,7 @@ spatial_power <- function(win = spatstat.geom::unit.square(),
rcluster_case <- function(x0, y0, rad, n, scalar, lamb, win, types = "case", ...) {

if (samp_case == "uniform") {
x <- spatstat.core::runifdisc(n = n, radius = rad, centre = c(x0, y0), win = win, ...)
x <- spatstat.random::runifdisc(n = n, radius = rad, centre = c(x0, y0), win = win, ...)
}

if (samp_case == "MVN") {
Expand All @@ -212,7 +212,7 @@ spatial_power <- function(win = spatstat.geom::unit.square(),
if (samp_case == "CSR") {
win_case <- spatstat.geom::disc(radius = rad, centre = c(0.5, 0.5), ...)
l <- n/(diff(win_case$xrange)*diff(win_case$yrange))
x <- spatstat.core::rpoispp(lambda = l, win = win_case, ...)
x <- spatstat.random::rpoispp(lambda = l, win = win_case, ...)
x <- spatstat.geom::shift(x, c(x0 - 0.5, y0 - 0.5))
}

Expand All @@ -221,7 +221,7 @@ spatial_power <- function(win = spatstat.geom::unit.square(),
stop("The argument 'l_case' should be an intensity function")
}
win_case <- spatstat.geom::disc(radius = rad, centre = c(0.5, 0.5), ...)
x <- spatstat.core::rpoispp(lambda = lamb, win = win_case, ...)
x <- spatstat.random::rpoispp(lambda = lamb, win = win_case, ...)
x <- spatstat.geom::shift(x, c(x0 - 0.5, y0 - 0.5))
}

Expand All @@ -232,7 +232,7 @@ spatial_power <- function(win = spatstat.geom::unit.square(),
# marked uniform ppp for controls
rcluster_control <- function(x0, y0, scalar, n, lamb, ex, nclust, rad, types = "control", win, ...) {
if (samp_control == "uniform") {
x <- spatstat.core::runifpoint(n, win = win, ...)
x <- spatstat.random::runifpoint(n, win = win, ...)
}

if (samp_control == "systematic") {
Expand All @@ -249,22 +249,22 @@ spatial_power <- function(win = spatstat.geom::unit.square(),

if (samp_control == "CSR") {
l <- n / (diff(win$xrange) * diff(win$yrange))
x <- spatstat.core::rpoispp(lambda = l, win = win, ...)
x <- spatstat.random::rpoispp(lambda = l, win = win, ...)
}

if (samp_control == "IPP") {
if (class(lamb) != "function") {
stop("The argument 'l_control' should be an intensity function")
}
x <- spatstat.core::rpoispp(lambda = lamb, win = win, ...)
x <- spatstat.random::rpoispp(lambda = lamb, win = win, ...)
}

if (samp_control == "clustered") {
control_clustering <- function(x0, y0, radius, n) {
X <- spatstat.core::runifdisc(n, radius, centre = c(x0, y0))
X <- spatstat.random::runifdisc(n, radius, centre = c(x0, y0))
return(X)
}
x <- spatstat.core::rNeymanScott(kappa = lamb,
x <- spatstat.random::rNeymanScott(kappa = lamb,
expand = ex,
rcluster = control_clustering,
n = nclust,
Expand Down
2 changes: 1 addition & 1 deletion dev/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ t2 <- Sys.time()
t2-t1
spatial_plots(tttt)

data(chorley)
data(chorley, package="spatstat.data")
t1 <- Sys.time()
f1 <- jitter_power(obs_data = unique(chorley),
samp_control = "CSR",
Expand Down
2 changes: 1 addition & 1 deletion man/jitter_power.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/sparrpowR-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4df5d85

Please sign in to comment.