@@ -4,7 +4,6 @@ NNS.M.reg <- function (X_n, Y, factor.2.dummy = TRUE, order = NULL, stn = NULL,
4
4
5
5
dist <- tolower(dist )
6
6
7
-
8
7
# ## For Multiple regressions
9
8
# ## Turn each column into numeric values
10
9
original.IVs <- X_n
@@ -190,99 +189,191 @@ NNS.M.reg <- function (X_n, Y, factor.2.dummy = TRUE, order = NULL, stn = NULL,
190
189
191
190
192
191
193
- # ## Point estimates
194
- if (! is.null(point.est )){
192
+ # ## Point Estimates
193
+ if (! is.null(point.est )) {
195
194
196
- # ## Point estimates
197
- central.points <- apply(REGRESSION.POINT.MATRIX [, .SD , .SDcols = 1 : n ], 2 , function ( x ) gravity( x ) )
195
+ # Calculate central points
196
+ central.points <- apply(REGRESSION.POINT.MATRIX [, .SD , .SDcols = 1 : n ], 2 , gravity )
198
197
199
198
predict.fit <- numeric ()
200
-
201
- outsiders <- point.est < minimums | point.est > maximums
199
+ outsiders <- point.est < minimums | point.est > maximums
202
200
outsiders [is.na(outsiders )] <- 0
203
201
204
- if (is.null(np )){
205
- l <- length(point.est )
206
-
207
- if (! any(outsiders )){
208
- predict.fit <- NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = point.est , k = n.best , class = type )
202
+ # Single point estimation
203
+ if (is.null(np )) {
204
+ if (! any(outsiders )) {
205
+ predict.fit <- NNS :: NNS.distance(
206
+ rpm = REGRESSION.POINT.MATRIX ,
207
+ dist.estimate = point.est ,
208
+ k = n.best ,
209
+ class = type
210
+ )
209
211
} else {
210
212
boundary.points <- pmin(pmax(point.est , minimums ), maximums )
211
213
mid.points <- (boundary.points + central.points ) / 2
212
214
mid.points_2 <- (boundary.points + mid.points ) / 2
213
- last.known.distance_1 <- sqrt(sum((boundary.points - central.points ) ^ 2 ))
214
- last.known.distance_2 <- sqrt(sum((boundary.points - mid.points ) ^ 2 ))
215
- last.known.distance_3 <- sqrt(sum((boundary.points - mid.points_2 ) ^ 2 ))
216
215
217
- boundary.estimates <- NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = boundary.points , k = n.best , class = type )
216
+ last.known.distances <- c(
217
+ sqrt(sum((boundary.points - central.points ) ^ 2 )),
218
+ sqrt(sum((boundary.points - mid.points ) ^ 2 )),
219
+ sqrt(sum((boundary.points - mid.points_2 ) ^ 2 ))
220
+ )
218
221
219
- last.known.gradient_1 <- (boundary.estimates - NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = central.points , k = n.best , class = type )) / last.known.distance_1
220
- last.known.gradient_2 <- (boundary.estimates - NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = mid.points , k = n.best , class = type )) / last.known.distance_2
221
- last.known.gradient_3 <- (boundary.estimates - NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = mid.points_2 , k = n.best , class = type )) / last.known.distance_3
222
+ boundary.estimates <- NNS :: NNS.distance(
223
+ rpm = REGRESSION.POINT.MATRIX ,
224
+ dist.estimate = boundary.points ,
225
+ k = n.best ,
226
+ class = type
227
+ )
222
228
223
- last.known.gradient <- (last.known.gradient_1 * 3 + last.known.gradient_2 * 2 + last.known.gradient_3 ) / 6
229
+ gradients <- sapply(1 : 3 , function (i ) {
230
+ compare.points <- list (central.points , mid.points , mid.points_2 )[[i ]]
231
+ (boundary.estimates - NNS :: NNS.distance(
232
+ rpm = REGRESSION.POINT.MATRIX ,
233
+ dist.estimate = compare.points ,
234
+ k = n.best ,
235
+ class = type
236
+ )) / last.known.distances [i ]
237
+ })
224
238
239
+ last.known.gradient <- sum(gradients * c(3 , 2 , 1 )) / 6
225
240
last.distance <- sqrt(sum((point.est - boundary.points ) ^ 2 ))
226
241
227
242
predict.fit <- last.distance * last.known.gradient + boundary.estimates
228
243
}
229
244
}
230
245
231
- if (! is.null(np )){
246
+ # Multiple point estimation
247
+ if (! is.null(np )) {
232
248
DISTANCES <- vector(mode = " list" , np )
233
249
distances <- data.table :: data.table(point.est )
234
- if (num_cores > 1 ){
235
- DISTANCES <- parallel :: parApply(cl , distances , 1 , function (z ) NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = z , k = n.best , class = type )[1 ])
236
-
237
- doParallel :: stopImplicitCluster()
238
- foreach :: registerDoSEQ()
239
- invisible (data.table :: setDTthreads(0 , throttle = NULL ))
240
- invisible (gc(verbose = FALSE ))
250
+
251
+ if (num_cores > 1 ) {
252
+ DISTANCES <- parallel :: parApply(
253
+ cl ,
254
+ distances ,
255
+ 1 ,
256
+ function (z ) NNS.distance(
257
+ rpm = REGRESSION.POINT.MATRIX ,
258
+ dist.estimate = z ,
259
+ k = n.best ,
260
+ class = type
261
+ )[1 ]
262
+ )
241
263
} else {
242
- distances <- distances [, DISTANCES : = NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = .SD , k = n.best , class = type )[1 ], by = 1 : nrow(point.est )]
264
+ distances <- distances [, DISTANCES : = NNS.distance(
265
+ rpm = REGRESSION.POINT.MATRIX ,
266
+ dist.estimate = .SD ,
267
+ k = n.best ,
268
+ class = type
269
+ )[1 ], by = 1 : nrow(point.est )]
243
270
244
271
DISTANCES <- as.numeric(unlist(distances $ DISTANCES ))
245
272
}
246
-
247
- if (any( outsiders > 0 )){
248
- outsiders <- rowSums(outsiders )
249
- outside.index <- as.numeric( which(outsiders > 0 ) )
273
+
274
+ # Parallel handling for outsiders
275
+ if (any( rowSums(outsiders ) > 0 )) {
276
+ outsider.indices <- which(rowSums( outsiders ) > 0 )
250
277
251
- for (i in outside.index ){
252
- outside.points <- point.est [i ,]
253
- boundary.points <- pmin(pmax(outside.points , minimums ), maximums )
254
- mid.points <- (boundary.points + central.points ) / 2
255
- mid.points_2 <- (boundary.points + mid.points ) / 2
256
- last.known.distance_1 <- sqrt(sum((boundary.points - central.points ) ^ 2 ))
257
- last.known.distance_2 <- sqrt(sum((boundary.points - mid.points ) ^ 2 ))
258
- last.known.distance_3 <- sqrt(sum((boundary.points - mid.points_2 ) ^ 2 ))
259
-
260
- boundary.estimates <- NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX ,
261
- dist.estimate = boundary.points ,
262
- k = n.best , class = type )
263
-
264
- last.known.gradient_1 <- (boundary.estimates - NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = central.points , k = n.best , class = type )) / last.known.distance_1
265
- last.known.gradient_2 <- (boundary.estimates - NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = mid.points , k = n.best , class = type )) / last.known.distance_2
266
- last.known.gradient_3 <- (boundary.estimates - NNS :: NNS.distance(rpm = REGRESSION.POINT.MATRIX , dist.estimate = mid.points_2 , k = n.best , class = type )) / last.known.distance_3
267
-
268
- last.known.gradient <- (last.known.gradient_1 * 3 + last.known.gradient_2 * 2 + last.known.gradient_3 ) / 6
269
-
270
- last.distance <- sqrt(sum((outside.points - boundary.points ) ^ 2 ))
271
-
272
-
273
- DISTANCES [i ] <- last.distance * last.known.gradient + boundary.estimates
278
+ if (num_cores > 1 ) {
279
+ DISTANCES [outsider.indices ] <- unlist(parallel :: parApply(
280
+ cl ,
281
+ as.matrix(point.est [outsider.indices , ]),
282
+ 1 ,
283
+ function (outside.points ) {
284
+ boundary.points <- pmin(pmax(outside.points , minimums ), maximums )
285
+ mid.points <- (boundary.points + central.points ) / 2
286
+ mid.points_2 <- (boundary.points + mid.points ) / 2
287
+
288
+ last.known.distances <- c(
289
+ sqrt(sum((boundary.points - central.points ) ^ 2 )),
290
+ sqrt(sum((boundary.points - mid.points ) ^ 2 )),
291
+ sqrt(sum((boundary.points - mid.points_2 ) ^ 2 ))
292
+ )
293
+
294
+ boundary.estimates <- NNS :: NNS.distance(
295
+ rpm = REGRESSION.POINT.MATRIX ,
296
+ dist.estimate = boundary.points ,
297
+ k = n.best ,
298
+ class = type
299
+ )
300
+
301
+ gradients <- sapply(1 : 3 , function (i ) {
302
+ compare.points <- list (central.points , mid.points , mid.points_2 )[[i ]]
303
+ (boundary.estimates - NNS :: NNS.distance(
304
+ rpm = REGRESSION.POINT.MATRIX ,
305
+ dist.estimate = compare.points ,
306
+ k = n.best ,
307
+ class = type
308
+ )) / last.known.distances [i ]
309
+ })
310
+
311
+ last.known.gradient <- sum(gradients * c(3 , 2 , 1 )) / 6
312
+ last.distance <- sqrt(sum((outside.points - boundary.points ) ^ 2 ))
313
+
314
+ last.distance * last.known.gradient + boundary.estimates
315
+ }
316
+ ))
317
+ } else {
318
+ DISTANCES [outsider.indices ] <- apply(
319
+ as.matrix(point.est [outsider.indices , ]),
320
+ 1 ,
321
+ function (outside.points ) {
322
+ boundary.points <- pmin(pmax(outside.points , minimums ), maximums )
323
+ mid.points <- (boundary.points + central.points ) / 2
324
+ mid.points_2 <- (boundary.points + mid.points ) / 2
325
+
326
+ last.known.distances <- c(
327
+ sqrt(sum((boundary.points - central.points ) ^ 2 )),
328
+ sqrt(sum((boundary.points - mid.points ) ^ 2 )),
329
+ sqrt(sum((boundary.points - mid.points_2 ) ^ 2 ))
330
+ )
331
+
332
+ boundary.estimates <- NNS :: NNS.distance(
333
+ rpm = REGRESSION.POINT.MATRIX ,
334
+ dist.estimate = boundary.points ,
335
+ k = n.best ,
336
+ class = type
337
+ )
338
+
339
+ gradients <- sapply(1 : 3 , function (i ) {
340
+ compare.points <- list (central.points , mid.points , mid.points_2 )[[i ]]
341
+ (boundary.estimates - NNS :: NNS.distance(
342
+ rpm = REGRESSION.POINT.MATRIX ,
343
+ dist.estimate = compare.points ,
344
+ k = n.best ,
345
+ class = type
346
+ )) / last.known.distances [i ]
347
+ })
348
+
349
+ last.known.gradient <- sum(gradients * c(3 , 2 , 1 )) / 6
350
+ last.distance <- sqrt(sum((outside.points - boundary.points ) ^ 2 ))
351
+
352
+ last.distance * last.known.gradient + boundary.estimates
353
+ }
354
+ )
274
355
}
356
+
357
+
275
358
}
276
359
277
360
predict.fit <- DISTANCES
278
-
279
- if (point.only ) return (list (Point.est = predict.fit , RPM = REGRESSION.POINT.MATRIX [] ))
280
361
}
281
362
363
+ if (point.only ) {
364
+ return (list (Point.est = predict.fit , RPM = REGRESSION.POINT.MATRIX []))
365
+ }
282
366
} else {
283
367
predict.fit <- NULL
284
368
} # is.null point.est
285
369
370
+ if (num_cores > 1 ){
371
+ doParallel :: stopImplicitCluster()
372
+ foreach :: registerDoSEQ()
373
+ invisible (data.table :: setDTthreads(0 , throttle = NULL ))
374
+ invisible (gc(verbose = FALSE ))
375
+ }
376
+
286
377
if (! is.null(type )){
287
378
fitted.matrix $ y.hat <- ifelse(fitted.matrix $ y.hat %% 1 < 0.5 , floor(fitted.matrix $ y.hat ), ceiling(fitted.matrix $ y.hat ))
288
379
fitted.matrix $ y.hat <- pmin(max(original.DV ), pmax(min(original.DV ), fitted.matrix $ y.hat ))
0 commit comments