Ë
    £�Dj8\  ã                   óŠ  — d dl mZ d dlZd dlZd dlmZmZ d dlZd dl	m
Z
 d dlmZmZmZ d dlmZ d dlmZ d„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Z G d„ d«      Z G d„ d«      Zedk(  r\d dlZd dlmZ  ej@                  ddg d¢¬«      Z! ede!¬«      jE                  «       Z# ede!¬«      jE                  «       Z$ ee#d¬«      Z%yy)é    )ÚlrangeN)Ú	DataFrameÚIndex)Ústats)Ú_has_interceptÚ_intercept_idxÚ_remove_intercept_patsy)Úsummary2)ÚOLSc                 óÊ   — |€| j                  «       S |dk(  r| j                  S |dk(  r| j                  S |dk(  r| j                  S |dk(  r| j                  S t        d|z  «      ‚)NÚhc0Úhc1Úhc2Úhc3z robust options %s not understood)Ú
cov_paramsÚcov_HC0Úcov_HC1Úcov_HC2Úcov_HC3Ú
ValueError)ÚmodelÚrobusts     ú[C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels\stats\anova.pyÚ_get_covariancer      sk   € Ø€~Ø×ÑÓ!Ð!Ø	�5ŠØ�}‰}ÐØ	�5ŠØ�}‰}ÐØ	�5ŠØ�}‰}ÐØ	�5ŠØ�}‰}ÐäÐ;¸fÑDÓEÐEó    c                 ó4  — |j                  dd«      }|j                  dd«      }|j                  dd«      }|j                  dd«      }|r|j                  «       }| j                  j                  }| j                  j                  }|j
                  d   }| j                  j                  }	| j                  j                  j                  }
| j                  j                  }t        |
j                  «      t        |
«      z
  dz   }d	|z  }d
dd||g}t        t        j                  |df«      |¬«      }|dv rt!        | ||||
|||||«
      S |dv rt#        | |
||||«      S |dv rt%        | |
||||«      S |dv rt'        d«      ‚t)        dt+        |«      z  «      ‚)a9  
    Anova table for one fitted linear model.

    Parameters
    ----------
    model : fitted linear model results instance
        A fitted linear model
    typ : int or str {1,2,3} or {"I","II","III"}
        Type of sum of squares to use.

    **kwargs**

    scale : float
        Estimate of variance, If None, will be estimated from the largest
    model. Default is None.
        test : str {"F", "Chisq", "Cp"} or None
        Test statistics to provide. Default is "F".

    Notes
    -----
    Use of this function is discouraged. Use anova_lm instead.
    ÚtestÚFÚscaleNÚtypé   r   r   zPR(>%s)ÚdfÚsum_sqÚmean_sqé   ©Úcolumns©r!   ÚI)é   ÚII)é   ÚIII)é   ÚIVzType IV not yet implementedzType %s not understood)ÚgetÚlowerr   ÚendogÚexogÚshapeÚendog_namesÚdataÚdesign_infoÚ
exog_namesÚlenÚtermsr   r   ÚnpÚzerosÚanova1_lm_singleÚanova2_lm_singleÚanova3_lm_singleÚNotImplementedErrorr   Ústr)r   Úkwargsr   r   r    r   r2   r3   ÚnobsÚresponse_namer7   r8   Ún_rowsÚpr_testÚnamesÚtables                   r   Úanova_singlerI   #   s˜  € ð. �:‰:�f˜cÓ"€DØ�J‰J�w Ó%€EØ
�*‰*�U˜AÓ
€CØ�Z‰Z˜ $Ó'€FÙØ—‘“ˆà�K‰K×Ñ€EØ�;‰;×Ñ€DØ�:‰:�a‰=€Dà—K‘K×+Ñ+€MØ—+‘+×"Ñ"×.Ñ.€KØ—‘×'Ñ'€Jä�+×#Ñ#Ó$¤~°kÓ'BÑBÀQÑF€Fà˜$Ñ€GØ�8˜Y¨¨gÐ6€Eä”b—h‘h ¨˜{Ó+°UÔ;€Eà
ˆh�Ü  u¨d°D¸+ÀuØ &¨¨g°vó?ð 	?à	�	Ñ	Ü  {°F¸DÀ'Ø &ó(ð 	(à	�
Ñ	Ü  {°F¸DÀ'Ø &ó(ð 	(à	�	Ñ	Ü!Ð"?Ó@Ð@äÐ1´C¸³HÑ<Ó=Ð=r   c
                 óš  — t        | dd«      }
|
€Bt        j                  j                  |«      \  }}t        j                  |j
                  |«      }
