
    >hA                     R    S r SSKrSSKrSSKJr   " S S5      rS r " S S5      r	g)	a  
Mediation analysis

Implements algorithm 1 ('parametric inference') and algorithm 2
('nonparametric inference') from:

Imai, Keele, Tingley (2010).  A general approach to causal mediation
analysis. Psychological Methods 15:4, 309-334.

http://imai.princeton.edu/research/files/BaronKenny.pdf

The algorithms are described on page 317 of the paper.

In the case of linear models with no interactions involving the
mediator, the results should be similar or identical to the earlier
Barron-Kenny approach.
    N)maybe_name_or_idxc                   Z    \ rS rSrSr   SS jrS rS rS rS r	S	 r
SS
 jrSS jrSrg)	Mediation   a  
Conduct a mediation analysis.

Parameters
----------
outcome_model : statsmodels model
    Regression model for the outcome.  Predictor variables include
    the treatment/exposure, the mediator, and any other variables
    of interest.
mediator_model : statsmodels model
    Regression model for the mediator variable.  Predictor
    variables include the treatment/exposure and any other
    variables of interest.
exposure : str or (int, int) tuple
    The name or column position of the treatment/exposure
    variable.  If positions are given, the first integer is the
    column position of the exposure variable in the outcome model
    and the second integer is the position of the exposure variable
    in the mediator model.  If a string is given, it must be the name
    of the exposure variable in both regression models.
mediator : {str, int}
    The name or column position of the mediator variable in the
    outcome regression model.  If None, infer the name from the
    mediator model formula (if present).
moderators : dict
    Map from variable names or index positions to values of
    moderator variables that are held fixed when calculating
    mediation effects.  If the keys are index position they must
    be tuples `(i, j)` where `i` is the index in the outcome model
    and `j` is the index in the mediator model.  Otherwise the
    keys must be variable names.
outcome_fit_kwargs : dict-like
    Keyword arguments to use when fitting the outcome model.
mediator_fit_kwargs : dict-like
    Keyword arguments to use when fitting the mediator model.
outcome_predict_kwargs : dict-like
    Keyword arguments to use when calling predict on the outcome
    model.

Returns a ``MediationResults`` object.

Notes
-----
The mediator model class must implement ``get_distribution``.

Examples
--------
A basic mediation analysis using formulas:

>>> import statsmodels.api as sm
>>> import statsmodels.genmod.families.links as links
>>> probit = links.probit
>>> outcome_model = sm.GLM.from_formula("cong_mesg ~ emo + treat + age + educ + gender + income",
...                                     data, family=sm.families.Binomial(link=Probit()))
>>> mediator_model = sm.OLS.from_formula("emo ~ treat + age + educ + gender + income", data)
>>> med = Mediation(outcome_model, mediator_model, "treat", "emo").fit()
>>> med.summary()

A basic mediation analysis without formulas.  This may be slightly
faster than the approach using formulas.  If there are any
interactions involving the treatment or mediator variables this
approach will not work, you must use formulas.

>>> import patsy
>>> outcome = np.asarray(data["cong_mesg"])
>>> outcome_exog = patsy.dmatrix("emo + treat + age + educ + gender + income", data,
...                              return_type='dataframe')
>>> probit = sm.families.links.probit
>>> outcome_model = sm.GLM(outcome, outcome_exog, family=sm.families.Binomial(link=Probit()))
>>> mediator = np.asarray(data["emo"])
>>> mediator_exog = patsy.dmatrix("treat + age + educ + gender + income", data,
...                               return_type='dataframe')
>>> mediator_model = sm.OLS(mediator, mediator_exog)
>>> tx_pos = [outcome_exog.columns.tolist().index("treat"),
...           mediator_exog.columns.tolist().index("treat")]
>>> med_pos = outcome_exog.columns.tolist().index("emo")
>>> med = Mediation(outcome_model, mediator_model, tx_pos, med_pos).fit()
>>> med.summary()

