如何对鸢尾花数据集变量组合批量应用decisionplot并存储ggplot图
Hey there! Let's tackle that . not found error you're hitting with map2 and your decisionplot function. I’ve run into similar context mix-ups before, so let’s break this down step by step.
First, Let’s Diagnose the Root Cause
The . not found error almost always happens when using map2’s formula shorthand (~) and trying to reference both input lists with the same . placeholder. R can’t tell which list you’re referring to, so it throws an error when it can’t resolve . correctly.
Let’s Fix the Code
First, let’s make sure we’re on the same page with your setup. I’ll assume your decisionplot function looks something like this (adjust if yours differs):
library(ggplot2) decisionplot <- function(model, data, xvar, yvar, ...) { # Create a grid of values for prediction x_grid <- seq(min(data[[xvar]]), max(data[[xvar]]), length.out = 100) y_grid <- seq(min(data[[yvar]]), max(data[[yvar]]), length.out = 100) prediction_grid <- expand.grid(x = x_grid, y = y_grid) colnames(prediction_grid) <- c(xvar, yvar) # Generate predictions prediction_grid$predicted <- predict(model, newdata = prediction_grid, type = "class") # Build the plot ggplot(data, aes(x = .data[[xvar]], y = .data[[yvar]], color = Species)) + geom_point(size = 2) + geom_tile(data = prediction_grid, aes(x = .data[[xvar]], y = .data[[yvar]], fill = predicted), alpha = 0.2, inherit.aes = FALSE) + labs(x = xvar, y = yvar) + theme_minimal() }
And your model/data list is built like this (handling Iris’s 3-class classification properly with nnet::multinom instead of binary glm):
library(tidyverse) library(nnet) # Get all non-repeating pairs of continuous variables cont_vars <- iris %>% select(-Species) %>% colnames() var_pairs <- combn(cont_vars, 2, simplify = FALSE) %>% set_names(map_chr(., ~paste(., collapse = " vs "))) # Build list of (model, data) pairs for each variable combination model_data_list <- map(var_pairs, function(pair) { data_subset <- iris %>% select(all_of(pair), Species) fitted_model <- multinom(Species ~ ., data = data_subset, trace = FALSE) list(data = data_subset, model = fitted_model) })
Fix 1: Use ..1 and ..2 to Distinguish Inputs
In map2’s formula syntax, ..1 refers to elements from the first list (model_data_list), and ..2 refers to elements from the second list (var_pairs):
plots_list <- map2(model_data_list, var_pairs, ~decisionplot(model = ..1$model, data = ..1$data, xvar = ..2[1], yvar = ..2[2]))
Fix 2: Use an Anonymous Function (More Readable)
If formula shorthand feels confusing, use a named anonymous function to explicitly define parameters—this eliminates ambiguity entirely:
plots_list <- map2(model_data_list, var_pairs, function(mdl_data, pair) { decisionplot(model = mdl_data$model, data = mdl_data$data, xvar = pair[1], yvar = pair[2]) })
Display the Plots in a Grid
Use the patchwork package to arrange all 6 plots neatly:
library(patchwork) wrap_plots(plots_list, ncol = 2) # Adjust ncol to change grid layout
Key Takeaway
The . placeholder only works for single-input maps (map()). For map2(), you need to either use positional placeholders (..1, ..2) or explicitly name your function parameters to avoid context confusion.
内容的提问来源于stack exchange,提问作者user113156

