
    M/Ph=6                     r    d Z ddlmZ ddlZddlmZ ddlmZ ddlZ	ddl
Z
efdZddddefd	ZddZddZdS )a  Analyze a set of multiple variables with a linear models

multiOLS:
    take a model and test it on a series of variables defined over a
    pandas dataset, returning a summary for each variable

multigroup:
    take a boolean vector and the definition of several groups of variables
    and test if the group has a fraction of true values higher than the
    rest. It allows to test if the variables in the group are significantly
    more significant than outside the group.
    )dmatrixN)OLS)statsc                     || |fi |                                 }t          j        |j        |j        d          }t          j        |j        |j        |j        |d          }t          j        d|j	        id|j
        id          }t          j        ||g                                          }|                                S )zreturn a series containing the summary of a linear model

    All the exceding parameters will be redirected to the linear model
    )r2adj_r2)paramspvalsstd
statistics_f_test)r	   r
   )fitpdSeriesrsquaredrsquared_adj	DataFramer	   pvaluesbsefvaluef_pvalueconcatunstackdropna)	model_endog
model_exog
model_typekwargsmodel_resultr   	result_df	fisher_df
res_seriess	            _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/multilinear.py_model2dataframer$      s     :k:@@@@DDFFL,"7(57 7 8 8J (;'3';%1%5,68 8 9 9I
 L4G(H(1<3H'IK K L LI Iy122::<<J    fdr_bh皙?c                 6    | fdj         D             }t          |t                    r|g}|j        |         i }t	           d          }	|D ]D}
	 |
         }n$# t
          $ r t	          |
dz             }Y nw xY wt          ||	|fi |}|||
<   Et          j        |          }|j	        
                    dg          }d|j        _        t          j        }|D ]4\  }}|d	k    r|||f         } ||||
          d         }||d|z   |f<   5|S )a  apply a linear model to several endogenous variables on a dataframe

    Take a linear model definition via formula and a dataframe that will be
    the environment of the model, and apply the linear model to a subset
    (or all) of the columns of the dataframe. It will return a dataframe
    with part of the information from the linear model summary.

    Parameters
    ----------
    model : str
        formula description of the model
    dataframe : pandas.dataframe
        dataframe where the model will be evaluated
    column_list : list[str], optional
        Names of the columns to analyze with the model.
        If None (Default) it will perform the function on all the
        eligible columns (numerical type and not in the model definition)
    model_type : model class, optional
        The type of model to be used. The default is the linear model.
        Can be any linear model (OLS, WLS, GLS, etc..)
    method : str, optional
        the method used to perform the pvalue correction for multiple testing.
        default is the Benjamini/Hochberg, other available methods are:

            `bonferroni` : one-step correction
            `sidak` : on-step correction
            `holm-sidak` :
            `holm` :
            `simes-hochberg` :
            `hommel` :
            `fdr_bh` : Benjamini/Hochberg
            `fdr_by` : Benjamini/Yekutieli

    alpha : float, optional
        the significance level used for the pvalue correction (default 0.05)
    subset : bool array
        the selected rows to be used in the regression

    all the other parameters will be directed to the model creation.

    Returns
    -------
    summary : pandas.DataFrame
        a dataframe containing an extract from the summary of the model
        obtained for each columns. It will give the model complexive f test
        result and p-value, and the regression value and standard deviarion
        for each of the regressors. The DataFrame has a hierachical column
        structure, divided as:

            - params: contains the parameters resulting from the models. Has
            an additional column named _f_test containing the result of the
            F test.
            - pval: the pvalue results of the models. Has the _f_test column
            for the significativity of the whole test.
            - adj_pval: the corrected pvalues via the multitest function.
            - std: uncertainties of the model parameters
            - statistics: contains the r squared statistics and the adjusted
            r squared.

    Notes
    -----
    The main application of this function is on system biology to perform
    a linear model testing of a lot of different parameters, like the
    different genetic expression of several genes.

    See Also
    --------
    statsmodels.stats.multitest
        contains several functions to perform the multiple p-value correction

    Examples
    --------
    Using the longley data as dataframe example

    >>> import statsmodels.api as sm
    >>> data = sm.datasets.longley.load_pandas()
    >>> df = data.exog
    >>> df['TOTEMP'] = data.endog

    This will perform the specified linear model on all the
    other columns of the dataframe
    >>> multiOLS('GNP + 1', df)

    This select only a certain subset of the columns
    >>> multiOLS('GNP + 0', df, ['GNPDEFL', 'TOTEMP', 'POP'])

    It is possible to specify a trasformation also on the target column,
    conforming to the patsy formula specification
    >>> multiOLS('GNP + 0', df, ['I(GNPDEFL**2)', 'center(TOTEMP)'])

    It is possible to specify the subset of the dataframe
    on which perform the analysis
    >> multiOLS('GNP + 1', df, subset=df.GNPDEFL > 90)

    Even a single column name can be given without enclosing it in a list
    >>> multiOLS('GNP + 0', df, 'GNPDEFL')
    Nc                 H    g | ]}|         j         t          k    |v|S  )dtypeobject).0name	dataframemodels     r#   
