Skip to content

Commit

Permalink
Merge pull request #48 from r-spatialecology/CRAN
Browse files Browse the repository at this point in the history
Cran to master
  • Loading branch information
mhesselbarth authored May 27, 2019
2 parents 0644f9f + c19a9a4 commit af328bb
Show file tree
Hide file tree
Showing 58 changed files with 1,197 additions and 285 deletions.
32 changes: 30 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,40 @@
#'
#' A species with negative associations to habitat 4 of `landscape`.
#'
#' @format A spatstat ppp object
#' @format A spatstat ppp object.
"species_a"

#' Species b
#'
#' A species with positive associations to habitat 5 of `landscape`.
#'
#' @format A spatstat ppp object
#' @format A spatstat ppp object.
"species_b"

#' Gamma test
#'
#' Randomized data for species b using the gamma test.
#'
#' @format rd_pat object.
"gamma_test"

#' Reconstruction
#'
#' Randomized data for species b using pattern reconstruction.
#'
#' @format rd_pat object.
"reconstruction"

#' Torus trans
#'
#' Torus translation of the classified landscape data.
#'
#' @format rd_ras object.
"torus_trans"

#' Random walk
#'
#' Randomization of the landscape data using the habitat randomization algorithm.
#'
#' @format rd_ras object.
"random_walk"
10 changes: 7 additions & 3 deletions R/plot_randomized_pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ plot_randomized_pattern <- function(pattern,
ask = TRUE,
verbose = TRUE){



# check if class is correct
if (!class(pattern) %in% c("rd_pat", "rd_mar")) {
stop("Class of 'pattern' must be 'rd_pat' or 'rd_mar'.",
Expand Down Expand Up @@ -232,6 +230,8 @@ plot_randomized_pattern <- function(pattern,
col = c("black", "#1f78b4"), lty = c(1,2), inset = 0.025)

graphics::par(ask = FALSE)

invisible()
}

else if (class(pattern) == "rd_mar") {
Expand Down Expand Up @@ -306,11 +306,13 @@ plot_randomized_pattern <- function(pattern,
graphics::legend(x = "topright",
legend = c("observed", "randomized"),
col = c("black", "#1f78b4"), lty = c(1, 2), inset = 0.025)

invisible()
}

else{
stop("'method' must be either 'method = 'spatial' or 'method = 'marks'.",
call. =FALSE)
call. = FALSE)
}
}

Expand Down Expand Up @@ -368,6 +370,8 @@ plot_randomized_pattern <- function(pattern,

# reset plotting window settings
graphics::par(mfrow = c(1, 1))

invisible()
}

else{
Expand Down
2 changes: 2 additions & 0 deletions R/plot_randomized_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,6 @@ plot_randomized_raster <- function(raster,
# plot result
raster::plot(raster_stack, col = col, nc = ncol, nr = nrow,
colNA = "grey")

invisible()
}
17 changes: 9 additions & 8 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ output: github_document

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
fig.path = "man/figures/README-"
)
```
Expand Down Expand Up @@ -52,10 +53,10 @@ landscape_classified <- classify_habitats(raster = landscape, classes = 5)

There are two possibilities to randomize the environmental data, both described in Harms et al. (2001). The first shifts the habitat map in all 4 cardinal directions around a torus. The second one assigns the habitat values to an empty map using a random walk algorithm. Both functions return a list with randomized rasters and the observed one. For more information on the methods, please click [here](https://r-spatialecology.github.io/shar/articles/background.html).

```{r habitat_random}
```{r habitat_random, eval = FALSE}
torus_trans <- translate_raster(raster = landscape_classified, verbose = FALSE)
random_walk <- randomize_raster(raster = landscape_classified, n_random = 19, verbose = FALSE)
random_walk <- randomize_raster(raster = landscape_classified, n_random = 39, verbose = FALSE)
```

```{r plot_habitat-random, fig.width = 5.5, fig.height = 4, fig.align = "center"}
Expand All @@ -66,28 +67,28 @@ plot_randomized_raster(torus_trans, n = 3, col = col)

To randomize the point pattern, either use the Gamma test described by Plotkin et al. (2000) or pattern reconstruction (Tscheschel & Stoyan 2006).

```{r pattern-random}
gamma_test <- fit_point_process(pattern = species_a, process = "cluster", n_random = 19, verbose = FALSE)
```{r pattern-random, eval = FALSE}
gamma_test <- fit_point_process(pattern = species_a, process = "cluster", n_random = 39, verbose = FALSE)
reconstruct <- reconstruct_pattern(pattern = species_b, max_runs = 500, n_random = 19, verbose = FALSE) # takes some time
reconstruction <- reconstruct_pattern(pattern = species_b, n_random = 39, verbose = FALSE) # takes some time
```

Of coures, there are several utility functions. For example, you can plot a randomized pattern or calculate the differences between the observed pattern and the randomized patterns (using summary functions).

```{r plot-random_pattern, fig.width = 5.5, fig.height = 4, fig.align = "center"}
plot_randomized_pattern(reconstruct, verbose = FALSE, ask = FALSE)
plot_randomized_pattern(reconstruction, verbose = FALSE, ask = FALSE)
```

```{r calculate-energy}
calculate_energy(reconstruct, verbose = FALSE)
calculate_energy(reconstruction, verbose = FALSE)
```

The data was created that `species_a` has a negative association to habitat 4 and `species_b` has a positive association to habitat 5. At one point a posititive association to one habitat leads consequently to a negative association to another habitat (and vice versa). All this can be seen in the results.

```{r results}
results_habitat_association(pattern = species_a, raster = torus_trans)
results_habitat_association(pattern = reconstruct, raster = landscape_classified)
results_habitat_association(pattern = reconstruction, raster = landscape_classified)
```

## References
Expand Down
65 changes: 32 additions & 33 deletions README.html

Large diffs are not rendered by default.

62 changes: 29 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ status](https://travis-ci.org/r-spatialecology/shar.svg?branch=master)](https://
status](https://ci.appveyor.com/api/projects/status/08hgwkr82pqb6ykq/branch/master?svg=true)](https://ci.appveyor.com/project/mhesselbarth/shar/branch/master)
[![Coverage
status](https://codecov.io/gh/r-spatialecology/shar/branch/master/graph/badge.svg)](https://codecov.io/gh/r-spatialecology/shar?branch=master)
[![Project Status: Active – The project has reached a stable, usable
[![Project Status: Active The project has reached a stable, usable
state and is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing)
Expand Down Expand Up @@ -78,7 +78,7 @@ For more information on the methods, please click
``` r
torus_trans <- translate_raster(raster = landscape_classified, verbose = FALSE)

random_walk <- randomize_raster(raster = landscape_classified, n_random = 19, verbose = FALSE)
random_walk <- randomize_raster(raster = landscape_classified, n_random = 39, verbose = FALSE)
```

``` r
Expand All @@ -94,30 +94,32 @@ Plotkin et al. (2000) or pattern reconstruction (Tscheschel & Stoyan
2006).

``` r
gamma_test <- fit_point_process(pattern = species_a, process = "cluster", n_random = 19, verbose = FALSE)
gamma_test <- fit_point_process(pattern = species_a, process = "cluster", n_random = 39, verbose = FALSE)

reconstruct <- reconstruct_pattern(pattern = species_b, max_runs = 500, n_random = 19, verbose = FALSE) # takes some time
reconstruction <- reconstruct_pattern(pattern = species_b, n_random = 39, verbose = FALSE) # takes some time
```

Of coures, there are several utility functions. For example, you can
plot a randomized pattern or calculate the differences between the
observed pattern and the randomized patterns (using summary functions).

``` r
plot_randomized_pattern(reconstruct, verbose = FALSE, ask = FALSE)
plot_randomized_pattern(reconstruction, verbose = FALSE, ask = FALSE)
```

<img src="man/figures/README-plot-random_pattern-1.png" style="display: block; margin: auto;" /><img src="man/figures/README-plot-random_pattern-2.png" style="display: block; margin: auto;" />

``` r
calculate_energy(reconstruct, verbose = FALSE)
calculate_energy(reconstruction, verbose = FALSE)
#> [1] 0.01356462 0.01867389 0.01455831 0.01754614 0.01858267 0.02008615
#> [7] 0.01544740 0.01470591 0.01255234 0.01837727 0.01622154 0.01765028
#> [13] 0.01517141 0.02075634 0.01836698 0.01523319 0.01832501 0.02072956
#> [19] 0.01845929 0.01733616 0.01639753 0.01887276 0.01738685 0.01361427
#> [25] 0.01619153 0.01695853 0.01856459 0.01692591 0.01986748 0.01604911
#> [31] 0.01972613 0.01518272 0.01681647 0.02010567 0.01472619 0.01794340
#> [37] 0.01740046 0.01531350 0.01869501
```

## [1] 0.06296249 0.04206978 0.05765356 0.04559935 0.07282586 0.05059367
## [7] 0.04891124 0.06394066 0.04618339 0.05240480 0.05701905 0.07678296
## [13] 0.05384696 0.05210462 0.06433170 0.04546687 0.03842786 0.04996899
## [19] 0.05361466

The data was created that `species_a` has a negative association to
habitat 4 and `species_b` has a positive association to habitat 5. At
one point a posititive association to one habitat leads consequently to
Expand All @@ -126,30 +128,24 @@ be seen in the results.

``` r
results_habitat_association(pattern = species_a, raster = torus_trans)
#> > Input: randomized raster | Quantile thresholds: negative < 0.025 - positive > 0.975
#> habitat count lo hi significance
#> 1 1 9 2 14 n.s.
#> 2 2 25 9 24 positive
#> 3 3 27 11 26 positive
#> 4 4 0 11 29 negative
#> 5 5 12 5 18 n.s.

results_habitat_association(pattern = reconstruction, raster = landscape_classified)
#> > Input: randomized point pattern | Quantile thresholds: negative < 0.025 - positive > 0.975
#> habitat count lo hi significance
#> 1 1 8 4.85 43.15 n.s.
#> 2 2 22 26.80 68.10 negative
#> 3 3 33 46.80 74.10 negative
#> 4 4 19 29.80 69.10 negative
#> 5 5 118 9.95 37.05 positive
```

## > Input: randomized raster | Quantile thresholds: negative < 0.025 - positive > 0.975

## habitat count lo hi significance
## 1 1 10 0 8 positive
## 2 2 14 8 24 n.s.
## 3 3 30 14 29 positive
## 4 4 0 10 26 negative
## 5 5 14 4 17 n.s.

``` r
results_habitat_association(pattern = reconstruct, raster = landscape_classified)
```

## > Input: randomized point pattern | Quantile thresholds: negative < 0.025 - positive > 0.975

## habitat count lo hi significance
## 1 1 7 10.45 26.75 negative
## 2 2 20 31.35 49.10 negative
## 3 3 31 50.45 73.55 negative
## 4 4 33 36.45 59.65 negative
## 5 5 109 23.45 43.65 positive

## References

Baddeley, A., Rubak, E., Turner, R. (2015). Spatial Point Patterns:
Expand Down
56 changes: 47 additions & 9 deletions data-raw/example_data.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library(dplyr)
library(NLMR)
library(maptools)
library(usethis)
library(spatstat)

#### Create example data ####
Expand All @@ -19,32 +20,69 @@ landscape_class <- shar::classify_habitats(landscape, classes = 5)
# Create species with negative
pattern_a <- spatstat::runifpoint(n = 100, win = spatstat::owin(c(0, 1000), c(0, 1000)))

owin_pattern <- raster::rasterToPolygons(landscape_class, fun = function(x){x == 4}, dissolve = TRUE) %>%
owin_pattern <- raster::rasterToPolygons(landscape_class,
fun = function(x){x == 4},
dissolve = TRUE) %>%
maptools::as.owin.SpatialPolygons()

pattern_b <- pattern_a[!spatstat::inside.owin(x = pattern_a, w = owin_pattern)]
pattern_c <- spatstat::rthin(pattern_a[spatstat::inside.owin(x = pattern_a, w = owin_pattern)], 0)
pattern_c <- spatstat::rthin(pattern_a[spatstat::inside.owin(x = pattern_a,
w = owin_pattern)], 0)

species_a <- spatstat::superimpose(pattern_b, pattern_c,
W = spatstat::owin(c(0, 1000), c(0, 1000)))

marks_df_a <- data.frame(status = factor(sample(c("dead", "alive"), size = species_a$n, replace = TRUE)),
marks_df_a <- data.frame(status = factor(sample(c("dead", "alive"),
size = species_a$n, replace = TRUE)),
dbh = runif(n = species_a$n, min = 5, max = 65))

spatstat::marks(species_a) <- marks_df_a

# Create species with positive associations
pattern <- spatstat::runifpoint(n = 100, win = spatstat::owin(c(0, 1000), c(0, 1000)))

species_b <- raster::rasterToPolygons(landscape_class, fun = function(x){x == 5}, dissolve = TRUE) %>%
species_b <- raster::rasterToPolygons(landscape_class,
fun = function(x){x == 5}, dissolve = TRUE) %>%
maptools::as.owin.SpatialPolygons() %>%
spatstat::runifpoint(n = floor(pattern$n * 1), win=.) %>%
spatstat::runifpoint(n = floor(pattern$n * 1), win = .) %>%
spatstat::superimpose.ppp(pattern, W = spatstat::owin(c(0, 1000), c(0, 1000)))

marks_df_b <- factor(sample(c("dominant", "understorey"), size = species_b$n, replace = TRUE))
marks_df_b <- factor(sample(c("dominant", "understorey"),
size = species_b$n, replace = TRUE))

spatstat::marks(species_b) <- marks_df_b

devtools::use_data(landscape, overwrite = TRUE)
devtools::use_data(species_a, overwrite = TRUE)
devtools::use_data(species_b, overwrite = TRUE)
# translate raster
torus_trans <- translate_raster(raster = landscape_class, verbose = FALSE)

# use randomization algorithm
random_walk <- randomize_raster(raster = landscape_class,
n_random = 39, verbose = FALSE)

# use gamma test
gamma_test <- fit_point_process(pattern = species_b, process = "cluster",
n_random = 39, verbose = FALSE)

# use pattern reconstruction
reconstruction <- reconstruct_pattern(pattern = species_b,
n_random = 39, verbose = FALSE)

#### Save data ####

# save landscape
usethis::use_data(landscape, overwrite = TRUE)

# save species data
usethis::use_data(species_a, overwrite = TRUE)

usethis::use_data(species_b, overwrite = TRUE)

# save random landscape data
usethis::use_data(torus_trans, overwrite = TRUE)

usethis::use_data(random_walk, overwrite = TRUE)

# save random point data
usethis::use_data(gamma_test, overwrite = TRUE)

usethis::use_data(reconstruction, overwrite = TRUE)
Binary file added data/gamma_test.rda
Binary file not shown.
Binary file modified data/landscape.rda
Binary file not shown.
Binary file added data/random_walk.rda
Binary file not shown.
Binary file added data/reconstruction.rda
Binary file not shown.
Binary file modified data/species_a.rda
Binary file not shown.
Binary file modified data/species_b.rda
Binary file not shown.
Binary file added data/torus_trans.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/articles/articles/background.html

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

2 changes: 1 addition & 1 deletion docs/articles/articles/publication_record.html

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

Loading

0 comments on commit af328bb

Please sign in to comment.