Skip to content

Commit 2fc8b78

Browse files
committed
Merge branch 'master' into CRAN
2 parents 0400fbd + e63ab8c commit 2fc8b78

File tree

110 files changed

+5017
-1913
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+5017
-1913
lines changed

DESCRIPTION

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: shar
33
Title: Species-Habitat Associations
4-
Version: 0.5
4+
Version: 1.0
55
Authors@R: c(person("Maximillian H.K.", "Hesselbarth", email = "[email protected]",
66
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-1125-9918")),
77
person("Marco", "Sciaini", email = "[email protected]",
@@ -21,6 +21,7 @@ Imports:
2121
classInt,
2222
graphics,
2323
grDevices,
24+
methods,
2425
raster,
2526
spatstat,
2627
stats,
@@ -30,7 +31,7 @@ RoxygenNote: 6.1.1
3031
Suggests:
3132
covr,
3233
dplyr,
33-
testthat,
34+
testthat (>= 2.1.0),
3435
knitr,
3536
rmarkdown
3637
VignetteBuilder: knitr

NAMESPACE

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(print,rd_mar)
4+
S3method(print,rd_pat)
5+
S3method(print,rd_ras)
36
export(calculate_energy)
47
export(classify_habitats)
58
export(create_neighbourhood)
69
export(estimate_pcf_fast)
710
export(extract_points)
811
export(fit_point_process)
12+
export(plot_energy)
913
export(plot_randomized_pattern)
1014
export(plot_randomized_raster)
1115
export(randomize_raster)
1216
export(rcpp_sample)
13-
export(reconstruct_marks)
14-
export(reconstruct_pattern)
17+
export(reconstruct_pattern_cluster)
18+
export(reconstruct_pattern_hetero)
19+
export(reconstruct_pattern_homo)
20+
export(reconstruct_pattern_marks)
1521
export(results_habitat_association)
1622
export(translate_raster)
1723
importFrom(Rcpp,sourceCpp)

NEWS.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# shar 1.0
2+
* Improvements
3+
* Printing methods for most objects
4+
* Possibility to specify intervals of r for all reconstruction functions
5+
* New functionality
6+
* `plot_energy()` to plot energy over iterations for reconstructed patterns
7+
* `reconstruct_pattern_hetero()` allows to reconstruct heterogeneous patterns
8+
* Renameing/Structure
9+
* `reconstruct_pattern()` was split to three functions: `reconstruct_pattern_homo()`, `reconstruct_pattern_hetero()`, `reconstruct_pattern_cluster()`,
10+
* `reconstruct_marks()` is now called `reconstruct_pattern_marks()`
11+
112
# shar 0.5
213
* Improvements
314
* Annealing probability can be specified for reconstruction

R/calculate_energy.R

+114-81
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#' K-function. For more information see \code{\link{estimate_pcf_fast}}.
1919
#'
2020
#' @seealso
21-
#' \code{\link{reconstruct_pattern}} \cr
21+
#' \code{\link{plot_energy}} \cr
22+
#' \code{\link{reconstruct_pattern_homo}} \cr
23+
#' \code{\link{reconstruct_pattern_hetero}} \cr
24+
#' \code{\link{reconstruct_pattern_cluster}} \cr
2225
#' \code{\link{plot_randomized_pattern}}
2326
#'
2427
#' @return numeric
@@ -30,7 +33,8 @@
3033
#'
3134
#' \dontrun{
3235
#' marks_sub <- spatstat::subset.ppp(species_a, select = dbh)
33-
#' marks_recon <- reconstruct_marks(pattern_random[[1]], marks_sub, n_random = 19, max_runs = 1000)
36+
#' marks_recon <- reconstruct_pattern_marks(pattern_random$randomized[[1]], marks_sub,
37+
#' n_random = 19, max_runs = 1000)
3438
#' calculate_energy(marks_recon, return_mean = FALSE)
3539
#' }
3640
#'
@@ -53,20 +57,22 @@ calculate_energy <- function(pattern,
5357

5458
# check if class is correct
5559
if (!class(pattern) %in% c("rd_pat", "rd_mar")) {
60+
5661
stop("Class of 'pattern' must be 'rd_pat' or 'rd_mar'.",
5762
call. = FALSE)
5863
}
5964

6065
# check if observed pattern is present
61-
if (!"observed" %in% names(pattern)) {
66+
if (!spatstat::is.ppp(pattern$observed)) {
67+
6268
stop("Input must include 'observed' pattern.", call. = FALSE)
6369
}
6470

6571
# extract observed pattern
6672
pattern_observed <- pattern$observed
6773

6874
# extract randomized patterns
69-
pattern_reconstructed <- pattern[names(pattern) != "observed"]
75+
pattern_randomized <- pattern$randomized
7076

7177
# calculate r sequence
7278
r <- seq(from = 0,
@@ -76,117 +82,144 @@ calculate_energy <- function(pattern,
7682

7783
if (class(pattern) == "rd_pat") {
7884

79-
# check if weights make sense
80-
if (sum(weights) > 1 || sum(weights) == 0) {
81-
stop("The sum of 'weights' must be 0 < sum(weights) <= 1.", call. = FALSE)
82-
}
85+
# get energy from df
86+
if (is.list(pattern$energy_df)) {
87+
88+
result <- vapply(pattern$energy_df, FUN = function(x) utils::tail(x, n = 1)[[2]],
89+
FUN.VALUE = numeric(1))
8390

84-
# check if number of points exceed comp_fast limit
85-
if (pattern_observed$n > comp_fast) {
86-
comp_fast <- TRUE
8791
}
8892

8993
else {
90-
comp_fast <- FALSE
91-
}
9294

93-
# calculate summary functions for observed pattern
94-
if (comp_fast) {
95+
# check if weights make sense
96+
if (sum(weights) > 1 || sum(weights) == 0) {
97+
stop("The sum of 'weights' must be 0 < sum(weights) <= 1.", call. = FALSE)
98+
}
9599

96-
gest_observed <- spatstat::Gest(X = pattern_observed,
97-
correction = "none",
98-
r = r)
100+
# check if number of points exceed comp_fast limit
101+
if (pattern_observed$n > comp_fast) {
102+
comp_fast <- TRUE
103+
}
99104

100-
pcf_observed <- shar::estimate_pcf_fast(pattern = pattern_observed,
101-
correction = "none",
102-
method = "c",
103-
spar = 0.5,
104-
r = r)
105-
}
105+
else {
106+
comp_fast <- FALSE
107+
}
106108

107-
else{
109+
# calculate summary functions for observed pattern
110+
if (comp_fast) {
111+
112+
gest_observed <- spatstat::Gest(X = pattern_observed,
113+
correction = "none",
114+
r = r)
115+
116+
pcf_observed <- shar::estimate_pcf_fast(pattern = pattern_observed,
117+
correction = "none",
118+
method = "c",
119+
spar = 0.5,
120+
r = r)
121+
}
122+
123+
else{
124+
125+
gest_observed <- spatstat::Gest(X = pattern_observed,
126+
correction = "han",
127+
r = r)
108128

109-
gest_observed <- spatstat::Gest(X = pattern_observed,
110-
correction = "han",
129+
pcf_observed <- spatstat::pcf(X = pattern_observed,
130+
correction = "best",
131+
divisor = "d",
111132
r = r)
133+
}
112134

113-
pcf_observed <- spatstat::pcf(X = pattern_observed,
114-
correction = "best",
115-
divisor = "d",
116-
r = r)
117-
}
135+
# loop through all reconstructed patterns
136+
result <- vapply(seq_along(pattern_randomized), function(x) {
118137

119-
# loop through all reconstructed patterns
120-
result <- vapply(seq_along(pattern_reconstructed), function(x) {
138+
# fast computation of summary stats
139+
if (comp_fast) {
121140

122-
# fast computation of summary stats
123-
if (comp_fast) {
141+
gest_reconstruction <- spatstat::Gest(X = pattern_randomized[[x]],
142+
correction = "none",
143+
r = r)
124144

125-
gest_reconstruction <- spatstat::Gest(X = pattern_reconstructed[[x]],
126-
correction = "none",
127-
r = r)
145+
pcf_reconstruction <- shar::estimate_pcf_fast(pattern = pattern_randomized[[x]],
146+
correction = "none",
147+
method = "c",
148+
spar = 0.5,
149+
r = r)
150+
}
128151

129-
pcf_reconstruction <- shar::estimate_pcf_fast(pattern = pattern_reconstructed[[x]],
130-
correction = "none",
131-
method = "c",
132-
spar = 0.5,
133-
r = r)
134-
}
152+
# normal computation of summary stats
153+
else{
135154

136-
# normal computation of summary stats
137-
else{
155+
gest_reconstruction <- spatstat::Gest(X = pattern_randomized[[x]],
156+
correction = "han",
157+
r = r)
138158

139-
gest_reconstruction <- spatstat::Gest(X = pattern_reconstructed[[x]],
140-
correction = "han",
159+
pcf_reconstruction <- spatstat::pcf(X = pattern_randomized[[x]],
160+
correction = "best",
161+
divisor = "d",
141162
r = r)
163+
}
142164

143-
pcf_reconstruction <- spatstat::pcf(X = pattern_reconstructed[[x]],
144-
correction = "best",
145-
divisor = "d",
146-
r = r)
147-
}
165+
# difference between observed and reconstructed pattern
166+
energy <- (mean(abs(gest_observed[[3]] - gest_reconstruction[[3]]), na.rm = TRUE) * weights[[1]]) +
167+
(mean(abs(pcf_observed[[3]] - pcf_reconstruction[[3]]), na.rm = TRUE) * weights[[2]])
148168

149-
# difference between observed and reconstructed pattern
150-
energy <- (mean(abs(gest_observed[[3]] - gest_reconstruction[[3]]), na.rm = TRUE) * weights[[1]]) +
151-
(mean(abs(pcf_observed[[3]] - pcf_reconstruction[[3]]), na.rm = TRUE) * weights[[2]])
169+
# print progress
170+
if (verbose) {
171+
message("\r> Progress: ", x, "/", length(pattern_randomized), "\t\t",
172+
appendLF = FALSE)
173+
}
152174

153-
# print progress
154-
if (verbose) {
155-
message("\r> Progress: ", x, "/", length(pattern_reconstructed), "\t\t",
156-
appendLF = FALSE)
157-
}
175+
return(energy)
158176

159-
return(energy)
177+
}, FUN.VALUE = numeric(1))
178+
}
160179

161-
}, FUN.VALUE = numeric(1))
180+
# set names
181+
names(result) <- paste0("randomized_", seq_along(result))
162182
}
163183

164184
else if (class(pattern) == "rd_mar") {
165185

166-
# calculate summary functions
167-
kmmr_observed <- spatstat::markcorr(pattern_observed,
168-
correction = "Ripley",
169-
r = r)
186+
# get energy from df
187+
if (is.list(pattern$energy_df)) {
170188

171-
result <- vapply(seq_along(pattern_reconstructed), function(x) {
189+
result <- vapply(pattern$energy_df, FUN = function(x) utils::tail(x, n = 1)[[2]],
190+
FUN.VALUE = numeric(1))
191+
}
192+
193+
else {
172194

173195
# calculate summary functions
174-
kmmr_reconstruction <- spatstat::markcorr(pattern_reconstructed[[x]],
175-
correction = "Ripley",
176-
r = r)
196+
kmmr_observed <- spatstat::markcorr(pattern_observed,
197+
correction = "Ripley",
198+
r = r)
177199

178-
# difference between observed and reconstructed pattern
179-
energy <- mean(abs(kmmr_observed[[3]] - kmmr_reconstruction[[3]]), na.rm = TRUE)
200+
result <- vapply(seq_along(pattern_randomized), function(x) {
180201

181-
# print progress
182-
if (verbose) {
183-
message("\r> Progress: ", x, "/", length(pattern_reconstructed), "\t\t",
184-
appendLF = FALSE)
185-
}
202+
# calculate summary functions
203+
kmmr_reconstruction <- spatstat::markcorr(pattern_randomized[[x]],
204+
correction = "Ripley",
205+
r = r)
206+
207+
# difference between observed and reconstructed pattern
208+
energy <- mean(abs(kmmr_observed[[3]] - kmmr_reconstruction[[3]]), na.rm = TRUE)
209+
210+
# print progress
211+
if (verbose) {
212+
message("\r> Progress: ", x, "/", length(pattern_randomized), "\t\t",
213+
appendLF = FALSE)
214+
}
186215

187-
return(energy)
216+
return(energy)
217+
218+
}, FUN.VALUE = numeric(1))
219+
}
188220

189-
}, FUN.VALUE = numeric(1))
221+
# set names
222+
names(result) <- paste0("randomized_", seq_along(result))
190223
}
191224

192225
# return mean for all reconstructed patterns

0 commit comments

Comments
 (0)