rgiordan / zaminfluence

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

Error in example code - ordinary regression #34

Closed maswiebe closed 2 years ago

maswiebe commented 2 years ago

When I run:

library(tidyverse)
library(gridExtra)
library(zaminfluence)
library(AER)

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(names_from=method, values_from=value)
    return(summary_df)
}

#############################
# Oridinary regression.

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

# 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.
# Derivatives are only computed for keep_pars, which can be in any order.
model_grads <-
    ComputeModelInfluence(fit_object, keep_pars=c("x2", "x1")) %>%
    AppendTargetRegressorInfluence("x1") %>%
    AppendTargetRegressorInfluence("x2")

I get the error: Error in ComputeModelInfluence(fit_object, keep_pars = c("x2", "x1")) : unused argument (keep_pars = c("x2", "x1"))

The code seems to work fine if I drop keep_pars and just run ComputeModelInfluence(fit_object), which is what the readme example does.

rgiordan commented 2 years ago

I just added the keep_pars argument last week. I see it in the master branch here: https://github.com/rgiordan/zaminfluence/blob/14fc32b43638b64b3603849450da3af73760170d/zaminfluence/R/ols_iv_grads_torch_lib.R#L447

So I think this could be caused by trying to run the new example script with an old version of zaminfluence. Are you sure you've got the latest version of zaminfluence installed?

maswiebe commented 2 years ago

Ok, just restarted R.

packageVersion("zaminfluence")

[1] ‘0.0.0.9000’

Now I can get through ComputeModelInfluence(fit_object, keep_pars=c("x2", "x1")), but it breaks on AppendTargetRegressorInfluence("x1"):

Error in cpp_torch_float64() : Lantern is not loaded. Please use install_torch() to install additional dependencies.

But this doesn't work: install_torch()

trying URL 'https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.10.2%2Bcpu.zip' Error in utils::download.file(library_url, temp_file) : cannot open URL 'https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.10.2%2Bcpu.zip' In addition: Warning message: In utils::download.file(library_url, temp_file) : URL 'https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.10.2%2Bcpu.zip': status was 'SSL peer certificate or SSH remote key was not OK'

Fyi sessionInfo()

R version 4.1.3 (2022-03-10) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.4 LTS

rgiordan commented 2 years ago

This sounds like a torch problem, though of course it may be somehow related to how I have set up dependencies.

Are you able to succesfully follow the installation instructions for the torch package?

https://torch.mlverse.org/docs/

maswiebe commented 2 years ago

Ok, install.packages("torch") did the trick. Works fine now!