t        j                  t        |j                  «      t        |j                  «      f«      }|j                  D �cg c]  }|j                  |«      ‘Œ }}t        |«      D ]  \  }}d|||f<   Œ t        j                  ||
dz  «      }t        |«      }||    }t        j                  |j                  «      }||    }|j                  «       }t!        |dgz   «      |_        t        j$                  ||    j'                  d«      |f   |j(                  |ddgf<   | j*                  | j,                  f|j(                  dddgf<   |dk(  r�|d   |d   z  | j*                  | j,                  z  z  ||<   t.        j0                  j3                  |d   |d   | j,                  «      ||<   t        j4                  t        j4                  f|j(                  d||gf<   |d   |d   z  |d	<   |S c c}w )
aá  
    Anova table for one fitted linear model.

    Parameters
    ----------
    model : fitted linear model results instance
        A fitted linear model

    **kwargs**

    scale : float
        Estimate of variance, If None, will be estimated from the largest
    model. Default is None.
        test : str {"F", "Chisq", "Cp"} or None
        Test statistics to provide. Default is "F".

    Notes
    -----
    Use of this function is discouraged. Use anova_lm instead.
    ÚeffectsNr!   r*   ÚResidualr"   r#   r   r$   )Úgetattrr;   ÚlinalgÚqrÚdotÚTr<   r9   r:   Úcolumn_namesÚ
term_namesÚsliceÚ	enumerater   ÚarrayÚtolistr   ÚindexÚc_ÚsumÚlocÚssrÚdf_residr   ÚfÚsfÚnan)r   r2   r3   rC   r7   rH   rE   r   rF   r   rK   ÚqÚrÚarrÚnameÚslicesÚiÚslice_r#   ÚidxrS   rX   s                         r   r=   r=   _   s  € ô. �e˜Y¨Ó-€GØ€Ü�i‰i�l‰l˜4Ó ‰ˆˆ!Ü—&‘&˜Ÿ™˜eÓ$ˆä
�(‰(”C˜×)Ñ)Ó*¬C°×0HÑ0HÓ,IÐJÓ
K€CØ2=×2HÑ2HÖI¨$ˆk×Ñ Õ%ÐI€FÐIÜ˜fÓ%ò ‰ˆˆ&ØˆˆAˆvˆIŠðô �V‰V�C˜ !™Ó$€Fä
˜Ó
%€CØ�S�D‰\€FÜ—‘˜+×0Ñ0Ó1€JØ˜S˜DÑ!€Jà×ÑÓ€EÜ˜  Ñ,Ó-€E„KÜ)+¯©¨s°C°4©y¯}©}¸QÓ/?ÀÐ/GÑ)H€E‡I�Iˆe�d˜HÐ%Ð%Ñ&à-2¯Y©Y¸¿¹Ð-F€E‡I�Iˆj˜8 D˜/Ð)Ñ*Øˆs‚{Ø˜h™¨%°©+Ñ5ØŸ	™	 E§N¡NÑ2ñ4ˆˆd‰äŸ™Ÿ™ E¨#¡J°°d±Ø$)§N¡Nó4ˆˆg‰ä13·±¼¿¹°ˆ�	‰	�*˜t W˜oÐ-Ñ.Ø˜X‘¨¨t©Ñ4€Eˆ)ÑØ€Lùò/ Js   ÂIc                 ó6  — |j                   dd }t        |«      }dd||g}t        t        j                  |df«      |¬«      }t        | d«      }	t        | |«      }
g }g }t        |«      D �]Ú  \  }}|j                  |«      }t        |j                  |j                  «      }g }t        |j                  «      }|D ]ž  }t        |j                  «      }|j                  |«      sŒ*||k(  rŒ0|j                  |«      }|j                  t        |j                  |j                  «      «       |j                  t        |j                  |j                  «      «       Œ  t        j                  | j                   j"                  j$                  d   «      |   }t        j                  | j                   j"                  j$                  d   «      |   }|j&                  r˜t        j(                  t        j(                  ||
«      |j*                  «      }ddlm} |j1                  |«      \  }}|j$                  d   |j$                  d   z
  }t        j(                  |dd…| d…f   j*                  |«      }n|}|j$                  d   }|d	k(  re| j3                  ||
¬
«      }|j4                  x|j6                  |j8                  |   |f<   }|j:                  |j6                  |j8                  |   |f<   ||j6                  |j8                  |   df<   |j=                  |j                  «       |j=                  |j?                  «       «       �ŒÝ tA        |dgz   «      |_        |jB                  t        jD                  || j                   j"                  j$                  d   dz   gz   «         }||   |d   z  | jF                  z  | jH                  z  }||d<   | jF                  | jH                  t        jJ                  t        jJ                  f|j6                  ddd||gf<   |S )a‰  
    Anova type II table for one fitted linear model.

    Parameters
    ----------
    model : fitted linear model results instance
        A fitted linear model

    **kwargs**

    scale : float
        Estimate of variance, If None, will be estimated from the largest
    model. Default is None.
        test : str {"F", "Chisq", "Cp"} or None
        Test statistics to provide. Default is "F".

    Notes
    -----
    Use of this function is discouraged. Use anova_lm instead.

    Type II
    Sum of Squares compares marginal contribution of terms. Thus, it is
    not particularly useful for models with significant interaction terms.
    Nr#   r"   r.   r&   r!   r   )rN   r   ©Úcov_prL   )&r:   r	   r   r;   r<   r   rU   rT   r   ÚstartÚstopÚsetÚfactorsÚissubsetÚextendÚeyer   r3   r4   ÚsizerP   rQ   ÚscipyrN   rO   Úf_testÚfvaluer[   rX   ÚpvalueÚappendrd   r   ÚilocÚargsortr\   r]   r`   )r   r7   rE   r   rF   r   Ú
