@@ -49,6 +49,11 @@ calc_exp <- function(dataset,
49
49
dataZoneTrue <- Alt [[" dataZoneTrue" ]] # used for catch and other variables
50
50
choice <- Alt [[" choice" ]] # used for catch and other variables
51
51
52
+ # Random grid sampling arguments
53
+ grid_sample <- Alt [[" grid_sample" ]] # TRUE indicates randomly sampling zones
54
+ grid_sample_n <- Alt [[" grid_sample_n" ]] # Number of zones to sample
55
+ rand_alts_mat <- Alt [[" rand_alts_mat" ]] # Randomly sampled zones
56
+
52
57
# user-defined ----
53
58
# check whether defining a group or using all fleet averaging
54
59
if (is_value_empty(defineGroup )) {
@@ -64,8 +69,14 @@ calc_exp <- function(dataset,
64
69
fleet <- fleet [z_ind ]
65
70
66
71
areas <- choice [z_ind ] # mapping to to the map file (zones)
67
- altc_areas <- as.character(unique(areas ))
68
- altc_fleet <- unique(paste0(fleet , areas ))
72
+
73
+ if (! grid_sample ){ # aggregating by zones
74
+ altc_areas <- as.character(unique(areas ))
75
+ altc_fleet <- unique(paste0(fleet , areas ))
76
+ } else { # random sampling from grid
77
+ altc_areas <- as.character(unique(as.vector(rand_alts_mat )))
78
+ altc_fleet <- unique(paste0(fleet , areas ))
79
+ }
69
80
70
81
altc_names <- if (is_value_empty(defineGroup )) altc_areas else altc_fleet
71
82
@@ -403,4 +414,75 @@ calc_exp <- function(dataset,
403
414
" empty.expectation" = empty.expectation , " temp.window" = temp.window , " temp.lag" = temp.lag ,
404
415
" year.lag" = year.lag , " dummy.exp" = dummy.exp , " weight_avg" = weight_avg ))
405
416
)
406
- }
417
+ }
418
+
419
+
420
+
421
+ # #### TEST SOLUTION FROM AI #####
422
+ moving_average_2d_optimized <- function (mat , window_size ) {
423
+ # Check if window_size is valid
424
+ if (window_size < = 0 | window_size %% 1 != 0 ) {
425
+ stop(" Window size should be a positive integer." )
426
+ }
427
+
428
+ # Get the number of rows and columns
429
+ nrows <- nrow(mat )
430
+ ncols <- ncol(mat )
431
+
432
+ # Initialize the result matrix
433
+ result <- matrix (NA , nrow = nrows , ncol = ncols )
434
+
435
+ # Loop through each column (spatial zones)
436
+ for (j in 1 : ncols ) {
437
+ # Initialize a running sum and the count of non-NA values in the window
438
+ running_sum <- 0
439
+ running_count <- 0
440
+
441
+ # Loop through each row (dates)
442
+ for (i in 1 : nrows ) {
443
+ # Add the current value to the running sum if it's not NA
444
+ if (! is.na(mat [i , j ])) {
445
+ running_sum <- running_sum + mat [i , j ]
446
+ running_count <- running_count + 1
447
+ }
448
+
449
+ # If the window has exceeded the desired size, remove the value that is sliding out
450
+ if (i > window_size ) {
451
+ if (! is.na(mat [i - window_size , j ])) {
452
+ running_sum <- running_sum - mat [i - window_size , j ]
453
+ running_count <- running_count - 1
454
+ }
455
+ }
456
+
457
+ # Store the moving average in the result matrix (avoid division by zero)
458
+ if (running_count > 0 ) {
459
+ result [i , j ] <- running_sum / running_count
460
+ }
461
+ }
462
+ }
463
+
464
+ return (result )
465
+ }
466
+
467
+ # Example usage: matrix with dates as rows and spatial zones as columns
468
+ set.seed(42 )
469
+ dates <- seq.Date(from = as.Date(" 2024-01-01" ), by = " days" , length.out = 10 )
470
+ zones <- c(" Zone1" , " Zone2" , " Zone3" )
471
+ data_matrix <- matrix (rnorm(30 ), nrow = 10 , ncol = 3 , dimnames = list (dates , zones ))
472
+
473
+ # Introduce some missing data (NA values)
474
+ data_matrix [3 , 2 ] <- NA # Missing data in Zone2 on 2024-01-03
475
+ data_matrix [5 , 1 ] <- NA # Missing data in Zone1 on 2024-01-05
476
+ data_matrix [8 , 3 ] <- NA # Missing data in Zone3 on 2024-01-08
477
+
478
+ # Print the original matrix with missing data
479
+ print(" Original Data Matrix (with missing values):" )
480
+ print(data_matrix )
481
+
482
+ # Apply moving average with a window size of 3
483
+ window_size <- 3
484
+ moving_avg_result <- moving_average_2d_optimized(data_matrix , window_size )
485
+
486
+ # Print the moving average result
487
+ print(" \n Optimized Moving Average Result (with missing values handled):" )
488
+ print(moving_avg_result )
0 commit comments