<listcomp>zmultiOLS.<locals>.<listcomp>   sA     P P P"4.&88T=N=N =N=N=Nr%   r/   )datareturn_typez + 0)r2   )r
   r   zendogenous varsr
   methodalpha   adj_)columns
isinstancestrlocr   KeyErrorr$   r   r   Tsort_valuesindexr.   r   multipletests)r0   r/   column_listr5   r6   subsetr   r   col_resultsr   col_namer   ressummarysmtkey1key2p_values	correcteds   ``                 r#   multiOLSrM   +   s   L P P P P P	(9 P P P +s## $"mM&)	K YKHHHJ 
$ 
$	E#H-KK 	E 	E 	E!(V"3)DDDKKK	E {J
MMfMM #Hl;''Gi##%9$:;;G*GM

C 1 1t7??4:&Cu===a@	 (1t#$$Ns   A""BBTc                 >    ddl m}m} dt                     z  }dt	          j                   z  } fd|D             } fd|D             }	|	r*d}
t          j        |
                    ||	                     dt          |          z  }dt           fd|D                       z  }d||z
  z  }d||z
  z  }d||z
  |z
  z  }|r|n|}||g||gg} |t	          j	        |                    d         }||||f}t	          j
        ||z  ||z  z            }|||fS )	ztest if the objects in the group are different from the general set.

    The test is performed on the pvalues set (ad a pandas series) over
    the group specified via a fisher exact test.
    r   )fisher_exactchi2_contingencyg      ?c                 &    g | ]}|j         v |S r*   r@   r-   cr   s     r#   r1   z_test_group.<locals>.<listcomp>   s%    :::qGM'9'91'9'9'9r%   c                 &    g | ]}|j         v|S r*   rR   rS   s     r#   r1   z_test_group.<locals>.<listcomp>   s%    :::Q1GM#9#9q#9#9#9r%   zthe test is not well defined if the group has elements not presents in the significativity array. group name: {}, missing elements: {}c                 $    g | ]}|         
