Skip to content

Commit

Permalink
add fixest_multi
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jul 22, 2024
1 parent 869a304 commit 0d8c28a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions R/get_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------

Expand Down Expand Up @@ -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)
}

0 comments on commit 0d8c28a

Please sign in to comment.