``` r
# Define color assignments for predictor types
color_palette <- c(miRNA = "#E69F00", lncRNA = "#0072B2", CpG = "#009E73")

#  Define well-predicted features
all_features_highR2 <- all_MP_highperf %>%
  filter(Mean_R2 > 0.5) %>%
  pull(Feature) %>%
  unique()

# Function to extract importance from a single model
get_feature_importance_for_feature <- function(model) {
  coefs <- as.matrix(coef(model, s = "lambda.min"))[-1, , drop = FALSE]  # remove intercept
  data.frame(Predictor = rownames(coefs), Importance = abs(as.numeric(coefs)))
}

# Function to assign predictor type
get_predictor_type <- function(predictor_name) {
  if (startsWith(predictor_name, "Cluster")) return("miRNA")
  if (startsWith(predictor_name, "lncRNA")) return("lncRNA")
  if (startsWith(predictor_name, "CpG")) return("CpG")
  return("Other")
}

# Initialize list to save top predictors
top_predictors <- list()

# Loop over features, aggregate importance across replicates, and plot
for (target_feature in all_features_highR2) {
  
  # Extract and combine importance scores across all replicates
  importance_all_reps <- lapply(TM_list, function(rep_entry) {
    model <- rep_entry$Models[[target_feature]]
    if (!is.null(model)) {
      get_feature_importance_for_feature(model)
    } else {
      NULL
    }
  }) %>% bind_rows()

  # If no importance data was found, skip
  if (nrow(importance_all_reps) == 0) next

  # Compute average importance across replicates
  mean_importance <- importance_all_reps %>%
    group_by(Predictor) %>%
    summarise(MeanImportance = mean(Importance, na.rm = TRUE), .groups = "drop") %>%
    arrange(desc(MeanImportance)) %>%
    slice_head(n = 20) %>%
    mutate(Type = sapply(Predictor, get_predictor_type))


  # Plot once per target feature
  plot <- ggplot(mean_importance, aes(x = reorder(Predictor, MeanImportance), y = MeanImportance, fill=Type)) +
    geom_bar(stat = "identity") +
    scale_fill_manual(values = color_palette) +
    coord_flip() +
    theme_minimal() +
    labs(title = paste("Top Predictors for", target_feature),
         x = "Predictor",
         y = "Mean Importance (across replicates)")

  # Save top 5 most important predictors for later use
  top5 <- mean_importance %>% 
    slice_head(n = 5) %>%
    mutate(Feature = target_feature)
  
  top_predictors[[target_feature]] <- top5
  
  print(plot)
}
```

![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-1.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-2.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-3.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-4.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-5.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-6.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-7.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-8.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-9.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-10.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-11.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-12.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-13.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-14.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-15.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-16.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-17.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-18.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-19.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-20.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-21.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-22.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-23.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-24.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-25.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-26.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-27.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-28.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-29.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-30.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-31.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-32.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-33.png)<!-- -->![](22.3-Apul-multiomic-machine-learning-byTP_files/figure-gfm/unnamed-chunk-71-34.png)<!-- -->