多组参数调用R函数并合并结果的技术实现问询
Let's work through this step by step—your original setup works great for single parameters, but we need to adjust it to cleanly handle all your parameter combinations without global variable conflicts or broken loops.
First, Fix the Custom Function
The biggest issue with your original foo1 is that it relies on global variables (adstock_rate, diminishing_rate), which will cause chaos when looping through different combinations. Let's rewrite it to accept all necessary parameters directly, plus handle the lag_number = 0 case properly:
foo1 <- function(dot, adstock_rate, diminishing_rate, lag_val = 1) { tmp <- dot # Handle lag_val = 0 (no prior period influence) if (lag_val == 0) { # Option 1: Return the original vector (no adstock applied for lag 0) return(tmp) # Option 2: Apply a multiplicative factor to the current period (uncomment if needed) # tmp <- tmp * (1 + adstock_rate * diminishing_rate) # return(tmp) } # For lag values >=1, run the adstock calculation for(i in (1 + lag_val):length(tmp)) { tmp[i] <- tmp[i] + adstock_rate * diminishing_rate * tmp[i - lag_val] } return(tmp) }
Fix the Parameter Combos Data Frame
Your original code had a syntax error when setting column names—let's correct that to ensure we can reference parameters clearly:
adstock_rate = c(0.50, 0.60, 0.70) lag_number = c(0,1) diminishing_rate = c(0.50, 0.60) combos <- expand.grid(adstock_rate, lag_number, diminishing_rate) colnames(combos) <- c("AdStock_Rate", "Lag_Number", "Diminish_Rate")
Loop Through All Combos & Add New Columns
Now we'll loop through each parameter combination, generate a descriptive column name (like adstock_0.5_1_0.6), and apply the function to your grouped data. We'll use dplyr's modern across function instead of mutate_all for better clarity (it's the recommended approach now):
# Start with your original dataset result <- advertising_dataset # Iterate over each parameter combination for (i in 1:nrow(combos)) { # Grab current parameter values current_params <- combos[i, ] rate <- current_params$AdStock_Rate lag <- current_params$Lag_Number diminish <- current_params$Diminish_Rate # Create a unique column name col_name <- paste0("adstock_", rate, "_", lag, "_", diminish) # Apply the function to all columns (replace everything() with specific column names if needed) result <- result %>% group_by(Region) %>% mutate(across(everything(), ~foo1(., adstock_rate = rate, diminishing_rate = diminish, lag_val = lag), .names = "{col_name}")) # Add {.col}_ before {col_name} to keep original column prefixes ungroup() }
Key Notes
- No Global Variables: By passing parameters directly to
foo1, we avoid conflicts between iterations. - Lag 0 Handling: We added a check for
lag_val = 0—pick the option that matches your business logic (either return the original value or apply a current-period factor). - Flexibility: If you only want to apply this to specific columns (not all), replace
everything()withc("TV", "Radio", "Newspaper")(or whatever your ad columns are named).
内容的提问来源于stack exchange,提问作者Nick Knauer

