rgiordan / zaminfluence

Tools in R for computing and using Z-estimator approximate influence functions.
Apache License 2.0
94 stars 10 forks source link

Error when I try to run example code #31

Closed matthewbondy closed 2 years ago

matthewbondy commented 2 years ago

I tried running the example code, and got an error that appears to be linked to the SummarizeReruns function. Perhaps this is user error but I'm hoping someone can help me resolve the issue. Thanks!

Below is the code that I initially ran.


rm(list=ls())

library(tidyverse)
library(gridExtra)
library(zaminfluence)
library(AER)
library(dplyr)
library(tidyr)
library(zaminfluence)

compare <- function(x, y) { return(max(abs(x - y))) }
check_equivalent  <- function(x, y) { stopifnot(compare(x, y) < 1e-8) }

num_obs <- 10000

set.seed(42)

SummarizeReruns <- function(reruns, preds) {
  reruns_df <- GetSignalsAndRerunsDataframe(signals, reruns, model_grads)
  preds_df <- GetSignalsAndRerunsDataframe(signals, preds, model_grads)

  summary_df <-
    rbind(reruns_df %>% mutate(method="rerun"),
          preds_df %>% mutate(method="prediction")) %>%
    pivot_wider(-method, names_from=method, values_from=value) #added df
  return(summary_df)
}

# Generate data.
set.seed(42)
x_dim <- 3
param_true <- 0.1 * runif(x_dim)
num_groups <- 50
df <- GenerateRegressionData(num_obs, param_true, num_groups=num_groups)

# se_group is zero-indexed group indicator with no missing entries.
table(df$se_group)

# Fit a regression model.
x_names <- sprintf("x%d", 1:x_dim)
reg_form <- formula(sprintf("y ~ %s - 1", paste(x_names, collapse=" + ")))
fit_object <- lm(data=df, formula=reg_form, x=TRUE, y=TRUE)

# Get influence and reruns.  Pass the grouping indicator to the `se_group`` argument
# of `ComputeModelInfluence`.
model_grads <-
  ComputeModelInfluence(fit_object, se_group=df$se_group) %>%
  AppendTargetRegressorInfluence("x1")

# The grouped standard error which zaminfluence computes...
cat("Zaminfluence SE:\t", model_grads$param_infls[["x1"]]$se$base_value, "\n")

# ...is equivalent to the that computed by the following standard command:
cat("vcovCL se:\t\t", 
    vcovCL(fit_object, cluster=df$se_group, type="HC0", cadjust=FALSE)["x1", "x1"] %>% sqrt(), 
    "\n")

signals <- GetInferenceSignals(model_grads)
reruns <- RerunForSignals(signals, model_grads)
preds <- PredictForSignals(signals, model_grads)
summary_df <- SummarizeReruns(reruns, preds)

Here's the error I got:

Error in `stop_subscript()`:
! Can't subset columns that don't exist.
x Column `method` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.

When I run rlang::last_error(), it gives me:

<error/vctrs_error_subscript_oob>
Error in `stop_subscript()`:
! Can't subset columns that don't exist.
x Column `method` doesn't exist.
---
Backtrace:
  1. global SummarizeReruns(reruns, preds)
  4. tidyr:::pivot_wider.data.frame(...)
  5. tidyr:::build_wider_id_cols_expr(...)
  6. tidyr:::select_wider_id_cols(...)
  7. tidyselect::eval_select(enquo(id_cols), data)
  8. tidyselect:::eval_select_impl(...)
 17. tidyselect:::vars_select_eval(...)
 18. tidyselect:::walk_data_tree(expr, data_mask, context_mask, error_call)
 19. tidyselect:::eval_minus(expr, data_mask, context_mask)
 20. tidyselect:::eval_bang(expr, data_mask, context_mask)
 21. tidyselect:::walk_data_tree(expr[[2]], data_mask, context_mask)
 22. tidyselect:::as_indices_sel_impl(...)
 23. tidyselect:::as_indices_impl(x, vars, call = call, strict = strict)
 24. tidyselect:::chr_as_locations(x, vars, call = call)
 25. vctrs::vec_as_location(x, n = length(vars), names = vars)
 26. vctrs `<fn>`()
 27. vctrs:::stop_subscript_oob(...)
 28. vctrs:::stop_subscript(...)
Run `rlang::last_trace()` to see the full context.
rgiordan commented 2 years ago

I also got a similarly strange error this week, but it went away when I updated my packages (update_packages() in R).

I never tracked down the root cause, but before spending much time digging further, could you please update your packages and try again?

rgiordan commented 2 years ago

Just checking whether update_packages() worked for you here?

rgiordan commented 2 years ago

I'm going to close this for inactivity, but feel free to re-open it if you still have problems!