
    >hٲ                        S r SSKJr  SSKJrJr  SSKrSSKr	SSK
Jr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJr  SSKJr  SSKJr  SSKJrJrJrJ r J!r!  / SQr"S r#S,S jr$S-S jr%S.S jr&S r'S0 S0 SSS4S jr(S/S jr)S.S jr*S/S jr+  S0S jr,\" \RZ                  " S10 SS0D65         S2S j5       r.\" \RZ                  " S10 SS0D65        S3S  j5       r/\" \ RZ                  " SS!05      5      S4S" j5       r0\" \ RZ                  " SS#05      5      S4S$ j5       r1\" \SS#0-  5        S5S% j5       r2\" \!SS#0-  5      S.S& j5       r3\" \SS'0-  5        S6S( j5       r4S7S) jr5S* r6  S8S+ jr7g)9zPartial Regression plot and residual plots to find misspecification


Author: Josef Perktold
License: BSD-3
Created: 2011-01-23

update
2011-06-05 : start to convert example to usable functions
2011-10-27 : docstrings

    )Appender)lrangelzipN)dmatrix)GEE)GLM)utils)lowess)GLSOLSWLS)wls_prediction_std)maybe_unwrap_results   )_plot_added_variable_doc_plot_ceres_residuals_doc_plot_influence_doc_plot_leverage_resid2_doc_plot_partial_residuals_doc)plot_fitplot_regress_exogplot_partregress	plot_ccprr   plot_partregress_gridplot_ccpr_grid
add_lowessabline_plotinfluence_plotplot_leverage_resid2added_variable_residspartial_residsceres_residsplot_added_variableplot_partial_residualsplot_ceres_residualsc                 @    SU R                   S-   -  U R                  -  $ )N       @r   )df_modelnobs)resultss    wC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/graphics/regressionplots.py_high_leverager,   ,   s!    !!A%&w||33    c                     U R                  5       U   R                  nU R                  5       U   R                  n[        XE4SU0UD6nU R	                  USS2S4   USS2S4   SSS9  U R
                  $ )a  
Add Lowess line to a plot.

Parameters
----------
ax : AxesSubplot
    The Axes to which to add the plot
lines_idx : int
    This is the line on the existing plot to which you want to add
    a smoothed lowess line.
frac : float
    The fraction of the points to use when doing the lowess fit.
lowess_kwargs
    Additional keyword arguments are passes to lowess.

Returns
-------
Figure
    The figure that holds the instance.
fracNr   r   rg      ?)lw)	get_lines_y_xr
   plotfigure)ax	lines_idxr/   lowess_kwargsy0x0lress          r+   r   r   1   sr    * 
		"	%	%B			"	%	%B"5t5}5DGGDAJQT
