Skip to content

Commit

Permalink
summary.estimate_slopes() no longer working.
Browse files Browse the repository at this point in the history
Fixes #345
  • Loading branch information
strengejacke committed Jan 29, 2025
1 parent be0f8d5 commit 483ffee
Showing 1 changed file with 9 additions and 54 deletions.
63 changes: 9 additions & 54 deletions R/summary.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
#' @export
summary.estimate_slopes <- function(object, ...) {
my_data <- as.data.frame(object)
trend <- attributes(object)$trend
out <- as.data.frame(object)

# Add "Confidence" col based on the sig index present in the data
my_data$Confidence <- .estimate_slopes_sig(my_data, ...)
out$Confidence <- .estimate_slopes_significance(out, ...)
out$Direction <- .estimate_slopes_direction(out, ...)

# Grouping variables
vars <- attributes(object)$at
vars <- vars[!vars %in% trend]

# If no grouping variables, summarize all
if (length(vars) == 0) {
out <- .estimate_slopes_summarize(my_data, trend = trend)
} else {
out <- data.frame()
# Create vizmatrix of grouping variables
groups <- as.data.frame(insight::get_datagrid(my_data[vars], factors = "all", numerics = "all"))
# Summarize all of the chunks
for (i in seq_len(nrow(groups))) {
g <- datawizard::data_match(my_data, groups[i, , drop = FALSE])
out <- rbind(out, .estimate_slopes_summarize(g, trend = trend))
}
out <- datawizard::data_relocate(out, vars)
}

# Clean and sanitize
out$Confidence <- NULL # Drop significance col
attributes(out) <- utils::modifyList(attributes(object), attributes(out))
class(out) <- c("estimate_slopes", class(out))
class(out) <- c("estimate_slopes", "data.frame")
attr(out, "table_title") <- c("Average Marginal Effects", "blue")

out
}

Expand All @@ -45,43 +25,18 @@ summary.reshape_grouplevel <- function(object, ...) {
# Utilities ===============================================================


.estimate_slopes_summarize <- function(data, trend, ...) {
# Find beginnings and ends -----------------------
# First row - starting point
.estimate_slopes_direction <- function(data, ...) {
centrality_columns <- datawizard::extract_column_names(
data,
c("Coefficient", "Slope", "Median", "Mean", "MAP_Estimate"),
verbose = FALSE
)
centrality_signs <- sign(data[[centrality_columns]])
centrality_sign <- centrality_signs[1]
sig <- data$Confidence[1]
starts <- 1
ends <- nrow(data)
# Iterate through all rows to find blocks
for (i in 2:nrow(data)) {
if ((data$Confidence[i] != sig) || ((centrality_signs[i] != centrality_sign) && data$Confidence[i] == "Uncertain")) {
centrality_sign <- centrality_signs[i]
sig <- data$Confidence[i]
starts <- c(starts, i)
ends <- c(ends, i - 1)
}
}
ends <- sort(ends)

# Summarize these groups -----------------------
out <- data.frame()
for (g in seq_len(length(starts))) {
dat <- data[starts[g]:ends[g], ]
dat <- as.data.frame(insight::get_datagrid(dat, by = NULL, factors = "mode"))
dat <- cbind(data.frame(Start = data[starts[g], trend], End = data[ends[g], trend]), dat)
out <- rbind(out, dat)
}
out
centrality_signs <- as.character(sign(data[[centrality_columns]]))
datawizard::recode_into(centrality_signs == -1 ~ "negative", default = "positive")
}


.estimate_slopes_sig <- function(x, confidence = "auto", ...) {
.estimate_slopes_significance <- function(x, confidence = "auto", ...) {
insight::check_if_installed("effectsize")

if (confidence == "auto") {
Expand Down

0 comments on commit 483ffee

Please sign in to comment.