terms_inforG   rH   ÚcovÚ
robust_covÚ	col_orderrX   rf   ÚtermÚcolsÚL1ÚL2Úterm_setÚtÚ	other_setÚcolÚLVLrN   Ú
orth_complÚ_rb   ÚL12r^   Ú
test_valuer\   s                                  r   r>   r>   –   sg  € ð2 ×"Ñ"¡1Ð%€JÜ(¨Ó4€Jà�t˜T 7Ð+€Eä”b—h‘h ¨˜{Ó+°uÔ=€EÜ
˜% Ó
&€CÜ  ¨Ó/€JØ€IØ€EÜ˜ZÓ(ó '"‰ˆˆ4ð × Ñ  Ó&ˆÜ�D—J‘J §	¡	Ó*ˆØˆÜ�t—|‘|Ó$ˆØò 	7ˆAÜ˜AŸI™I›ˆIØ× Ñ  Õ+°HÀ	Ó4IØ!×'Ñ'¨Ó*�à—	‘	œ& §¡¨C¯H©HÓ5Ô6Ø—	‘	œ& §¡¨C¯H©HÓ5Õ6ð	7ô �V‰V�E—K‘K×$Ñ$×*Ñ*¨1Ñ-Ó.¨rÑ2ˆÜ�V‰V�E—K‘K×$Ñ$×*Ñ*¨1Ñ-Ó.¨rÑ2ˆà�7Š7Ü—&‘&œŸ™  :Ó.¨r¯t©tÓ4ˆCÝ$Ø!Ÿ9™9 S›>‰LˆJ�qØ—‘˜‘˜bŸh™h q™kÑ)ˆAô —&‘&˜¢A q b¡c EÑ*×,Ñ,¨bÓ1‰CàˆCØ—‘˜‘ˆAà�3Š;Ø—‘˜S¨
�Ó3ˆAØ;<¿8¹8ÐCˆE�I‰I�e—k‘k !‘n dÐ*Ñ+¨jØ12·±ˆE�I‰I�e—k‘k !‘n gÐ-Ñ.ð +,ˆ�	‰	�%—+‘+˜a‘. $Ð&Ñ'Ø×Ñ˜Ÿ™Ô$Ø�‰�T—Y‘Y“[Ö!ðO'"ôR ˜  Ñ,Ó-€E„KØ�J‰J”r—z‘z )¨u¯{©{×/?Ñ/?×/EÑ/EÀaÑ/HÈÑ/JÐ.KÑ"KÓLÑM€Eà
�‰+˜˜d™Ñ
# e§i¡iÑ
/°·±Ñ
>€CØ€Eˆ(�Oà=B¿Y¹YØ<A¿N¹NÜ<>¿F¹FÄBÇFÁFð=L€E‡I�Iˆj˜8 D¨$°Ð8Ð8Ñ9ð €Lr   c                 óæ  — |t        |«      z  }|j                  }dd||g}t        t        j                  |df«      |¬«      }t        | |«      }	g }
g }t        |«      D �]  \  }}|j                  |«      }t        j                  | j                  j                  j                  d   «      |   }|}|j                  d   }|dk(  re| j                  ||	¬«      }|j                  x|j                  |j                  |   |f<   }|j                   |j                  |j                  |   |f<   ||j                  |j                  |   df<   |j#                  |j%                  «       «       �Œ t'        |d	gz   «      |_        ||   |d   z  | j(                  z  | j*                  z  }||d<   | j(                  | j*                  t        j,                  t        j,                  f|j                  d	dd||gf<   |S )
Nr#   r"   r.   r&   r!   r   r   rj   rL   )r   r:   r   r;   r<   r   rU   rT   rr   r   r3   r4   ru   rv   r[   rX   rw   rx   rd   r   r\   r]   r`   )r   r7   rE   r   rF   r   r{   rG   rH   r|   r~   rX   rf   r   r€   r�   rŠ   rb   r^   r‹   r\   s                        r   r?   r?   î   sË  € Ø
Œn˜[Ó)Ñ)€FØ×"Ñ"€Jà�t˜T 7Ð+€Eä”b—h‘h ¨˜{Ó+°uÔ=€EÜ
˜% Ó
(€CØ€IØ€EÜ˜ZÓ(ó "‰ˆˆ4à× Ñ  Ó&ˆÜ�V‰V�E—K‘K×$Ñ$×*Ñ*¨1Ñ-Ó.¨tÑ4ˆØˆØ�H‰H�Q‰Kˆà�3Š;Ø—‘˜S¨�Ó,ˆAØ;<¿8¹8ÐCˆE�I‰I�e—k‘k !‘n dÐ*Ñ+¨jØ12·±ˆE�I‰I�e—k‘k !‘n gÐ-Ñ.ð +,ˆ�	‰	�%—+‘+˜a‘. $Ð&Ñ'à�‰�T—Y‘Y“[Ö!ð"ô" ˜  Ñ,Ó-€E„Kð �‰+˜˜d™Ñ
# e§i¡iÑ
/°·±Ñ
>€CØ€Eˆ(�Oà=B¿Y¹YØ<A¿N¹NÜ<>¿F¹FÄBÇFÁFð=L€E‡I�Iˆj˜8 D¨$°Ð8Ð8Ñ9ð €Lr   c                  ó–  — |j                  dd«      }t        | «      dk(  r| d   }t        |fi |¤ŽS |dvrt        dt	        |«      z  «      ‚|j                  dd«      }|j                  dd	«      }t        | «      }d
|z  }dddd||g}t        t        j                  |df«      |¬«      }	|s| d   j                  }| D �
cg c]  }
|
j                  ‘Œ c}
|	d<   | D �
cg c]  }
|
j                  ‘Œ c}
|	d<   t        j                  |	d   j                  «       |	j                  |	j                  dd	 df<   |	d   j                  «        |	d<   |dk(  rn|	d   |	d   z  |z  |	d<   t        j                   j#                  |	d   |	d   |	d   «      |	|<   t        j$                  |	j                  |	d   j'                  «       |f<   |	S c c}
w c c}
w )aÁ	  
    Anova table for one or more fitted linear models.

    Parameters
    ----------
    args : fitted linear model results instance
        One or more fitted linear models
    scale : float
        Estimate of variance, If None, will be estimated from the largest
        model. Default is None.
    test : str {"F", "Chisq", "Cp"} or None
        Test statistics to provide. Default is "F".
    typ : str or int {"I","II","III"} or {1,2,3}
        The type of Anova test to perform. See notes.
    robust : {None, "hc0", "hc1", "hc2", "hc3"}
        Use heteroscedasticity-corrected coefficient covariance matrix.
        If robust covariance is desired, it is recommended to use `hc3`.

    Returns
    -------
    anova : DataFrame
        When args is a single model, return is DataFrame with columns:

        sum_sq : float64
            Sum of squares for model terms.
        df : float64
            Degrees of freedom for model terms.
        F : float64
            F statistic value for significance of adding model terms.
        PR(>F) : float64
            P-value for significance of adding model terms.

        When args is multiple models, return is DataFrame with columns:

        df_resid : float64
            Degrees of freedom of residuals in models.
        ssr : float64
            Sum of squares of residuals in models.
        df_diff : float64
            Degrees of freedom difference from previous model in args
        ss_dff : float64
            Difference in ssr from previous model in args
        F : float64
            F statistic comparing to previous model in args
        PR(>F): float64
            P-value for significance comparing to previous model in args

    Notes
    -----
    Model statistics are given in the order of args. Models must have been fit
    using the formula api.

    See Also
    --------
    model_results.compare_f_test, model_results.compare_lm_test

    Examples
    --------
    >>> import statsmodels.api as sm
    >>> from statsmodels.formula.api import ols
    >>> moore = sm.datasets.get_rdataset("Moore", "carData", cache=True) # load
    >>> data = moore.data
    >>> data = data.rename(columns={"partner.status" :
    ...                             "partner_status"}) # make name pythonic
    >>> moore_lm = ols('conformity ~ C(fcategory, Sum)*C(partner_status, Sum)',
    ...                 data=data).fit()
    >>> table = sm.stats.anova_lm(moore_lm, typ=2) # Type 2 Anova DataFrame
    >>> print(table)
    r    r!   r   r(   z6Multiple models only supported for type I. Got type %sr   r   r   NzPr(>%s)r]   r\   Údf_diffÚss_diffé   r&   éÿÿÿÿ)r0   r9   rI   r   rA   r   r;   r<   r   r\   r]   ÚdiffÚvaluesr[   rX   r   r^   r_   r`   Úisnull)ÚargsrB   r    r   r   r   Ún_modelsrF   rG   rH   Úmdls              r   Úanova_lmr˜     sÒ  € ðL �*‰*�U˜AÓ
€Cô ˆ4ƒy�A‚~Ø�Q‘ˆÜ˜EÑ, VÑ,Ð,à
�(ÑÜð 'Ü),¨S«ñ2ó 3ð 	3ð �:‰:�f˜cÓ"€DØ�J‰J�w Ó%€EÜ�4‹y€HØ˜$Ñ€GØ˜ 	¨9°d¸GÐD€EÜ”b—h‘h ¨!˜}Ó-°uÔ=€EáØ�R‘—‘ˆà'+Ö, �C—G“GÒ,€Eˆ%�LØ15Ö6¨#˜Ÿ›Ò6€Eˆ*ÑÜ-/¯W©W°U¸:Ñ5F×5MÑ5MÓ-NÐ,N€E‡I�Iˆe�k‰k˜!˜"ˆo˜yÐ(Ñ)Ø˜e™×)Ñ)Ó+Ð+€Eˆ)ÑØˆs‚{Ø˜9Ñ%¨¨iÑ(8Ñ8¸5Ñ@ˆˆc‰
ÜŸ™Ÿ™ E¨#¡J°°iÑ0@Ø$)¨*Ñ$5ó7ˆˆg‰ô 35·&±&ˆ�	‰	�%˜‘*×#Ñ#Ó% wÐ.Ñ/à€Lùò -ùÚ6s   Ã GÃGc                 óZ   — t        j                  dg|z  «      }|D ]  }| |   }d||<   Œ |S )NTF)r;   rV   )re   Úslices_to_excludeÚnÚindr   Úss         r   Ú
_not_slicerž     s>   € Ü
�(‰(�D�6˜!‘8Ó
€CØ!ò ˆØ�4‰LˆØˆˆAŠðð €Jr   c                 ó  — t        |||j                  d   «      }||   }t        j                  | |dd…|f   j	                  |«      «      }|j
                  j	                  |«      }t        | «      t        |«      z
  }||fS )ah  
    Residual sum of squares of OLS model excluding factors in `keys`
    Assumes x matrix is orthogonal

    Parameters
    ----------
    y : array_like
        dependent variable
    x : array_like
        independent variables
    term_slices : a dict of slices
        term_slices[key] is a boolean array specifies the parameters
        associated with the factor `key`
    params : ndarray
        OLS solution of y = x * params
    keys : keys for term_slices
        factors to be excluded

    Returns
    -------
    rss : float
        residual sum of squares
    df : int
        degrees of freedom
    r!   N)rž   r4   r;   ÚsubtractrP   rQ   r9   )	ÚyÚxÚterm_slicesÚparamsÚkeysrœ   Úparams1r\   r]   s	            r   Ú_ssr_reduced_modelr§   ‡  sq   € ô4 �[ $¨¯©°©
Ó
3€CØ�S‰k€GÜ
�+‰+�a˜š1˜c˜6™Ÿ™ wÓ/Ó
0€CØ
�%‰%�)‰)�C‹.€CÜ�1‹vœ˜G›Ñ$€HØ�ˆ=Ðr   c                   ó.   — e Zd ZdZ	 	 dd„Zd„ Zd„ Zd„ Zy)ÚAnovaRMaò  
    Repeated measures Anova using least squares regression

    The full model regression residual sum of squares is
    used to compare with the reduced model for calculating the
    within-subject effect sum of squares [1].

    Currently, only fully balanced within-subject designs are supported.
    Calculation of between-subject effects and corrections for violation of
    sphericity are not yet implemented.

    Parameters
    ----------
    data : DataFrame
    depvar : str
        The dependent variable in `data`
    subject : str
        Specify the subject id
    within : list[str]
        The within-subject factors
    between : list[str]
        The between-subject factors, this is not yet implemented
    aggregate_func : {None, 'mean', callable}
        If the data set contains more than a single observation per subject
        and cell of the specified model, this function will be used to
        aggregate the data before running the Anova. `None` (the default) will
        not perform any aggregation; 'mean' is s shortcut to `numpy.mean`.
        An exception will be raised if aggregation is required, but no
        aggregation function was specified.

    Returns
    -------
    results : AnovaResults instance

    Raises
    ------
    ValueError
        If the data need to be aggregated, but `aggregate_func` was not
        specified.

    Notes
    -----
    This implementation currently only supports fully balanced designs. If the
    data contain more than one observation per subject and cell of the design,
    these observations need to be aggregated into a single observation
    before the Anova is calculated, either manually or by passing an aggregation
    function via the `aggregate_func` keyword argument.
    Note that if the input data set was not balanced before performing the
    aggregation, the implied heteroscedasticity of the data is ignored.

    References
    ----------
    .. [*] Rutherford, Andrew. Anova and ANCOVA: a GLM approach. John Wiley & Sons, 2011.
    Nc                 ó˜  — || _         || _        || _        d|v rt        d«      ‚|| _        |�t        d«      ‚|| _        |dk(  r t        j                  j                  | _
        n|| _
        |j                  |j                  |g|z   ¬«      «      s*| j                  �| j                  «        nd}t        |«      ‚| j                  «        y )NÚCzSFactor name cannot be 'C'! This is in conflict with patsy's contrast function name.z)Between subject effect not yet supported!Úmean)Úsubsetz‘The data set contains more than one observation per subject and cell. Either aggregate the data manually, or pass the `aggregate_func` parameter.)r6   ÚdepvarÚwithinr   Úbetweenr@   ÚsubjectÚpdÚSeriesr¬   Úaggregate_funcÚequalsÚdrop_duplicatesÚ
_aggregateÚ_check_data_balanced)Úselfr6   r®   r±   r¯   r°   r´   Úmsgs           r   Ú__init__zAnovaRM.__init__á  sÐ   € àˆŒ	ØˆŒØˆŒØ�&‰=Üð Dó Eð EàˆŒØÐÜ%ð '7ó 8ð 8àˆŒà˜VÒ#Ü"$§)¡)§.¡.ˆDÕà"0ˆDÔà�{‰{˜4×/Ñ/¸°yÀ6Ñ7IÐ/ÓJÔKØ×"Ñ"Ð.Ø—‘Õ!ðA�ô ! “oÐ%à×!Ñ!Õ#r   c                 óÄ   — | j                   j                  | j                  g| j                  z   d¬«      | j                     j                  | j                  «      | _         y )NF)Úas_index)r6   Úgroupbyr±   r¯   r®   Úaggr´   ©r¹   s    r   r·   zAnovaRM._aggregateÿ  sQ   € Ø—Y‘Yß‘g˜tŸ|™|˜n¨t¯{©{Ñ:Ø',ð ó .Ø.2¯k©kñ;÷ ‘c˜$×-Ñ-Ó.ð 	�	r   c                 óR  — d}| j                   D ]+  }|t        | j                  |   j                  «       «      z  }Œ- i }t	        | j                  j
                  d   «      D ]`  }g }| j                   D ]-  }|j                  | j                  |   j                  |   «       Œ/ t        |«      }||v r||   dz   ||<   Œ\d||<   Œb d}t        |«      |k7  rt        |«      ‚|   }|D ]  }|||   k7  sŒt        |«      ‚ | j                  j
                  d   ||z  kD  rt        d«      ‚y)z¬raise if data is not balanced

        This raises a ValueError if the data is not balanced, and
        returns None if it is balance

        Return might change
        r!   r   zData is unbalanced.z9There are more than 1 element in a cell! Missing factors?N)
