Seaborn: Passing a list of confounding variables with x_partial in regplot

Created on 25 Feb 2015  路  12Comments  路  Source: mwaskom/seaborn

I'm plotting interaction effects with regplot. I want to take into account two confounding variables.
The documentation of regplot indicates the possibility of passing a list of string for x_partial.
{x, y}_partial : matrix or string(s) , optional
Matrix with same first dimension as x, or column name(s) in data.
These variables are treated as confounding and are removed from
the x or y variables before plotting.

However, I am a getting an error "ValueError: all the input array dimensions except for the concatenation axis must match exactly" in linearmodels.py/regplot

seaborn/linearmodels.pyc in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, xlabel, ylabel, label, color, marker, scatter_kws, line_kws, ax)
1185 order, logistic, lowess, robust, logx,
1186 x_partial, y_partial, truncate, dropna,
-> 1187 x_jitter, y_jitter, color, label)

Is the feature not implemented or am I missing something here?

thank you

annoyance regression

Most helpful comment

import seaborn as sns
iris = sns.load_dataset("iris")
sns.regplot("sepal_length", "sepal_width", data=iris,
            x_partial=iris[["petal_length", "petal_width"]])

This results in an AttributeError:

AttributeError                            Traceback (most recent call last)
<ipython-input-6-c142720672d1> in <module>
      1 sns.regplot("sepal_length", "sepal_width", data=iris,
----> 2             x_partial=iris[["petal_length", "petal_width"]])

~/miniconda3/envs/pymc37/lib/python3.7/site-packages/seaborn/regression.py in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, label, color, marker, scatter_kws, line_kws, ax)
    779                                  order, logistic, lowess, robust, logx,
    780                                  x_partial, y_partial, truncate, dropna,
--> 781                                  x_jitter, y_jitter, color, label)
    782 
    783     if ax is None:

~/miniconda3/envs/pymc37/lib/python3.7/site-packages/seaborn/regression.py in __init__(self, x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, color, label)
    111         # Regress nuisance variables out of the data
    112         if self.x_partial is not None:
--> 113             self.x = self.regress_out(self.x, self.x_partial)
    114         if self.y_partial is not None:
    115             self.y = self.regress_out(self.y, self.y_partial)

~/miniconda3/envs/pymc37/lib/python3.7/site-packages/seaborn/regression.py in regress_out(self, a, b)
    313         b = np.c_[b]
    314         a_prime = a - b.dot(np.linalg.pinv(b).dot(a))
--> 315         return (a_prime + a_mean).reshape(a.shape)
    316 
    317     def plot(self, ax, scatter_kws, line_kws):

~/miniconda3/envs/pymc37/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5065             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5066                 return self[name]
-> 5067             return object.__getattribute__(self, name)
   5068 
   5069     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'reshape'

What is the correct way to use this?

All 12 comments

Not implemented

Actually that's wrong, you can't pass a list of strings, but you can pass multiple columns, I think.

import seaborn as sns
iris = sns.load_dataset("iris")
sns.regplot("sepal_length", "sepal_width", data=iris,
            x_partial=iris[["petal_length", "petal_width"]])

Thanks you

Sorry the documentation is confusing -- I think the intention was to read as "strings" meaning for x_partial and y_partial, but it could be clearer.

Thank you for your prompt answer. It's perhaps a feature worth adding.

@hanisaf I am interested to know how you will decide to handle interaction plots.

It's perhaps a feature worth adding.

It's not so much a feature that doesn't exist as that the way the dataframe is reduce to avoid overzealous nan-removal isn't compatible with a list of strings. Probably a straightforward fix.

import seaborn as sns
iris = sns.load_dataset("iris")
sns.regplot("sepal_length", "sepal_width", data=iris,
            x_partial=iris[["petal_length", "petal_width"]])

This results in an AttributeError:

AttributeError                            Traceback (most recent call last)
<ipython-input-6-c142720672d1> in <module>
      1 sns.regplot("sepal_length", "sepal_width", data=iris,
----> 2             x_partial=iris[["petal_length", "petal_width"]])

~/miniconda3/envs/pymc37/lib/python3.7/site-packages/seaborn/regression.py in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, label, color, marker, scatter_kws, line_kws, ax)
    779                                  order, logistic, lowess, robust, logx,
    780                                  x_partial, y_partial, truncate, dropna,
--> 781                                  x_jitter, y_jitter, color, label)
    782 
    783     if ax is None:

~/miniconda3/envs/pymc37/lib/python3.7/site-packages/seaborn/regression.py in __init__(self, x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, color, label)
    111         # Regress nuisance variables out of the data
    112         if self.x_partial is not None:
--> 113             self.x = self.regress_out(self.x, self.x_partial)
    114         if self.y_partial is not None:
    115             self.y = self.regress_out(self.y, self.y_partial)

~/miniconda3/envs/pymc37/lib/python3.7/site-packages/seaborn/regression.py in regress_out(self, a, b)
    313         b = np.c_[b]
    314         a_prime = a - b.dot(np.linalg.pinv(b).dot(a))