CCG099r-   Tc           	          [         R                  " U5      u  pc[         R                  " XR                  5      u  pq[	        U 5      n U R                  R
                  nU R                  R                  SS2U4   n	[        R                  " U	5      n
X   nX   n	UR                  XSU R                  R                  S9  Ub  UR                  XU
   SSS9  SU-  nUR                  " XR                  U
   S4SS	S
.UD6  USL a&  [        U 5      u  pnUR                  XU
   X   SSSS9  UR                  U5        UR                  U5        UR!                  U R                  R                  5        UR#                  SSS9  U$ )a  
Plot fit against one regressor.

This creates one graph with the scatterplot of observed values
compared to fitted values.

Parameters
----------
results : Results
    A result instance with resid, model.endog and model.exog as
    attributes.
exog_idx : {int, str}
    Name or index of regressor in exog matrix.
y_true : array_like. optional
    If this is not None, then the array is added to the plot.
ax : AxesSubplot, optional
    If given, this subplot is used to plot in instead of a new figure being
    created.
vlines : bool, optional
    If this not True, then the uncertainty (pointwise prediction intervals) of the fit is not
    plotted.
**kwargs
    The keyword arguments are passed to the plot command for the fitted
    values points.

Returns
-------
Figure
    If `ax` is None, the created figure.  Otherwise the figure to which
    `ax` is connected.

Examples
--------
Load the Statewide Crime data set and perform linear regression with
`poverty` and `hs_grad` as variables and `murder` as the response

>>> import statsmodels.api as sm
>>> import matplotlib.pyplot as plt

>>> data = sm.datasets.statecrime.load_pandas().data
>>> murder = data['murder']
>>> X = data[['poverty', 'hs_grad']]

>>> X["constant"] = 1
>>> y = murder
>>> model = sm.OLS(y, X)
>>> results = model.fit()

Create a plot just for the variable 'Poverty.'
Note that vertical bars representing uncertainty are plotted since vlines is true

>>> fig, ax = plt.subplots()
>>> fig = sm.graphics.plot_fit(results, 0, ax=ax)
>>> ax.set_ylabel("Murder Rate")
>>> ax.set_xlabel("Poverty Level")
>>> ax.set_title("Linear Regression")

>>> plt.show()

.. plot:: plots/graphics_plot_fit_ex.py
Nbo)labelzb-zTrue valueszFitted values versus %sDr0   fitted)colorr?   Tr   kffffff?	linewidthrB   alphabest)loc	numpoints)r	   create_mpl_axmaybe_name_or_idxmodelr   endogexognpargsortr5   endog_namesfittedvaluesr   vlines	set_title
set_xlabel
set_ylabellegend)r*   exog_idxy_truer7   rT   kwargsfig	exog_nameyx1
x1_argsorttitle_iv_liv_us                  r+   r   r   M   sj   ~ !!"%GC11(MMJI"7+G 	A			AxK	(BBJ	A	BGGB4w}}88G9
:&MB%	1EGGB$$Z0# &S&$&~*73
		":&(8A2 	 	' LLMM)MM'--++,II&AI&Jr-   c           	         [         R                  " U5      n[         R                  " XR                  5      u  p1[	        U 5      n U R                  R
                  nU R                  R                  SS2U4   n[        U 5      u  pgnUR                  SSS5      n	U	R                  XPR                  R                  SSSUS9  U	R                  XPR                  SS	S
SS9  U	R                  XWUSSSS9  U	R                  SSS9  U	R                  U5        U	R                  U5        U	R!                  SS9  UR                  SSS5      n	U	R                  XPR"                  S5        U	R%                  SSS9  U	R                  SU-  SS9  U	R                  U5        U	R                  S5        UR                  SSS5      n	[&        R(                  " U R                  R                  R*                  S   [,        5      n
SX'   U R                  R                  SS2U
4   nSSKJn  [3        U R                  R4                  R6                  U" XSU R                  R4                  R8                  S9USU	S9nU	R                  SSS9  UR                  SSS 5      n	[;        XU	S!9nU	R                  S"SS9  UR=                  S#U-  SS9  UR?                  5         URA                  SS$9  U$ )%a  Plot regression results against one regressor.

This plots four graphs in a 2 by 2 figure: 'endog versus exog',
'residuals versus exog', 'fitted versus exog' and
'fitted plus residual versus exog'

Parameters
----------
results : result instance
    A result instance with resid, model.endog and model.exog as attributes.
exog_idx : int or str
    Name or index of regressor in exog matrix.
fig : Figure, optional
    If given, this figure is simply returned.  Otherwise a new figure is
    created.

Returns
-------
Figure
    The value of `fig` if provided. Otherwise a new instance.

Examples
--------
Load the Statewide Crime data set and build a model with regressors
including the rate of high school graduation (hs_grad), population in urban
areas (urban), households below poverty line (poverty), and single person
households (single).  Outcome variable is the murder rate (murder).

Build a 2 by 2 figure based on poverty showing fitted versus actual murder
rate, residuals versus the poverty rate, partial regression plot of poverty,
and CCPR plot for poverty rate.

>>> import statsmodels.api as sm
>>> import matplotlib.pyplot as plt
>>> import statsmodels.formula.api as smf

>>> fig = plt.figure(figsize=(8, 6))
>>> crime_data = sm.datasets.statecrime.load_pandas()
>>> results = smf.ols('murder ~ hs_grad + urban + poverty + single',
...                   data=crime_data.data).fit()
>>> sm.graphics.plot_regress_exog(results, 'poverty', fig=fig)
>>> plt.show()

.. plot:: plots/graphics_regression_regress_exog.py
N   r   obg?)rB   rG   r?   r@   r0   rA         ?)rB   r?   rG   rC   rD   rE   zY and Fitted vs. XlargefontsizerH   )rI   r   black)r^   rB   zResiduals versus %sresid   F)Series)nameindex)
obs_labelsr7   zPartial regression plot   r7   z	CCPR PlotzRegression Plots for %stop)!r	   create_mpl_figrL   rM   r   rR   rO   r   add_subplotr5   rN   rS   rT   rU   rV   rW   rX   rn   axhlinerP   onesshapeboolpandasrp   r   data
orig_endog
row_labelsr   suptitletight_layoutsubplots_adjust)r*   rY   r\   r]   y_namer_   prstdrc   rd   r7   	exog_notiexog_othersrp   s                r+   r   r      s   ^ 