r¯   r9   r6   ÚuniqueÚranger4   rx   ry   Útupler   )	r¹   Úfactor_levelsÚwiÚ
cell_countrX   Úkeyr†   Úerror_messageÚcounts	            r   r¸   zAnovaRM._check_data_balanced  sG  € ð ˆØ—+‘+ò 	9ˆBØœS §¡¨2¡×!5Ñ!5Ó!7Ó8Ñ8‰Mð	9ð ˆ
Ü˜4Ÿ9™9Ÿ?™?¨1Ñ-Ó.ò 	$ˆEØˆCØ—{‘{ò 7�Ø—
‘
˜4Ÿ9™9 S™>×.Ñ.¨uÑ5Õ6ð7ä˜“*ˆCØ�jÑ Ø",¨S¡/°AÑ"5�
˜3’à"#�
˜3’ð	$ð .ˆÜˆz‹?˜mÒ+Ü˜]Ó+Ð+Ø˜3‘ˆØò 	0ˆCØ˜
 3™Ó'Ü  Ó/Ð/ð	0ð �9‰9�?‰?˜1Ñ ¨Ñ 5Ò5Üð )ó *ð *ð 6r   c           	      ó
  — | j                   | j                     j                  }| j                  D �cg c]  }d|z  ‘Œ	 }}d| j                  z  }||gz   }t        j                  dj                  |«      | j                   ¬«      }|j                  j                  }|D ]H  }t        j                  dg|j                  d   z  «      }	d|	||   <   t        j                  |	«      ||<   ŒJ dj                  |«      g}
