From 0d8c28a289d7bd46f57b27bc13bd5eef21951b5c Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 22 Jul 2024 17:35:43 +0200 Subject: [PATCH] add fixest_multi --- NAMESPACE | 1 + R/get_df.R | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index a52271336..d71a8bce4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -511,6 +511,7 @@ S3method(get_df,default) S3method(get_df,emmGrid) S3method(get_df,emm_list) S3method(get_df,fixest) +S3method(get_df,fixest_multi) S3method(get_df,lme) S3method(get_df,lmerMod) S3method(get_df,lmerModTest) diff --git a/R/get_df.R b/R/get_df.R index a03a29df8..e6e133ef5 100644 --- a/R/get_df.R +++ b/R/get_df.R @@ -278,6 +278,22 @@ get_df.fixest <- function(x, type = "residual", ...) { } +#' @export +get_df.fixest_multi <- function(x, ...) { + out <- do.call(rbind, lapply(x, get_df, ...)) + + # add response and group columns + id_columns <- .get_fixest_multi_columns(x) + + # add response column + out$Response <- id_columns$Response + out$Group <- id_columns$Group + + row.names(out) <- NULL + out +} + + # Mixed models - special treatment -------------- @@ -525,3 +541,33 @@ get_df.mediate <- function(x, ...) { TRUE } + + +.get_fixest_multi_columns <- function(model) { + # add response and group columns + s <- summary(model) + l <- lengths(lapply(s, stats::coef)) + parts <- strsplit(names(l), ";", fixed = TRUE) + + id_columns <- Map(function(i, j) { + if (length(j) == 1 && startsWith(j, "rhs")) { + data.frame( + Group = rep(trim_ws(sub("rhs:", "", j, fixed = TRUE)), i), + stringsAsFactors = FALSE + ) + } else if (length(j) == 1 && startsWith(j, "lhs")) { + data.frame( + Response = rep(trim_ws(sub("lhs:", "", j, fixed = TRUE)), i), + stringsAsFactors = FALSE + ) + } else { + data.frame( + Response = rep(trim_ws(sub("lhs:", "", j[1], fixed = TRUE)), i), + Group = rep(trim_ws(sub("rhs:", "", j[2], fixed = TRUE)), i), + stringsAsFactors = FALSE + ) + } + }, unname(l), parts) + + do.call(rbind, id_columns) +}