--> 315         return (a_prime + a_mean).reshape(a.shape)
    316 
    317     def plot(self, ax, scatter_kws, line_kws):

~/miniconda3/envs/pymc37/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5065             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5066                 return self[name]
-> 5067             return object.__getattribute__(self, name)
   5068 
   5069     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'reshape'

What is the correct way to use this?

Having the same problem/error message. "AttributeError: 'Series' object has no attribute 'reshape"

Can someone help solve this? Would really like to use this param for work..

`x = 'dose'
y = 'telomere length'

sns.lmplot(x=x, y=y, data=data, height=8, aspect=1,
x_partial='Age (months)',
)`

`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in
3
4 sns.lmplot(x=x, y=y, data=merge_kelly_teloFISH_dose, height=8, aspect=1,
----> 5 x_partial='Age (months)',
6 )

/usr/local/lib/python3.7/site-packages/seaborn/regression.py in lmplot(x, y, data, hue, col, row, palette, col_wrap, height, aspect, markers, sharex, sharey, hue_order, col_order, row_order, legend, legend_out, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, x_jitter, y_jitter, scatter_kws, line_kws, size)
587 scatter_kws=scatter_kws, line_kws=line_kws,
588 )
--> 589 facets.map_dataframe(regplot, x, y, **regplot_kws)
590
591 # Add a legend

/usr/local/lib/python3.7/site-packages/seaborn/axisgrid.py in map_dataframe(self, func, args, *kwargs)
818
819 # Draw the plot
--> 820 self._facet_plot(func, ax, args, kwargs)
821
822 # Finalize the annotations and layout

/usr/local/lib/python3.7/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
836
837 # Draw the plot
--> 838 func(plot_args, *plot_kwargs)
839
840 # Sort out the supporting information

/usr/local/lib/python3.7/site-packages/seaborn/regression.py in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, label, color, marker, scatter_kws, line_kws, ax)
779 order, logistic, lowess, robust, logx,
780 x_partial, y_partial, truncate, dropna,
--> 781 x_jitter, y_jitter, color, label)
782
783 if ax is None:

/usr/local/lib/python3.7/site-packages/seaborn/regression.py in __init__(self, x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, color, label)
111 # Regress nuisance variables out of the data
112 if self.x_partial is not None:
--> 113 self.x = self.regress_out(self.x, self.x_partial)
114 if self.y_partial is not None:
115 self.y = self.regress_out(self.y, self.y_partial)

/usr/local/lib/python3.7/site-packages/seaborn/regression.py in regress_out(self, a, b)
313 b = np.c_[b]
314 a_prime = a - b.dot(np.linalg.pinv(b).dot(a))
--> 315 return (a_prime + a_mean).reshape(a.shape)
316
317 def plot(self, ax, scatter_kws, line_kws):

/usr/local/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
5178 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5179 return self[name]
-> 5180 return object.__getattribute__(self, name)
5181
5182 def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'reshape'`

sns.regplot(x='oefoefhct',y=outcome,data=prunedOEF, x_partial=prunedOEF[['age','cdrbinary']])

Using the described fix by mwaskom I get ValueError: regplot inputs must be 1d

I can only use x_partial with one input as
sns.regplot(x='oefoefhct',y=outcome,data=prunedOEF, x_partial=prunedOEF[['age']])
works just fine

Yes, in addressing https://github.com/mwaskom/seaborn/issues/1822 a check for 1d inputs to x/y was added which over-zealously applies to the confound arguments too.

I have the same issue when passing x_partial and y_partial parameters. The same AttributeError raises even the x_partial is with single input.

sns.regplot("sepal_length", "sepal_width", data=iris, x_partial=iris[["petal_length"]])

`Traceback (most recent call last):
File "C:\Users\Miao\Anaconda3\envs\psy37\lib\site-packages\IPythoncore\interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 3, in
x_partial=iris[["petal_length"]])

File "C:\Users\Miao\Anaconda3\envs\psy37\lib\site-packages\seaborn\regression.py", line 781, in regplot
x_jitter, y_jitter, color, label)

File "C:\Users\Miao\Anaconda3\envs\psy37\lib\site-packages\seaborn\regression.py", line 113, in __init__
self.x = self.regress_out(self.x, self.x_partial)

File "C:\Users\Miao\Anaconda3\envs\psy37\lib\site-packages\seaborn\regression.py", line 315, in regress_out
return (a_prime + a_mean).reshape(a.shape)

File "C:\Users\Miao\Anaconda3\envs\psy37\lib\site-packages\pandascore\generic.py", line 5057, in __getattr__
return object.__getattribute__(self, name)

AttributeError: 'Series' object has no attribute 'reshape'`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amelio-vazquez-reina picture amelio-vazquez-reina  路  4Comments

alexpetralia picture alexpetralia  路  3Comments

btyukodi picture btyukodi  路  3Comments

bondarevts picture bondarevts  路  3Comments

songololo picture songololo  路  4Comments