t        ||
|j                  d   «      }	|dd…|	f   }t        ||«      }|j                  «       }|j                   |j                  d   k  rt#        d	«      ‚|
D ]  }|j%                  |«       Œ |D ]  }||   |	   ||<   Œ |j&                  }|j(                  }|j*                  }g d
¢}t-        j.                  t        j0                  d«      |¬«      }|D �]  }| j                  |vsŒ|dk7  sŒt3        |||||g«      \  }}||z
  }||z
  |z  }|dj                  |dd «      k(  s
|dz   |z   |vr||z  }|}n&t3        |||||dz   |z   g«      \  }}||z
  }||z
  |z  }||z  }t4        j6                  j9                  |||«      }|j;                  dd«      j;                  dd«      }||j<                  |df<   ||j<                  |df<   ||j<                  |df<   ||j<                  |df<   �Œ t?        |«      S c c}w )zvestimate the model and compute the Anova table

        Returns
        -------
        AnovaResults instance
        z
C(%s, Sum)Ú*©r6   Fr!   Tú:Nz$Independent variables are collinear.)úF ValueúNum DFúDen DFúPr > F)r   r.   r&   Ú	Interceptr‘   zC(Ú z, Sum)rÏ   rÐ   rÑ   rÒ   ) r6   r®   r“   r¯   r±   ÚpatsyÚdmatrixÚjoinr7   Úterm_name_slicesr;   rV   r4   rž   r   ÚfitÚrankr   Úpopr¤   r]   r\   r²   r   r<   r§   r   r^   r_   Úreplacer[   ÚAnovaResults)r¹   r¡   rf   r¯   r±   ro   r¢   r£   rÈ   rœ   Úterm_excluder   Úresultsr¤   r]   r\   r'   Úanova_tableÚssr1Ú	df_resid1Údf1ÚmsmÚmseÚdf2r   Úpr   s                              r   rÙ   zAnovaRM.fit&  s  € ð �I‰I�d—k‘kÑ"×)Ñ)ˆð -1¯K©KÖ8 q�, Ó"Ð8ˆÐ8Ø §¡Ñ-ˆØ˜G˜9Ñ$ˆÜ�M‰M˜#Ÿ(™( 7Ó+°$·)±)Ô<ˆØ—m‘m×4Ñ4ˆØò 	-ˆCÜ—(‘(˜E˜7 1§7¡7¨1¡:Ñ-Ó.ˆCØ$(ˆC�˜CÑ Ñ!Ü!Ÿx™x¨›}ˆK˜Òð	-ð Ÿ™ Ó)Ð*ˆÜ˜ l°A·G±G¸A±JÓ?ˆØŠa�ˆf‰Iˆô �A�q“	ˆØ—)‘)“+ˆØ�:‰:˜Ÿ™ ™
Ò"ÜÐCÓDÐDØò 	ˆAØ�O‰O˜AÕð	àò 	5ˆCØ*¨3Ñ/°Ñ4ˆK˜Òð	5à—‘ˆØ×#Ñ#ˆØ�k‰kˆâ;ˆÜ—l‘l¤2§8¡8¨FÓ#3¸WÔEˆàó 	4ˆCØ�|‰| 3Ò&¨3°+Ó+=ä"4Ø�q˜+ v°¨uó#6‘��ià (Ñ*�Ø˜c‘z SÑ(�Ø˜3Ÿ8™8 G¨C¨R LÓ1Ò1Ø˜s™ WÑ,°KÑ?Ø ™.�CØ"‘Cä&8Ø˜1˜k¨6Ø˜s™ WÑ,Ð-ó'/‘O�D˜)ð $ hÑ.�CØ #™:¨Ñ,�CØ˜#‘I�Ü—G‘G—J‘J˜q # sÓ+�Ø—{‘{ 4¨Ó,×4Ñ4°X¸rÓB�Ø34�—‘  i Ñ0Ø25�—‘  h Ñ/Ø25�—‘  h Ñ/Ø23�—‘  h Ó/ð/	4ô2 ˜KÓ(Ð(ùòm 9s   ²L )NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r»   r·   r¸   rÙ   © r   r   r©   r©   ©  s(   „ ñ5ðn DHØ $ó$ò<0ò*óB@)r   r©   c                   ó"   — e Zd ZdZd„ Zd„ Zd„ Zy)rÝ   zX
    Anova results class

    Attributes
    ----------
    anova_table : DataFrame
    c                 ó   — || _         y ©N)rà   )r¹   rà   s     r   r»   zAnovaResults.__init__q  s
   € Ø&ˆÕr   c                 ó>   — | j                  «       j                  «       S rï   )ÚsummaryÚ__str__rÀ   s    r   rò   zAnovaResults.__str__t  s   € Ø�|‰|‹~×%Ñ%Ó'Ð'r   c                 ó†   — t        j                  «       }|j                  d«       |j                  | j                  «       |S )zlcreate summary results

        Returns
        -------
        summary : summary2.Summary instance
        ÚAnova)r
   ÚSummaryÚ	add_titleÚadd_dfrà   )r¹   Úsumms     r   rñ   zAnovaResults.summaryw  s5   € ô ×ÑÓ!ˆØ�‰�wÔØ�‰�D×$Ñ$Ô%àˆr   N)rè   ré   rê   rë   r»   rò   rñ   rì   r   r   rÝ   rÝ   i  s   „ ñò'ò(ór   rÝ   Ú__main__)Úolsz	moore.csvr!   )Úpartner_statusÚ
conformityÚ	fcategoryÚfscore)ÚskiprowsrG   z5conformity ~ C(fcategory, Sum)*C(partner_status, Sum)rÍ   z#conformity ~ C(partner_status, Sum)r*   )r    )&Ústatsmodels.compat.pythonr   Únumpyr;   Úpandasr²   r   r   rÕ   rt   r   Ú statsmodels.formula.formulatoolsr   r   r	   Ústatsmodels.iolibr
   Ú#statsmodels.regression.linear_modelr   r   rI   r=   r>   r?   r˜   rž   r§   r©   rÝ   rè   Ústatsmodels.formula.apirú   Úread_csvÚmoorerÙ   Úmoore_lmÚmooreBrH   rì   r   r   ú<module>r     sã   ðÝ ,ã Û ß #Û Ý ÷ñ õ
 'Ý 3òFò"9>òx4ònVòp%òNgòTò÷D})ñ })÷@ñ ð8 ˆzÒÛå+ð
 ˆF�O‰O˜K°!ò#9ô:€Eñ ÐJØô ß #¡£ð ñ Ð6¸UÔC×GÑGÓI€Fñ �X 1Ô%�Eð/ r   