A moderated mediation analysis.  The mediation effect is computed
for people of age 20.

>>> fml = "cong_mesg ~ emo + treat*age + emo*age + educ + gender + income",
>>> outcome_model = sm.GLM.from_formula(fml, data,
...                                      family=sm.families.Binomial())
>>> mediator_model = sm.OLS.from_formula("emo ~ treat*age + educ + gender + income", data)
>>> moderators = {"age" : 20}
>>> med = Mediation(outcome_model, mediator_model, "treat", "emo",
...                 moderators=moderators).fit()

References
----------
Imai, Keele, Tingley (2010).  A general approach to causal mediation
analysis. Psychological Methods 15:4, 309-334.
http://imai.princeton.edu/research/files/BaronKenny.pdf

Tingley, Yamamoto, Hirose, Keele, Imai (2014).  mediation : R
package for causal mediation analysis.  Journal of Statistical
Software 59:5.  http://www.jstatsoft.org/v59/i05/paper
Nc	                    Xl         X l        X0l        Ub  UO0 U l        Uc  U R	                  US5      U l        OX@l        U b  UO0 U l        U b  UO0 U l        Ub  UO0 U l        UR                  R                  5       U l        UR                  R                  5       U l        U R                  SS5      U l        U R                  SS5      U l        U R                  SS5      U l        g )Nmediatorexposureoutcome)outcome_modelmediator_modelr	   
moderators_guess_endog_namer   _outcome_fit_kwargs_mediator_fit_kwargs_outcome_predict_kwargsexogcopy_outcome_exog_mediator_exog_variable_pos_exp_pos_mediator_exp_pos_outcome_med_pos_outcome)	selfr   r   r	   r   r   outcome_fit_kwargsmediator_fit_kwargsoutcome_predict_kwargss	            nC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/stats/mediation.py__init__Mediation.__init__}   s    +, (2(>*B 22>:NDM$M:L;$6!# 	 <O=%8!# 	! '2 )?8: 	$ +//446,11668 "&!3!3J
!K !% 2 2:y I !% 2 2:y I    c                 B   US:X  a  U R                   nOU R                  nUS:X  a  [        U R                  U5      S   $ U R                  n[        U5      S:H  =(       a    [        U[        5      (       + nU(       a  US:X  a  US   $ US:X  a  US   $ g [        XC5      S   $ )Nr         r
   r   )r   r   r   r   r	   len
isinstancestr)r   varmodelmodexpexp_is_2s         r   r   Mediation._variable_pos   s    J%%C$$C*$T]]C8;;mmX]@JsC,@(@	!1v*$1v % %S.q11r!   c                     [        US5      (       a,  UR                  R                  S5      S   R                  5       $ [	        SU-  5      e)Nformula~r   z$cannot infer %s name without formula)hasattrr/   splitstrip
ValueError)r   r)   typs      r   r   Mediation._guess_endog_name   sC    5)$$==&&s+A.4466CcIJJr!   c                 x    UR                   nUR                  5       n[        R                  R	                  X#5      $ )z>
Simulate model parameters from fitted sampling distribution.
)params
cov_paramsnprandommultivariate_normal)r   resultmncovs       r   _simulate_paramsMediation._simulate_params   s0     ]]!yy,,R55r!   c                 V   U R                   n[        U R                  S5      (       dA  XSS2U R                  4'   U R                   H  nU R                  U   nXBSS2US   4'   M     U$ U R                  R
                  R                  R                  5       nXU R                  '   U R                   H$  nU R                  U   nXER                  SS2U4'   M&     U R                  R                  nU R                  R                  5       nUR                  " SSU0UD6n	U	R                  nU$ )zu
Return the mediator exog matrix with exposure set to the given
value.  Set values of moderated variables as needed.
r/   Nr#   data )r   r1   r   r   r   rC   framer   r	   loc	__class___get_init_kwdsfrom_formular   )
r   r	   mediator_exogixvdfvnameklassinit_kwargsr)   s
             r   _get_mediator_exogMediation._get_mediator_exog   s   
 ++t**I667?!T3334ooOOB'*+aAh' &  $$))//446B (t}}OOE*#$q%x  ) ''11E--<<>K&&>B>+>E!JJMr!   c                 z   U R                   n[        U R                  S5      (       dS  X#SS2U R                  4'   XSS2U R                  4'   U R
                   H  nU R
                  U   nXSSS2US   4'   M     U$ U R                  R                  R                  R                  5       nXU R                  '   X&U R                  '   U R
                   H  nU R
                  U   nXVU'   M     U R                  R                  nU R                  R                  5       n	UR                  " SSU0U	D6n
