18
18
# ' K-function. For more information see \code{\link{estimate_pcf_fast}}.
19
19
# '
20
20
# ' @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
22
25
# ' \code{\link{plot_randomized_pattern}}
23
26
# '
24
27
# ' @return numeric
30
33
# '
31
34
# ' \dontrun{
32
35
# ' 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)
34
38
# ' calculate_energy(marks_recon, return_mean = FALSE)
35
39
# ' }
36
40
# '
@@ -53,20 +57,22 @@ calculate_energy <- function(pattern,
53
57
54
58
# check if class is correct
55
59
if (! class(pattern ) %in% c(" rd_pat" , " rd_mar" )) {
60
+
56
61
stop(" Class of 'pattern' must be 'rd_pat' or 'rd_mar'." ,
57
62
call. = FALSE )
58
63
}
59
64
60
65
# check if observed pattern is present
61
- if (! " observed" %in% names(pattern )) {
66
+ if (! spatstat :: is.ppp(pattern $ observed )) {
67
+
62
68
stop(" Input must include 'observed' pattern." , call. = FALSE )
63
69
}
64
70
65
71
# extract observed pattern
66
72
pattern_observed <- pattern $ observed
67
73
68
74
# extract randomized patterns
69
- pattern_reconstructed <- pattern [names( pattern ) != " observed " ]
75
+ pattern_randomized <- pattern $ randomized
70
76
71
77
# calculate r sequence
72
78
r <- seq(from = 0 ,
@@ -76,117 +82,144 @@ calculate_energy <- function(pattern,
76
82
77
83
if (class(pattern ) == " rd_pat" ) {
78
84
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 ))
83
90
84
- # check if number of points exceed comp_fast limit
85
- if (pattern_observed $ n > comp_fast ) {
86
- comp_fast <- TRUE
87
91
}
88
92
89
93
else {
90
- comp_fast <- FALSE
91
- }
92
94
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
+ }
95
99
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
+ }
99
104
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
+ }
106
108
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 )
108
128
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" ,
111
132
r = r )
133
+ }
112
134
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 ) {
118
137
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 ) {
121
140
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 )
124
144
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
+ }
128
151
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 {
135
154
136
- # normal computation of summary stats
137
- else {
155
+ gest_reconstruction <- spatstat :: Gest(X = pattern_randomized [[x ]],
156
+ correction = " han" ,
157
+ r = r )
138
158
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" ,
141
162
r = r )
163
+ }
142
164
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 ]])
148
168
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
+ }
152
174
153
- # print progress
154
- if (verbose ) {
155
- message(" \r > Progress: " , x , " /" , length(pattern_reconstructed ), " \t\t " ,
156
- appendLF = FALSE )
157
- }
175
+ return (energy )
158
176
159
- return (energy )
177
+ }, FUN.VALUE = numeric (1 ))
178
+ }
160
179
161
- }, FUN.VALUE = numeric (1 ))
180
+ # set names
181
+ names(result ) <- paste0(" randomized_" , seq_along(result ))
162
182
}
163
183
164
184
else if (class(pattern ) == " rd_mar" ) {
165
185
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 )) {
170
188
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 {
172
194
173
195
# 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 )
177
199
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 ) {
180
201
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
+ }
186
215
187
- return (energy )
216
+ return (energy )
217
+
218
+ }, FUN.VALUE = numeric (1 ))
219
+ }
188
220
189
- }, FUN.VALUE = numeric (1 ))
221
+ # set names
222
+ names(result ) <- paste0(" randomized_" , seq_along(result ))
190
223
}
191
224
192
225
# return mean for all reconstructed patterns
0 commit comments