s
#C11(MMJI"7+G ]]&&F			AxK	(B*73E	Aq	!BGGB##S3fGMGGB$$cH  IIbBI?LL%L8MM)MM&II&I	Aq	!BGGBs#JJ'J"LL&2WLEMM)MM'	Aq	!B**003T:II--$$Q	\2K
7==--88!"(/(:(:(E(EG&5RAC LL*WL= 
Aq	!B
G"
-CLLwL/ LL*Y6LIC Jr-   c                     [        X5      R                  5       n[        X5      R                  5       n[        UR                  UR                  5      R                  5       nXSU44$ )a]  Partial regression.

regress endog on exog_i conditional on exog_others

uses OLS

Parameters
----------
endog : array_like
exog : array_like
exog_others : array_like

Returns
-------
res1c : OLS results instance

(res1a, res1b) : tuple of OLS results instances
     results from regression of endog on exog_others and of exog_i on
     exog_others
)r   fitrn   )rN   exog_ir   res1ares1bres1cs         r+   _partial_regressionr     sT    , #'')E$((*EU[[)--/E%.  r-   Fc
           	      4   [         R                  " U5      u  p[        U [        5      (       a  [	        U S-   X9S9n [        U[        5      (       a  [	        X#U	S9nO3[        U[
        5      (       a  SR                  U5      n[	        XU	S9nOUnSn[        U[        R                  5      (       a  UR                  S:X  a  SnO2[        U[        R                  5      (       a  UR                  (       a  Sn[        U[        5      (       a  [	        US-   X9S9nU(       a  [        R                  " U 5      n [        R                  " U5      nUR                  " XS40 U
D6  [        X5      R!                  5       n[        U[        R                  5      (       a  SOUR"                  n[        U [        R                  5      (       a  S	OU R$                  R&                  S   nO[        X5      R!                  5       n[        X5      R!                  5       nUR(                  nUR(                  nUR*                  R,                  nUR*                  R,                  nUR                  " UUS40 U
D6  [        UU5      R!                  5       n[/        S[        R                  " UR0                  5      S   S
US9nUS	:X  a  SnUR3                  SU-  5        UR5                  SU-  5        UR6                  " S0 UD6  USL ae  Ub  UR8                  nO>[;        US5      (       a  UR8                  nO WR*                  R<                  R>                  nUc  [A        [C        U5      5      nUSLa  [C        U5      [C        U5      :w  a  [E        S5      eURG                  [I        SSS95        [         RJ                  " [A        [C        U5      5      U[M        WR(                  WR(                  5      S/[C        U5      -  S4SU0UD6nU(       a  UWR(                  WR(                  44$ U$ )a2  Plot partial regression for a single regressor.

Parameters
----------
endog : {ndarray, str}
   The endogenous or response variable. If string is given, you can use a
   arbitrary translations as with a formula.
exog_i : {ndarray, str}
    The exogenous, explanatory variable. If string is given, you can use a
    arbitrary translations as with a formula.
exog_others : {ndarray, list[str]}
    Any other exogenous, explanatory variables. If a list of strings is
    given, each item is a term in formula. You can use a arbitrary
    translations as with a formula. The effect of these variables will be
    removed by OLS regression.
data : {DataFrame, dict}
    Some kind of data structure with names if the other variables are
    given as strings.
title_kwargs : dict
    Keyword arguments to pass on for the title. The key to control the
    fonts is fontdict.
obs_labels : {bool, array_like}
    Whether or not to annotate the plot points with their observation
    labels. If obs_labels is a boolean, the point labels will try to do
    the right thing. First it will try to use the index of data, then
    fall back to the index of exog_i. Alternatively, you may give an
    array-like object corresponding to the observation numbers.
label_kwargs : dict
    Keyword arguments that control annotate for the observation labels.
ax : AxesSubplot, optional
    If given, this subplot is used to plot in instead of a new figure being
    created.
ret_coords : bool
    If True will return the coordinates of the points in the plot. You
    can use this to add your own annotations.
eval_env : int
    Patsy eval environment if user functions and formulas are used in
    defining endog or exog.
**kwargs
    The keyword arguments passed to plot for the points.

Returns
-------
fig : Figure
    If `ax` is None, the created figure.  Otherwise the figure to which
    `ax` is connected.
coords : list, optional
    If ret_coords is True, return a tuple of arrays (x_coords, y_coords).

See Also
--------
plot_partregress_grid : Plot partial regression for a set of regressors.

Notes
-----
The slope of the fitted line is the that of `exog_i` in the full
multiple regression. The individual points can be used to assess the
influence of points on the estimated coefficient.

Examples
--------
Load the Statewide Crime data set and plot partial regression of the rate
of high school graduation (hs_grad) on the murder rate(murder).

The effects of the percent of the population living in urban areas (urban),
below the poverty line (poverty) , and in a single person household (single)
are removed by OLS regression.

>>> import statsmodels.api as sm
>>> import matplotlib.pyplot as plt

>>> crime_data = sm.datasets.statecrime.load_pandas()
>>> sm.graphics.plot_partregress(endog='murder', exog_i='hs_grad',
...                              exog_others=['urban', 'poverty', 'single'],
...                              data=crime_data.data, obs_labels=False)
>>> plt.show()

.. plot:: plots/graphics_regression_partregress.py

More detailed examples can be found in the Regression Plots notebook
on the examples page.
z-1)eval_env+Fr   Trg   xr^   rC   )rB   r7   z	e(%s | X)rr   z*obs_labels does not match length of exog_icenterbottom)havar      x-larger7   )Partial Regression Plot)'r	   rK   
isinstancestrr   listjoinrP   ndarraysizepd	DataFrameemptyasarrayr5   r   r   rq   design_infocolumn_namesrn   rM   rR   r   paramsrV   rW   rU   rr   hasattrr   r   r   len
ValueErrorupdatedictannotate_axesr   )rN   r   r   r   title_kwargsrs   label_kwargsr7   
ret_coordsr   r[   r\   RHSRHS_isemtpyfitted_linex_axis_endog_namey_axis_endog_name	res_yaxis	res_xaxisxaxis_residyaxis_resids                        r+   r   r   /  sR   n !!"%GC %d>+s##k(;	K	&	&hh{#c(3K#rzz""sxx{	C	&	&399&#$@ 

5!F#
s-f-%(,,.#-fbjj#A#ACv{{#-eRZZ#@#@CeFWFWFdFdefFgO'')	$((*	oooo%OO77%OO77
[#88+{3779
aK$6$67:#"
MCCMM+ 112MM+ 112LL;l; TJVW%%J"--88J F,Jz?c&k)IJJDH:;  J!8*!%iooy!G"(C
O!;Y1KM1 $01
 Y__ioo666
r-   c           
      ~   SSK n[        R                  " U5      n[        R                  " XR                  5      u  pQUR                  U R                  R                  U R                  R                  S9nU R                  R                  nUR                  S   n[        U5      S-   S-  n	U	[        U5      :X  a  SOSn
Ub  Uu  pU
S:  a  SSS00n[        R                  " U R                  R                  5      n[        U5       H  u  p[        U5      nUR!                  U5        UR#                  USS2U4   X   S	9nUR%                  XUS-   5      n['        XdR                  USS2U4   X   S9UUWS
S9  UR)                  S5        M     UR+                  SSS9  UR-                  5         UR/                  SS9  U$ )a  
Plot partial regression for a set of regressors.

Parameters
----------
results : Results instance
    A regression model results instance.
exog_idx : {None, list[int], list[str]}
    The indices  or column names of the exog used in the plot, default is
    all.
grid : {None, tuple[int]}
    If grid is given, then it is used for the arrangement of the subplots.
    The format of grid is  (nrows, ncols). If grid is None, then ncol is
    one, if there are only 2 subplots, and the number of columns is two
    otherwise.
fig : Figure, optional
    If given, this figure is simply returned.  Otherwise a new figure is
    created.

Returns
-------
Figure
    If `fig` is None, the created figure.  Otherwise `fig` itself.

See Also
--------
plot_partregress : Plot partial regression for a single regressor.
plot_ccpr : Plot CCPR against one regressor

Notes
-----
A subplot is created for each explanatory variable given by exog_idx.
The partial regression plot shows the relationship between the response
and the given explanatory variable after removing the effect of all other
explanatory variables in exog.

References
----------
See http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/partregr.htm

Examples
--------
Using the state crime dataset separately plot the effect of the each
variable on the on the outcome, murder rate while accounting for the effect
of all other variables in the model visualized with a grid of partial
regression plots.

>>> from statsmodels.graphics.regressionplots import plot_partregress_grid
>>> import statsmodels.api as sm
>>> import matplotlib.pyplot as plt
>>> import statsmodels.formula.api as smf

>>> fig = plt.figure(figsize=(8, 6))
>>> crime_data = sm.datasets.statecrime.load_pandas()
>>> results = smf.ols('murder ~ hs_grad + urban + poverty + single',
...                   data=crime_data.data).fit()
>>> plot_partregress_grid(results, fig=fig)
>>> plt.show()

.. plot:: plots/graphics_regression_partregress_grid.py
r   N)rq   r   rf   fontdictrl   small)columnsF)r7   r   rs    r   rj   rk   ffffff?rv   )r~   r	   rx   rL   rM   rp   rN   rR   rO   r|   r   rP   array
exog_names	enumerater   popr   ry   r   rU   r   r   r   )r*   rY   gridr\   r~   r]   r^   rO   k_varsnrowsncolsr   other_namesiidxothersr   r7   s                     r+   r   r     s   | 


s
#C11(MMJI 	gmm))0I0IJA==DZZ]F ]Q1$E#h-'AQEqy"Z$9: ((7==334KH%

3&&tAvI/:/B ' D__U1q51MM$q#v,/:/? * A$,$)	+ 	R & LL*WL=C Jr-   c                 $   [         R                  " U5      u  p2[         R                  " XR                  5      u  pA[	        U 5      n U R                  R
                  SS2U4   nXPR                  U   -  nUR                  XVU R                  -   S5        SSK	J
n  [        Xg" U5      5      R                  5       nUR                  n	[        U	0 [        US9D6nUR                  S5        UR!                  SXA4-  5        UR#                  SU-  5        U$ )	a  
Plot CCPR against one regressor.

Generates a component and component-plus-residual (CCPR) plot.

Parameters
----------
results : result instance
    A regression results instance.
exog_idx : {int, str}
    Exogenous, explanatory variable. If string is given, it should
    be the variable name that you want to use, and you can use arbitrary
    translations as with a formula.
ax : AxesSubplot, optional
    If given, it is used to plot in instead of a new figure being
    created.

Returns
-------
Figure
    If `ax` is None, the created figure.  Otherwise the figure to which
    `ax` is connected.

See Also
--------
plot_ccpr_grid : Creates CCPR plot for multiple regressors in a plot grid.

Notes
-----
The CCPR plot provides a way to judge the effect of one regressor on the
response variable by taking into account the effects of the other
independent variables. The partial residuals plot is defined as
Residuals + B_i*X_i versus X_i. The component adds the B_i*X_i versus
X_i to show where the fitted line would lie. Care should be taken if X_i
is highly correlated with any of the other independent variables. If this
is the case, the variance evident in the plot will be an underestimate of
the true variance.

References
----------
http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/ccpr.htm

Examples
--------
Using the state crime dataset plot the effect of the rate of single
households ('single') on the murder rate while accounting for high school
graduation rate ('hs_grad'), percentage of people in an urban area, and rate
of poverty ('poverty').

>>> import statsmodels.api as sm
>>> import matplotlib.pyplot as plt
>>> import statsmodels.formula.api as smf

>>> crime_data = sm.datasets.statecrime.load_pandas()
>>> results = smf.ols('murder ~ hs_grad + urban + poverty + single',
...                   data=crime_data.data).fit()
>>> sm.graphics.plot_ccpr(results, 'single')
>>> plt.show()

.. plot:: plots/graphics_regression_ccpr.py
Nrg   r   )add_constantru   z*Component and component plus residual plotzResidual + %s*beta_%dz%s)r	   rK   rL   rM   r   rO   r   r5   rn   statsmodels.tools.toolsr   r   r   r   r   rU   rW   rV   )
r*   rY   r7   r\   r]   r_   x1betar   modr   s
             r+   r   r   <  s    | !!"%GC11(MMJI"7+G			AxK	(Bx((FGGB&,4
fl2&
'
+
+
-CZZF
v
-
-CLL=>MM)Y,AABMM$"#Jr-   c                 p   [         R                  " U5      n[         R                  " XR                  5      u  pAUb  Uu  pVOJ[	        U5      S:  a.  [        [        R                  " [	        U5      S-  5      5      nSnO[	        U5      nSnSn[        U5       Hk  u  pU R                  R                  SS2U	4   R                  5       S:X  a  SnM8  UR                  XVUS-   U-
  5      n
[        X	U
S9nU
R                  S5        Mm     UR                  SS	S
9  UR                  5         UR!                  SS9  U$ )a  
Generate CCPR plots against a set of regressors, plot in a grid.

Generates a grid of component and component-plus-residual (CCPR) plots.

Parameters
----------
results : result instance
    A results instance with exog and params.
exog_idx : None or list of int
    The indices or column names of the exog used in the plot.
grid : None or tuple of int (nrows, ncols)
    If grid is given, then it is used for the arrangement of the subplots.
    If grid is None, then ncol is one, if there are only 2 subplots, and
    the number of columns is two otherwise.
fig : Figure, optional
    If given, this figure is simply returned.  Otherwise a new figure is
    created.

Returns
-------
Figure
    If `ax` is None, the created figure.  Otherwise the figure to which
    `ax` is connected.

See Also
--------
plot_ccpr : Creates CCPR plot for a single regressor.

Notes
-----
Partial residual plots are formed as::

    Res + Betahat(i)*Xi versus Xi

and CCPR adds::

    Betahat(i)*Xi versus Xi

References
----------
See http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/ccpr.htm

Examples
--------
Using the state crime dataset separately plot the effect of the each
variable on the on the outcome, murder rate while accounting for the effect
of all other variables in the model.

>>> import statsmodels.api as sm
>>> import matplotlib.pyplot as plt
>>> import statsmodels.formula.api as smf

>>> fig = plt.figure(figsize=(8, 8))
>>> crime_data = sm.datasets.statecrime.load_pandas()
>>> results = smf.ols('murder ~ hs_grad + urban + poverty + single',
...                   data=crime_data.data).fit()
>>> sm.graphics.plot_ccpr_grid(results, fig=fig)
>>> plt.show()

.. plot:: plots/graphics_regression_ccpr_grid.py
Nrf   r'   r   r   )rY   r7   r   z&Component-Component Plus Residual Plotrj   rk   r   rv   )r	   rx   rL   rM   r   intrP   ceilr   rO   varry   r   rU   r   r   r   )r*   rY   r   r\   r]   r   r   seen_constantr   r   r7   s              r+   r   r     s   ~ 

s
#C11(MMJIux=1Hb 012EEMEEMH%==af%))+q0M__U1Q3}+<="5
R & LL9GLLC Jr-   c                 F  ^ ^ Ub  UR                  5       nOSn[        R                  " U5      u  pU(       ai  UR                  u  m mUcV  UR                  R
                  SS2S4   R                  5       UR                  R
                  SS2S4   R                  5       /nO$T b  Tc  [        S5      eUc  UR                  5       nUS   T-  T -   US   T-  T -   /n	UR                  U5        SSK
Jn
   " U U4S jSU
5      nU" Xy40 UD6nUR                  U5        UR                  R                  SUR                  5      Ul        UR                  R                  S	UR                  5      Ul        U(       a  UR%                  U5        U(       a  UR'                  U5        U$ )
a@  
Plot a line given an intercept and slope.

Parameters
----------
intercept : float
    The intercept of the line.
slope : float
    The slope of the line.
horiz : float or array_like
    Data for horizontal lines on the y-axis.
vert : array_like
    Data for verterical lines on the x-axis.
model_results : statsmodels results instance
    Any object that has a two-value `params` attribute. Assumed that it
    is (intercept, slope).
ax : axes, optional
    Matplotlib axes instance.
**kwargs
    Options passed to matplotlib.pyplot.plt.

Returns
-------
Figure
    The figure given by `ax.figure` or a new instance.

Examples
--------
>>> import numpy as np
>>> import statsmodels.api as sm

>>> np.random.seed(12345)
>>> X = sm.add_constant(np.random.normal(0, 20, size=30))
>>> y = np.dot(X, [25, 3.5]) + np.random.normal(0, 30, size=30)
>>> mod = sm.OLS(y,X).fit()
>>> fig = sm.graphics.abline_plot(model_results=mod)
>>> ax = fig.axes[0]
>>> ax.scatter(X[:,1], y)
>>> ax.margins(.1)
>>> import matplotlib.pyplot as plt
>>> plt.show()

.. plot:: plots/graphics_regression_abline.py
Nr   z-specify slope and intercepty or model_resultsr   )Line2Dc                   D   >^  \ rS rSrU 4S jrU 4S jrUU4S jrSrU =r$ )abline_plot.<locals>.ABLine2Di4  c                 B   > [         TU ]  " U0 UD6  S U l        S U l        g N)super__init__id_xlim_callbackid_ylim_callback)selfargsr[   	__class__s      r+   r   &abline_plot.<locals>.ABLine2D.__init__5  s%    Gd-f-$(D!$(D!r-   c                   > U R                   nU R                  (       a%  UR                  R                  U R                  5        U R                  (       a%  UR                  R                  U R                  5        [
        TU ]  5         g r   )axesr   	callbacks
disconnectr   r   remove)r   r7   r   s     r+   r   $abline_plot.<locals>.ABLine2D.remove:  sX    B$$''(=(=>$$''(=(=>GNr-   c                 B  > UR                  S5        UR                  5       nU Vs/ s H  o3U L d  M	  UPM     nnUS   nUR                  5       nUS   T	-  T-   US   T	-  T-   /nUR                  Xg5        UR                  R
                  R                  5         g s  snf )NFr   r   )set_autoscale_onget_childrenget_xlimset_datar6   canvasdraw)
r   r7   childrenchildablinesabliner   r^   	interceptslopes
           r+   update_datalim,abline_plot.<locals>.ABLine2D.update_datalimB  s    &(H*2D(tmu(GDQZFA1	)1Q4%<)+CDAOOA!II!!# Es
   BB)r   r   )	__name__
__module____qualname____firstlineno__r   r   r   __static_attributes____classcell__)r   r   r   s   @r+   ABLine2Dr   4  s    	)
		$ 	$r-   r   xlim_changedylim_changed)r   r	   rK   r   rM   rO   minmaxr   set_xlimmatplotlib.linesr   add_liner   connectr   r   r   hlinevline)r   r   horizvertmodel_resultsr7   r[   r   r\   data_yr   r   lines   ``           r+   r   r     sy   \ 
~KKM!!"%GC(//	59$$))!Q$/335$$))!Q$/3357A %%*;LMM9Ad5j"AaDJy$89FKKN ($ $6 $2 A((DKKLL00ATATUDLL00ATATUD

Jr-   extra_params_doczresults: object
        Results for a fitted regression model.
    influence: instance
        The instance of Influence for model.c
           
         Un[         R                  " U5      u  pUR                  5       R                  S5      (       a  UR                  S   nOVUR                  5       R                  S5      (       a$  [
        R                  " UR                  S   5      nO[        SU-  5      e[
        R                  " U5      nUS-  S-
  nXR                  5       -
  U-  U-  S-   nUc  UR                  nU	c#  SnU(       a  UR                  n	O%UR                  n	O[
        R                  " U	5      n	SnSS	KJn  UR"                  R%                  S
US-  -
  U R&                  5      n[
        R                  " U	5      U:  nU[)        U 5      :  n[
        R*                  " UU5      nUR-                  XXS9  U R.                  R0                  R2                  nUc  [5        [7        U	5      5      n[         R8                  " [
        R:                  " U5      S   U[=        X5      [=        US-  S-  * US-  S-  5      SU5      nSSS.nUR>                  " U40 UD6  UR@                  " S0 UD6  URB                  " S0 UD6  U$ )Ncoor   dffzCriterion %s not understoodrf   @   zStudentized Residuals	Residuals)stats      ?)srG   ri   r      rm   )rl   rB   )Leverage)zInfluence Plot)"r	   rK   lower
startswithcooks_distancerP   absdffitsr   ptpr  hat_matrix_diagresid_studentized_externalresid_studentizedr   scipyr  tppfdf_residr,   
logical_orscatterrM   r   r   r   r   r   wherer   rW   rV   rU   )r*   	influenceexternalrG   	criterionr   
plot_alphar7   leveragern   r[   inflr\   psize	old_range	new_rangeylabelr  cutofflarge_residlarge_leveragelarge_pointslabelsfonts                           r+   _influence_plotr;  Y  s    D!!"%GC##E**##A&			%	%e	,	,t{{1~&6BCC uIa$IYY[ I-i7$>E''}(33E**E

5!WW[[E!GW%5%56F&&-&(Kw 77N==n=LJJx%J: ]]**F~E
#			RXXl3A6!(2!E!Gb=.57R-@)
!B W-DMM&!D!MM%%LL*T*Jr-   z@results : Results
        Results for a fitted regression model.c           
      F    U R                  5       n[        X4XX4XVS.UD6n	U	$ )N)r,  rG   r-  r   r.  r7   )get_influencer;  )
r*   r,  rG   r-  r   r.  r7   r[   r0  ress
             r+   r   r     s=       "D
' B($-%/B:@BC Jr-   zqresults: object
    Results for a fitted regression model
influence: instance
    instance of Influence for modelc                    SSK JnJn  [        R                  " U5      u  psUnUR
                  n	U" UR                  5      n
UR                  " U
S-  U	S40 UD6  UR                  S5        UR                  S5        UR                  S5        U	[        U 5      :  nUR                  SUS-  -
  5      n[        R                  " U
5      U:  nU R                  R                   R"                  nUc  [%        ['        U R(                  5      5      n[        R*                  " [        R,                  " X5      5      S   n[        R.                  " X[1        U
S-  U	5      S	/['        U R(                  5      -  S
USSS9nUR3                  SS5        U$ )Nr   )normzscorerf   rg   zNormalized residuals**2r  z)Leverage vs. Normalized residuals squaredr  r   rj   r   r   )r7   r   r   g333333?)scipy.statsr@  rA  r	   rK   r!  rn   r5   rV   rW   rU   r,   r&  rP   r  rM   r   r   r   r   r)   r*  r(  r   r   margins)r*   r+  rG   r7   r[   r@  rA  r\   r0  r/  rn   r7  r5  r6  r9  rr   s                   r+   _plot_leverage_resid2rD    sF    )!!"%GCD##H4::EGGE1Hh.v.MM+,MM*LL<=w 77NXXbqj!F&&-&(K]]**F~GLL)*HHR]]>?@CE			UD8,D$Xc',,&77 "xH
>B JJtTJr-   z:results : object
    Results for a fitted regression modelc                 >    U R                  5       n[        X4XS.UD6$ )N)rG   r7   )r=  rD  )r*   rG   r7   r[   r0  s        r+   r   r     s'    
   "D MeMfMMr-   c                 T   U R                   n[        R                  " U5      u  pu[        XUUUS9u  pUR	                  XSSS9  UR                  SSS9  [        U[        5      (       a  Un
OUR                  U   n
UR                  U
SS	9  UR                  UR                  S
-   SS	9  U$ )N)
resid_typeuse_glm_weights
fit_kwargsrg   333333?rG   zAdded variable plotrj   rk      r   z
 residuals)rM   r	   rK   r    r5   rU   r   r   r   rV   rW   rR   )r*   
focus_exogrG  rH  rI  r7   rM   r\   endog_residfocus_exog_residxnames              r+   r#   r#     s     MME!!"%GC 'w2<7F2<> "K GG3cG:LL&L9*c""  ,MM%bM!MM%##l2M<Jr-   c                    U R                   n[        R                  " X5      u  p[        X5      nU R                   R                  S S 2U4   n[        R
                  " U5      u  prUR                  XeSSS9  UR                  SSS9  [        U[        5      (       a  UnOUR                  U   nUR                  USS9  UR                  S	SS9  U$ )
Nrg   rJ  rK  zPartial residuals plotrj   rk   rL  rM  Component plus residual)rM   r	   rL   r!   rO   rK   r5   rU   r   r   r   rV   rW   )	r*   rN  r7   rM   	focus_colprfocus_exog_valsr\   rQ  s	            r+   r$   r$     s     MME!33JFJ		,Bmm((I6O!!"%GCGGOCG0LL)GL<*c""  ,MM%bM!MM+"M5Jr-   zHresults : Results
        Results instance of a fitted regression model.c                 <   U R                   n[        R                  " X5      u  p[        XUUS9nUR                  S S 2U4   n[        R
                  " U5      u  pUR                  XSSS9  UR                  SSS9  UR                  USS	9  UR                  S
SS	9  U	$ )N)r/   
cond_meansrg   rJ  rK  zCERES residuals plotrj   rk   rL  rM  rS  )
rM   r	   rL   r"   rO   rK   r5   rU   rV   rW   )
r*   rN  r/   rX  r7   rM   rT  presidrV  r\   s
             r+   r%   r%     s     MME!33JFJ'D%/1F jjI.O!!"%GCGGOSG4LL''L:MM*2M&MM+"M5Jr-   c                 T   U R                   n[        U[        [        [        45      (       d"  [        SUR                  R                  -  5      e[        R                  " X5      u  p[        [        U R                  5      5      n[        U5      nUR                  U5        [        U5      nUc  UR                  SS2U4   nXR!                  S5      -  n["        R$                  R'                  US5      u  pn["        R(                  " U
S:  5      nU	SS2U4   nUR                  SS2U4   n["        R*                  " [        U5      UR,                  S   45      n[        UR,                  S   5       H   nUSS2U4   n[/        XUSS9nUUSS2U4'   M"     ["        R0                  " UR                  SS2U4   U4SS9nUR                  nUR3                  5       nU" UR4                  U40 UD6nUR7                  5       nUR4                  UR8                  -
  n[        U[        [        45      (       a2  UUR:                  R<                  R?                  UR8                  5      -  nUR,                  S   U:  a0  U["        R@                  " USS2US24   UR                  US 5      -  nU$ )	a  
Calculate the CERES residuals (Conditional Expectation Partial
Residuals) for a fitted model.

Parameters
----------
results : model results instance
    The fitted model for which the CERES residuals are calculated.
focus_exog : int
    The column of results.model.exog used as the 'focus variable'.
frac : float, optional
    Lowess smoothing parameter for estimating the conditional
    means.  Not used if `cond_means` is provided.
cond_means : array_like, optional
    If provided, the columns of this array are the conditional
    means E[exog | focus exog], where exog ranges over some
    or all of the columns of exog other than focus exog.  If
    this is an empty nx0 array, the conditional means are
    treated as being zero.  If None, the conditional means are
    estimated.

Returns
-------
An array containing the CERES residuals.

Notes
-----
If `cond_means` is not provided, it is obtained by smoothing each
column of exog (except the focus column) against the focus column.

Currently only supports GLM, GEE, and OLS models.
z$ceres residuals not available for %sNr   gư>r   F)r/   return_sorted)axis)!rM   r   r   r   r   r   r   r   r	   rL   ranger   r   r   r   rO   meanrP   linalgsvdflatnonzeror   r|   r
   concatenate_get_init_kwdsrN   r   rS   familylinkderivdot)r*   rN  r/   rX  rM   rT  ix_nfnnfpexogur  vtiifcoljr:   cfnew_exogklassinit_kwargs	new_model
new_resultrY  s                          r+   r"   r"   *  sM   D MMEec3_--?112 3 	3 "33JFJ #gnn%&EKE	IIi
e*C 
 

1e8$A99==*b^^AH%!R%zz!Y,'XXs4y%++a.9:
u{{1~&A q!tBt5AB!Jq!t ' ~~uzz!U(3Z@qIH OOE&&(Kekk8;{;IJ [[:222F%#s$$%,,##))**A*ABB~~a3"&&!ST'*J,=,=cd,CDDMr-   c                    U R                   nUR                  U R                  5       -
  n[        U[        [
        45      (       a2  X2R                  R                  R                  U R                  5      -  nO8[        U[        [        [        45      (       a  O[        S[        U5      -  5      e[        U5      [        L a  UR                   R#                  U5      nOUnU R$                  U   UR&                  SS2U4   -  nXS-   $ )a  
Returns partial residuals for a fitted model with respect to a
'focus predictor'.

Parameters
----------
results : results instance
    A fitted regression model.
focus col : int
    The column index of model.exog with respect to which the
    partial residuals are calculated.

Returns
-------
An array of partial residuals.

References
----------
RD Cook and R Croos-Dabrera (1998).  Partial residual plots in
generalized linear models.  Journal of the American Statistical
Association, 93:442.
z+Partial residuals for '%s' not implemented.N)rM   rN   predictr   r   r   rd  re  rf  rS   r   r   r   r   typer   r   rr   r   rO   )r*   rN  rM   rn   rT  	focus_vals         r+   r!   r!     s    8 MMEKK'//++E%#s$$""(()=)=>>	ECc?	+	+F;' ( 	( J3$$**:6		y)EJJq)|,DDIr-   c                    U R                   n[        U[        [        [        45      (       d"  [        SUR                  R                  -  5      eUR                  nUR                  n[        R                  " X5      u  pUSS2U4   n	Uc   [        U[        [        45      (       a  SnOSn[        UR                  S   5      n
[        U
5      n
U
R                  U5        USS2U
4   nU R                   U
   nUR                  nUR#                  5       nU" X{40 UD6nSU0nUb  UR%                  U5        UR&                  " S0 UD6n[)        USS5      (       d  [        S	5      e [)        UU5      nSSKJs  Jn  [        U[        [        45      (       am  U(       af  UR2                  R5                  U R6                  5      n[9        US5      (       a  UUR:                  -  nUR=                  XU5      R'                  5       nOUR	                  X5      R'                  5       nUR>                  nUU4$ ! [*         a    [        S
U-  5      ef = f)a  
Residualize the endog variable and a 'focus' exog variable in a
regression model with respect to the other exog variables.

Parameters
----------
results : regression results instance
    A fitted model including the focus exog and all other
    predictors of interest.
focus_exog : {int, str}
    The column of results.model.exog or a variable name that is
    to be residualized against the other predictors.
resid_type : str
    The type of residuals to use for the dependent variable.  If
    None, uses `resid_deviance` for GLM/GEE and `resid` otherwise.
use_glm_weights : bool
    Only used if the model is a GLM or GEE.  If True, the
    residuals for the focus predictor are computed using WLS, with
    the weights obtained from the IRLS calculations for fitting
    the GLM.  If False, unweighted regression is used.
fit_kwargs : dict, optional
    Keyword arguments to be passed to fit when refitting the
    model.

Returns
-------
endog_resid : array_like
    The residuals for the original exog
focus_exog_resid : array_like
    The residuals for the focus predictor

Notes
-----
The 'focus variable' residuals are always obtained using linear
regression.

Currently only GLM, GEE, and OLS models are supported.
z8model type %s not supported for added variable residualsNresid_deviancern   r   start_params	convergedTz>fit did not converge when calculating added variable residualsz '%s' residual type not availabler   data_weights ) rM   r   r   r   r   r   r   r   rO   rN   r	   rL   r]  r|   r   r   r   rc  r   r   getattrAttributeError#statsmodels.regression.linear_model
regressionlinear_modelrd  weightsrS   r   r~  r   rn   )r*   rN  rG  rH  rI  rM   rO   rN   rT  rV  rm  reduced_exogr|  rr  r[   rt  r   ru  rO  lmr  
lm_resultsrP  s                          r+   r    r      s   R MMEec3_--S112 3 	3 ::DKKE!33JFJ1i<(O ec3Z(()J J	tzz!}	B	bBFF92;L>>"%LOOE!!#Fe4V4IL)DJ&&J:{D11YZZJj*5 54%#s$$,,&&w';';<5.)) 2 22GVVO7CGGI
VVO:>>@
!''(((  J;jHIIJs   $H0 0I	)r   g?)NNTr   )NNN)NNNNNNr  )T皙?cooks0         ?NNN)Tr  r  r  r  N)r  N)NTNN)Q?NN)r  N)NTN)8__doc__statsmodels.compat.pandasr   statsmodels.compat.pythonr   r   numpyrP   r~   r   patsyr   3statsmodels.genmod.generalized_estimating_equationsr   +statsmodels.genmod.generalized_linear_modelr   statsmodels.graphicsr	   *statsmodels.nonparametric.smoothers_lowessr
   r  r   r   r   &statsmodels.sandbox.regression.predstdr   r   r   _regressionplots_docr   r   r   r   r   __all__r,   r   r   r   r   r   r   r   r   r   formatr;  r   rD  r   r#   r$   r%   r"   r!   r    r  r-   r+   <module>r     sh   / 2    C ; & = = = E 8 =4
8]@bJ!: 7;"$BdNcLPf\~ >B'+hV 


$
$ I G(H I J
 >ACG)-;J
;| 


$
$ K I(J K L AH/3L 

#
*
* >,? @ A
A
: 

#
*
* D,E F GNGN 

" D&E E F 9=BFF6 

% D)E E FF2 

# !'" " # EI 	#.Wr.` ;?;?])r-   