Skip to content

Commit d86a964

Browse files
committed
push before working from home, starting to calculate exp catch for random sampling
1 parent fba688d commit d86a964

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

R/calc_exp.R

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ calc_exp <- function(dataset,
4949
dataZoneTrue <- Alt[["dataZoneTrue"]] # used for catch and other variables
5050
choice <- Alt[["choice"]] # used for catch and other variables
5151

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+
5257
# user-defined ----
5358
# check whether defining a group or using all fleet averaging
5459
if (is_value_empty(defineGroup)) {
@@ -64,8 +69,14 @@ calc_exp <- function(dataset,
6469
fleet <- fleet[z_ind]
6570

6671
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+
}
6980

7081
altc_names <- if (is_value_empty(defineGroup)) altc_areas else altc_fleet
7182

@@ -403,4 +414,75 @@ calc_exp <- function(dataset,
403414
"empty.expectation" = empty.expectation, "temp.window" = temp.window, "temp.lag" = temp.lag,
404415
"year.lag" = year.lag, "dummy.exp" = dummy.exp, "weight_avg" = weight_avg))
405416
)
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("\nOptimized Moving Average Result (with missing values handled):")
488+
print(moving_avg_result)

R/create_expectations.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ create_expectations <-
158158

159159
}
160160

161-
162161
# checks ----
163162

164163
column_check(dataset, c(catch, price, temp.var, defineGroup))

0 commit comments

Comments
 (0)