U
R                  nU$ )z
Retun the exog design matrix with mediator and exposure set to
the given values.  Set values of moderated variables as
needed.
r/   Nr   rC   rD   )r   r1   r   r   r   r   rC   rE   r   r	   r   rG   rH   rI   r   )r   r	   r   outcome_exogrK   rL   rM   rN   rO   rP   r)   s              r   _get_outcome_exogMediation._get_outcome_exog   s'    ))t))9555=D11125=D1112ooOOB')*Q1X& &   ##((..335B (t}} (t}}OOE*5	 ) &&00E,,;;=K&&>B>+>E ::Lr!   c                 ,   UR                   nUR                  5       nUR                  nUR                  nU(       a?  [        R
                  R                  S[        U5      [        U5      5      nXh   nXxS S 24   nU" Xg40 UD6n	U	R                  " S0 UD6$ )Nr   rD   )	rG   rH   endogr   r:   r;   randintr%   fit)
r   r)   
fit_kwargsbootrO   rP   rX   r   iir   s
             r   
_fit_modelMediation._fit_model   s    **,zz""1c%j#e*=BIEA;De9[9  .:..r!   c                    UR                  S5      (       aM  U R                  U R                  U R                  5      nU R                  U R                  U R
                  5      nO!UR                  S5      (       d  [        S5      e/ / /n/ / /n[        U5       GH  nUS:X  a#  U R                  W5      nU R                  W5      n	ObU R                  U R                  U R                  SS9nUR                  nU R                  U R                  U R
                  SS9nUR                  n	SS/SS//n
S H  nU R                  U5      nS	U0n[        US
5      (       a  UR                  US
'   U R                  R                  " U	40 UD6nUR                  UR                  S   5      nS HD  nU R!                  UU5      nU R                  R"                  " UU40 U R$                  D6nUX   U'   MF     M     S HI  nUU   R'                  U
S   U   U
S   U   -
  5        UU   R'                  U
U   S   U
U   S   -
  5        MK     GM     S HO  n[(        R*                  " UU   5      R,                  UU'   [(        R*                  " UU   5      R,                  UU'   MQ     XPl        X`l        [3        U R.                  U R0                  5      nUUl        U$ )z
Fit a regression model to assess mediation.

Parameters
----------
method : str
    Either 'parametric' or 'bootstrap'.
n_rep : int
    The number of simulation replications.

Returns a MediationResults object.
parar\   z1method must be either 'parametric' or 'bootstrap'
parametricT)r\   Nr   r#   r   scaler   r#   )
startswithr^   r   r   r   r   r4   ranger@   r8   rQ   r1   rd   get_distributionrvsshaperU   predictr   appendr:   asarrayTindirect_effectsdirect_effectsMediationResultsmethod)r   rq   n_repoutcome_resultmediator_resultrn   ro   iteroutcome_paramsmediation_paramspredicted_outcomestmmexkwargsgenpotential_mediatorteoexpotrslts                        r   rZ   Mediation.fit	  s    V$$!__T-?-?AYAYZN"ood.A.A4C\C\]O""6**C  8b%LD%!%!6!6~!F $(#8#8#I !%1C1C151I1IPT "1 "V!/!6!6"&//$2E2E262K2KRV #2 #X#2#9#9 
 $(,t!=--b1 #?G44&5&;&;F7O))::;K E=CE%(WWSYYq\%:"B005GHC++33NC <"::<B13&*2.	    #**+=a+@+CFXYZF[\]F^+^_q!(();A)>q)ADVWXDYZ[D\)\] G !N A"$**-=a-@"A"C"CQ "

>!+< = ? ?N1  !1, 5 5t7J7JKr!   )r   r   r   r   r   r   r   r   ro   r	   rn   r   r   r   r   )NNNNN)F)rb   i  )__name__
__module____qualname____firstlineno____doc__r   r   r   r@   rQ   rU   r^   rZ   __static_attributes__rD   r!   r   r   r      sB    cJ JNOS(, JF2*K64:
/Jr!   r   c                 z    S[        [        U S:  5      [        U S:  5      5      -  [        [        U 5      5      -  $ )Nr$   r   )minsumfloatr%   )vecs    r   _pvaluer   V  s1    s3sQw<S1W..s3x@@r!   c                   (    \ rS rSrSrS rSS jrSrg)rp   iZ  z
A class for holding the results of a mediation analysis.

The following terms are used in the summary output:

ACME : average causal mediated effect
ADE : average direct effect
c                    Xl         X l        S S /nS S /nS H-  nX   R                  S5      X5'   X%   R                  S5      XE'   M/     US   U l        US   U l        US   U l        US   U l        U R                  U R                  -   U R
                  -   U R                  -   S-  U l        U R                  U R                  -  U l        U R                  U R                  -  U l	        U R                  U R                  -   S-  U l
        U R                  U R                  -   S-  U l        U R
                  U R                  -   S-  U l        g )Nrc   r   r#   r$   )rn   ro   mean	ACME_ctrlACME_txADE_ctrlADE_txtotal_effectprop_med_ctrlprop_med_txprop_med_avgACME_avgADE_avg)r   rn   ro   indirect_effects_avgdirect_effects_avgr   s         r   r   MediationResults.__init__d  s7    0, $d|"D\A&6&9&>&>q&A #$2$5$:$:1$=!  .a0+A.*1-(+!^^dll:T]]JT[[X\]]!^^d.?.??<<$*;*;;!//$2B2BBaG$,,6!;3q8r!   c                 F   / SQn/ SQn[         R                  " X#S9n[        U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  /
5       H  u  pVX`R                  L d  X`R                  L d  X`R                  L a&  [        R                  " U5      UR                  US4'   OUR!                  5       UR                  US4'   [        R"                  " USU-  S-  5      UR                  US4'   [        R"                  " USSUS-  -
  -  5      UR                  US4'   [%        U5      UR                  US4'   M     UR'                  [         R(                  S	S
9nU$ )z,
Provide a summary of a mediation analysis.
)EstimatezLower CI boundzUpper CI boundzP-value)
zACME (control)zACME (treated)zADE (control)zADE (treated)zTotal effectzProp. mediated (control)zProp. mediated (treated)zACME (average)zADE (average)zProp. mediated (average))columnsindexr   d   r$   r#      coerce)errors)pd	DataFrame	enumerater   r   r   r   r   r   r   r   r   r   r:   medianilocr   
percentiler   apply
to_numeric)r   alphar   r   smryir   s          r   summaryMediationResults.summary|  sV   
 N- ||G9!%!%!2!2D4F4F!%!1!14==!%t/@/@	!B CFA ***7G7G0G---"$))C.		!Q$"%((*		!Q$ mmCuqADIIadO mmCEAI1FGDIIadO%clDIIadOC zz"--z9r!   )r   r   r   r   r   r   ro   rn   r   r   r   r   N)g?)r   r   r   r   r   r   r   r   rD   r!   r   rp   rp   Z  s    90 r!   rp   )
r   numpyr:   pandasr   statsmodels.graphics.utilsr   r   r   rp   rD   r!   r   <module>r      s4   "   8| |~	AB Br!   