|S r*   r*   rS   s     r#   r1   z_test_group.<locals>.<listcomp>   s"    AAA!gajAAAAAr%   r7   )scipy.statsrO   rP   lennpsumloggingwarningformatarraylog)r   
group_namegroupexactrO   rP   totalstotal_significantcross_indexmissingsgroup_total
group_signgroup_nonsignextern_signextern_nonsigntesttablepvaluepartincreases   `                    r#   _test_grouprr      s    ;:::::::3w<<FbfWoo-::::e:::K::::%:::G 7; 	W55666K(((KsAAAA{AAABBBJ;34M*Z78KF%66FGN 6<<&6Dk*]J,GHET"(5//""1%F}k>ADvv
**[8: ; ;H8T!!r%   c                 "   t          j        |           } t          |                                           ddhk    st	          d          t          | j        d          r| j        j        st	          d          i i i i i i d}|                                D ]\  }}t          | |||          }|d         |d         |<   |d	         |d
         |<   |d         d         |d         |<   |d         d	         |d         |<   |d         d         |d         |<   |d         d         |d         |<   t          j
        |                              d          }	|s|	|	j                 }	t          j        }
 |
|	d         d|          d	         }||	d<   |	S )aC  Test if the given groups are different from the total partition.

    Given a boolean array test if each group has a proportion of positives
    different than the complexive proportion.
    The test can be done as an exact Fisher test or approximated as a
    Chi squared test for more speed.

    Parameters
    ----------
    pvals : pandas series of boolean
        the significativity of the variables under analysis
    groups : dict of list
        the name of each category of variables under exam.
        each one is a list of the variables included
    exact : bool, optional
        If True (default) use the fisher exact test, otherwise
        use the chi squared test for contingencies tables.
        For high number of elements in the array the fisher test can
        be significantly slower than the chi squared.
    keep_all : bool, optional
        if False it will drop those groups where the fraction
        of positive is below the expected result. If True (default)
         it will keep all the significant results.
    alpha : float, optional
        the significativity level for the pvalue correction
        on the whole set of groups (not inside the groups themselves).

    Returns
    -------
    result_df: pandas dataframe
        for each group returns:

            pvals - the fisher p value of the test
            adj_pvals - the adjusted pvals
            increase - the log of the odd ratio between the
                internal significant ratio versus the external one
            _in_sign - significative elements inside the group
            _in_non - non significative elements inside the group
            _out_sign - significative elements outside the group
            _out_non - non significative elements outside the group

    Notes
    -----
    This test allow to see if a category of variables is generally better
    suited to be described for the model. For example to see if a predictor
    gives more information on demographic or economical parameters,
    by creating two groups containing the endogenous variables of each
    category.

    This function is conceived for medical dataset with a lot of variables
    that can be easily grouped into functional groups. This is because
    The significativity of a group require a rather large number of
    composing elements.

    Examples
    --------
    A toy example on a real dataset, the Guerry dataset from R
    >>> url = "https://raw.githubusercontent.com/vincentarelbundock/"
    >>> url = url + "Rdatasets/csv/HistData/Guerry.csv"
    >>> df = pd.read_csv(url, index_col='dept')

    evaluate the relationship between the various paramenters whith the Wealth
    >>> pvals = multiOLS('Wealth', df)['adj_pvals', '_f_test']

    define the groups
    >>> groups = {}
    >>> groups['crime'] = ['Crime_prop', 'Infanticide',
    ...     'Crime_parents', 'Desertion', 'Crime_pers']
    >>> groups['religion'] = ['Donation_clergy', 'Clergy', 'Donations']
    >>> groups['wealth'] = ['Commerce', 'Lottery', 'Instruction', 'Literacy']

    do the analysis of the significativity
    >>> multigroup(pvals < 0.05, groups)
    FTzthe series should be binary	is_uniquez,series with duplicated index is not accepted)r
   rq   _in_sign_in_non	_out_sign_out_nonr   r
   r7   rq      ru   rv   rw      rx   r&   r4   	adj_pvals)r   r   setunique
ValueErrorhasattrr@   rt   itemsrr   r   r?   rq   r   rA   )r
   groupsrb   keep_allr6   resultsr`   
group_listrF   r    rH   rL   s               r#   
multigroupr      s   V IeEE4=006777u{K(( I1F IGHHH G #),,.. 4 4
J%Z??'*1v$*-a&
J'*-a&)
J'),Q	:&+.q6!9Z(*-a&)
J''W%%11'::I 2i01	

CIg&xuEEEaHI&Ikr%   )T)TTr'   )__doc__patsyr   pandasr   statsmodels.apir   r   numpyrY   r[   r$   rM   rr   r   r*   r%   r#   <module>r      s                     ! ! ! ! ! !     9<    . ,0M M M M`!" !" !" !"Hd d d d d dr%   