pymc-labs/CausalPy

fix error in plot method

drbenvincent opened this issue · 4 comments

When calling result.plot() we are getting an error:

ValueError: 
CI_{94%}
  • This seems to affect the PyMC difference in difference models. For example, see the documentation notebooks did_pymc_banks.ipynb, did_pymc.ipynb.
  • We'd also want to check that any solution doesn't break the doctests.
  • Ideally we'd add some sort of test(s) that result.plot() executes ok.

I think there is a general issue withe the plot function for difference in differences class

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[169], line 1
----> 1 result.plot()

File /opt/conda/lib/python3.10/site-packages/causalpy/pymc_experiments.py:567, in DifferenceInDifferences.plot(self, round_to)
    564 fig, ax = plt.subplots()
    566 # Plot raw data
--> 567 sns.scatterplot(
    568     self.data,
    569     x=self.time_variable_name,
    570     y=self.outcome_variable_name,
    571     hue=self.group_variable_name,
    572     alpha=1,
    573     legend=False,
    574     markers=True,
    575     ax=ax,
    576 )
    578 # Plot model fit to control group
    579 time_points = self.x_pred_control[self.time_variable_name].values

File /opt/conda/lib/python3.10/site-packages/seaborn/_decorators.py:46, in _deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
     36     warnings.warn(
     37         "Pass the following variable{} as {}keyword arg{}: {}. "
     38         "From version 0.12, the only valid positional argument "
   (...)
     43         FutureWarning
     44     )
     45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46 return f(**kwargs)

File /opt/conda/lib/python3.10/site-packages/seaborn/relational.py:808, in scatterplot(x, y, hue, style, size, data, palette, hue_order, hue_norm, sizes, size_order, size_norm, markers, style_order, x_bins, y_bins, units, estimator, ci, n_boot, alpha, x_jitter, y_jitter, legend, ax, **kwargs)
    793 @_deprecate_positional_args
    794 def scatterplot(
    795     *,
   (...)
    804     legend="auto", ax=None, **kwargs
    805 ):
    807     variables = _ScatterPlotter.get_semantics(locals())
--> 808     p = _ScatterPlotter(
    809         data=data, variables=variables,
    810         x_bins=x_bins, y_bins=y_bins,
    811         estimator=estimator, ci=ci, n_boot=n_boot,
    812         alpha=alpha, x_jitter=x_jitter, y_jitter=y_jitter, legend=legend,
    813     )
    815     p.map_hue(palette=palette, order=hue_order, norm=hue_norm)
    816     p.map_size(sizes=sizes, order=size_order, norm=size_norm)

File /opt/conda/lib/python3.10/site-packages/seaborn/relational.py:587, in _ScatterPlotter.__init__(self, data, variables, x_bins, y_bins, estimator, ci, n_boot, alpha, x_jitter, y_jitter, legend)
    571 def __init__(
    572     self, *,
    573     data=None, variables={},
   (...)
    581     # the kind of plot to draw, but for the time being we need to set
    582     # this information so the SizeMapping can use it
    583     self._default_size_range = (
    584         np.r_[.5, 2] * np.square(mpl.rcParams["lines.markersize"])
    585     )
--> 587     super().__init__(data=data, variables=variables)
    589     self.alpha = alpha
    590     self.legend = legend

File /opt/conda/lib/python3.10/site-packages/seaborn/_core.py:605, in VectorPlotter.__init__(self, data, variables)
    603 def __init__(self, data=None, variables={}):
--> 605     self.assign_variables(data, variables)
    607     for var, cls in self._semantic_mappings.items():
    608 
    609         # Create the mapping function
    610         map_func = partial(cls.map, plotter=self)

File /opt/conda/lib/python3.10/site-packages/seaborn/_core.py:668, in VectorPlotter.assign_variables(self, data, variables)
    666 else:
    667     self.input_format = "long"
--> 668     plot_data, variables = self._assign_variables_longform(
    669         data, **variables,
    670     )
    672 self.plot_data = plot_data
    673 self.variables = variables

File /opt/conda/lib/python3.10/site-packages/seaborn/_core.py:903, in VectorPlotter._assign_variables_longform(self, data, **kwargs)
    898 elif isinstance(val, (str, bytes)):
    899 
    900     # This looks like a column name but we don't know what it means!
    902     err = f"Could not interpret value `{val}` for parameter `{key}`"
--> 903     raise ValueError(err)
    905 else:
    906 
    907     # Otherwise, assume the value is itself data
    908 
    909     # Raise when data object is present and a vector can't matched
    910     if isinstance(data, pd.DataFrame) and not isinstance(val, pd.Series):

ValueError: Could not interpret value `y` for parameter `y`

Hi @juliangardin. Would you be able to create a new issue with a minimum working example - i.e. what steps you took which resulted in the error? And clarify which version of CausalPy you are running.

@drbenvincent I was running the did-pymc example here: https://causalpy.readthedocs.io/en/stable/notebooks/did_pymc.html

Version = 0.2.2

So I re-ran the notebook using the development version of CausalPy and no errors.

To see if it's a bug that was fixed between 0.2.2 and the development version I created a new environment and ran the commands from that notebook in ipython.

set up env

conda create -c conda-forge -n causalpydebug "pymc>=5" "causalpy==0.2.2"
conda activate causalpydebug
ipython

Then in that ipython session I ran

import arviz as az
import causalpy as cp
import matplotlib.pyplot as plt
seed = 42

df = cp.load_data("did")

result = cp.pymc_experiments.DifferenceInDifferences(
    df,
    formula="y ~ 1 + group*post_treatment",
    time_variable_name="t",
    group_variable_name="group",
    model=cp.pymc_models.LinearRegression(sample_kwargs={"random_seed": seed}),
)

fig, ax = result.plot()
plt.show()

and the image appeared fine.

There is a bug with result.summary(), but that is fixed in the development version and will filter through into 0.2.3 when we release it soonish.

Sorry, but I can't reproduce the bug so far. Feel free to create an issue with all the steps to see if we can reproduce the issue.