<s> import warnings <EOL> import numpy as np <EOL> import pandas as pd <EOL> from statsmodels . regression . mixed_linear_model import MixedLM , MixedLMParams <EOL> from numpy . testing import ( assert_almost_equal , assert_equal , assert_allclose , <EOL> dec , assert_ ) <EOL> from . import lme_r_results <EOL> from statsmodels . base import _penalties as penalties <EOL> import statsmodels . tools . numdiff as nd <EOL> import os <EOL> import csv <EOL> class R_Results ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , meth , irfs , ds_ix ) : <EOL> bname = "<STR_LIT>" % ( meth , irfs , ds_ix ) <EOL> self . coef = getattr ( lme_r_results , "<STR_LIT>" + bname ) <EOL> self . vcov_r = getattr ( lme_r_results , "<STR_LIT>" + bname ) <EOL> self . cov_re_r = getattr ( lme_r_results , "<STR_LIT>" + bname ) <EOL> self . scale_r = getattr ( lme_r_results , "<STR_LIT>" + bname ) <EOL> self . loglike = getattr ( lme_r_results , "<STR_LIT>" + bname ) <EOL> if hasattr ( lme_r_results , "<STR_LIT>" + bname ) : <EOL> self . ranef_postmean = getattr ( lme_r_results , "<STR_LIT>" <EOL> + bname ) <EOL> self . ranef_condvar = getattr ( lme_r_results , <EOL> "<STR_LIT>" + bname ) <EOL> self . ranef_condvar = np . atleast_2d ( self . ranef_condvar ) <EOL> cur_dir = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> rdir = os . path . join ( cur_dir , '<STR_LIT>' ) <EOL> fname = os . path . join ( rdir , "<STR_LIT>" % ds_ix ) <EOL> fid = open ( fname ) <EOL> rdr = csv . reader ( fid ) <EOL> header = next ( rdr ) <EOL> data = [ [ float ( x ) for x in line ] for line in rdr ] <EOL> data = np . asarray ( data ) <EOL> self . endog = data [ : , header . index ( "<STR_LIT>" ) ] <EOL> self . groups = data [ : , header . index ( "<STR_LIT>" ) ] <EOL> ii = [ i for i , x in enumerate ( header ) if <EOL> x . startswith ( "<STR_LIT>" ) ] <EOL> self . exog_fe = data [ : , ii ] <EOL> ii = [ i for i , x in enumerate ( header ) if <EOL> x . startswith ( "<STR_LIT>" ) ] <EOL> self . exog_re = data [ : , ii ] <EOL> def loglike_function ( model , profile_fe , has_fe ) : <EOL> def f ( x ) : <EOL> params = MixedLMParams . from_packed ( <EOL> x , model . k_fe , model . k_re , model . use_sqrt , has_fe = has_fe ) <EOL> return - model . loglike ( params , profile_fe = profile_fe ) <EOL> return f <EOL> class TestMixedLM ( object ) : <EOL> @ dec . slow <EOL> def test_compare_numdiff ( self ) : <EOL> n_grp = <NUM_LIT:200> <EOL> grpsize = <NUM_LIT:5> <EOL> k_fe = <NUM_LIT:3> <EOL> k_re = <NUM_LIT:2> <EOL> for use_sqrt in False , True : <EOL> for reml in False , True : <EOL> for profile_fe in False , True : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> exog_fe = np . random . normal ( size = ( n_grp * grpsize , k_fe ) ) <EOL> exog_re = np . random . normal ( size = ( n_grp * grpsize , k_re ) ) <EOL> exog_re [ : , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> exog_vc = np . random . normal ( size = ( n_grp * grpsize , <NUM_LIT:3> ) ) <EOL> slopes = np . random . normal ( size = ( n_grp , k_re ) ) <EOL> slopes [ : , - <NUM_LIT:1> ] *= <NUM_LIT:2> <EOL> slopes = np . kron ( slopes , np . ones ( ( grpsize , <NUM_LIT:1> ) ) ) <EOL> slopes_vc = np . random . normal ( size = ( n_grp , <NUM_LIT:3> ) ) <EOL> slopes_vc = np . kron ( slopes_vc , np . ones ( ( grpsize , <NUM_LIT:1> ) ) ) <EOL> slopes_vc [ : , - <NUM_LIT:1> ] *= <NUM_LIT:2> <EOL> re_values = ( slopes * exog_re ) . sum ( <NUM_LIT:1> ) <EOL> vc_values = ( slopes_vc * exog_vc ) . sum ( <NUM_LIT:1> ) <EOL> err = np . random . normal ( size = n_grp * grpsize ) <EOL> endog = exog_fe . sum ( <NUM_LIT:1> ) + re_values + vc_values + err <EOL> groups = np . kron ( range ( n_grp ) , np . ones ( grpsize ) ) <EOL> vc = { "<STR_LIT:a>" : { } , "<STR_LIT:b>" : { } } <EOL> for i in range ( n_grp ) : <EOL> ix = np . flatnonzero ( groups == i ) <EOL> vc [ "<STR_LIT:a>" ] [ i ] = exog_vc [ ix , <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> vc [ "<STR_LIT:b>" ] [ i ] = exog_vc [ ix , <NUM_LIT:2> : <NUM_LIT:3> ] <EOL> model = MixedLM ( endog , exog_fe , groups , <EOL> exog_re , exog_vc = vc , use_sqrt = use_sqrt ) <EOL> rslt = model . fit ( reml = reml ) <EOL> loglike = loglike_function ( <EOL> model , profile_fe = profile_fe , has_fe = not profile_fe ) <EOL> for kr in range ( <NUM_LIT:5> ) : <EOL> fe_params = np . random . normal ( size = k_fe ) <EOL> cov_re = np . random . normal ( size = ( k_re , k_re ) ) <EOL> cov_re = np . dot ( cov_re . T , cov_re ) <EOL> vcomp = np . random . normal ( size = <NUM_LIT:2> ) ** <NUM_LIT:2> <EOL> params = MixedLMParams . from_components ( <EOL> fe_params , cov_re = cov_re , vcomp = vcomp ) <EOL> params_vec = params . get_packed ( <EOL> has_fe = not profile_fe , use_sqrt = use_sqrt ) <EOL> gr = - model . score ( params , profile_fe = profile_fe ) <EOL> ngr = nd . approx_fprime ( params_vec , loglike ) <EOL> assert_allclose ( gr , ngr , rtol = <NUM_LIT> ) <EOL> if ( profile_fe is False ) and ( use_sqrt is False ) : <EOL> hess = - model . hessian ( rslt . params_object ) <EOL> params_vec = rslt . params_object . get_packed ( <EOL> use_sqrt = False , has_fe = True ) <EOL> loglike_h = loglike_function ( <EOL> model , profile_fe = False , has_fe = True ) <EOL> nhess = nd . approx_hess ( params_vec , loglike_h ) <EOL> assert_allclose ( hess , nhess , rtol = <NUM_LIT> ) <EOL> def test_default_re ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> exog = np . random . normal ( size = ( <NUM_LIT> , <NUM_LIT:4> ) ) <EOL> groups = np . kron ( np . arange ( <NUM_LIT:100> ) , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> g_errors = np . kron ( np . random . normal ( size = <NUM_LIT:100> ) , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + g_errors + np . random . normal ( size = <NUM_LIT> ) <EOL> mdf1 = MixedLM ( endog , exog , groups ) . fit ( ) <EOL> mdf2 = MixedLM ( endog , exog , groups , np . ones ( <NUM_LIT> ) ) . fit ( ) <EOL> assert_almost_equal ( mdf1 . params , mdf2 . params , decimal = <NUM_LIT:8> ) <EOL> def test_history ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> exog = np . random . normal ( size = ( <NUM_LIT> , <NUM_LIT:4> ) ) <EOL> groups = np . kron ( np . arange ( <NUM_LIT:100> ) , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> g_errors = np . kron ( np . random . normal ( size = <NUM_LIT:100> ) , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + g_errors + np . random . normal ( size = <NUM_LIT> ) <EOL> mod = MixedLM ( endog , exog , groups ) <EOL> rslt = mod . fit ( full_output = True ) <EOL> assert_equal ( hasattr ( rslt , "<STR_LIT>" ) , True ) <EOL> def test_profile_inference ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> k_fe = <NUM_LIT:2> <EOL> gsize = <NUM_LIT:3> <EOL> n_grp = <NUM_LIT:100> <EOL> exog = np . random . normal ( size = ( n_grp * gsize , k_fe ) ) <EOL> exog_re = np . ones ( ( n_grp * gsize , <NUM_LIT:1> ) ) <EOL> groups = np . kron ( np . arange ( n_grp ) , np . ones ( gsize ) ) <EOL> vca = np . random . normal ( size = n_grp * gsize ) <EOL> vcb = np . random . normal ( size = n_grp * gsize ) <EOL> errors = <NUM_LIT:0> <EOL> g_errors = np . kron ( np . random . normal ( size = <NUM_LIT:100> ) , np . ones ( gsize ) ) <EOL> errors += g_errors + exog_re [ : , <NUM_LIT:0> ] <EOL> rc = np . random . normal ( size = n_grp ) <EOL> errors += np . kron ( rc , np . ones ( gsize ) ) * vca <EOL> rc = np . random . normal ( size = n_grp ) <EOL> errors += np . kron ( rc , np . ones ( gsize ) ) * vcb <EOL> errors += np . random . normal ( size = n_grp * gsize ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + errors <EOL> vc = { "<STR_LIT:a>" : { } , "<STR_LIT:b>" : { } } <EOL> for k in range ( n_grp ) : <EOL> ii = np . flatnonzero ( groups == k ) <EOL> vc [ "<STR_LIT:a>" ] [ k ] = vca [ ii ] [ : , None ] <EOL> vc [ "<STR_LIT:b>" ] [ k ] = vcb [ ii ] [ : , None ] <EOL> rslt = MixedLM ( endog , exog , groups = groups , <EOL> exog_re = exog_re , exog_vc = vc ) . fit ( ) <EOL> rslt . profile_re ( <NUM_LIT:0> , vtype = '<STR_LIT>' , dist_low = <NUM_LIT:1> , num_low = <NUM_LIT:3> , dist_high = <NUM_LIT:1> , <EOL> num_high = <NUM_LIT:3> ) <EOL> rslt . profile_re ( '<STR_LIT:b>' , vtype = '<STR_LIT>' , dist_low = <NUM_LIT:0.5> , num_low = <NUM_LIT:3> , <EOL> dist_high = <NUM_LIT:0.5> , num_high = <NUM_LIT:3> ) <EOL> def test_vcomp_1 ( self ) : <EOL> import scipy <EOL> v = scipy . __version__ . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> ] <EOL> v = int ( v ) <EOL> if v < <NUM_LIT:16> : <EOL> return <EOL> np . random . seed ( <NUM_LIT> ) <EOL> exog = np . random . normal ( size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> exog_re = np . random . normal ( size = ( <NUM_LIT> , <NUM_LIT:2> ) ) <EOL> groups = np . kron ( np . arange ( <NUM_LIT:100> ) , np . ones ( <NUM_LIT:4> ) ) <EOL> slopes = np . random . normal ( size = ( <NUM_LIT:100> , <NUM_LIT:2> ) ) <EOL> slopes [ : , <NUM_LIT:1> ] *= <NUM_LIT:2> <EOL> slopes = np . kron ( slopes , np . ones ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) ) * exog_re <EOL> errors = slopes . sum ( <NUM_LIT:1> ) + np . random . normal ( size = <NUM_LIT> ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + errors <EOL> free = MixedLMParams ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> free . fe_params = np . ones ( <NUM_LIT:1> ) <EOL> free . cov_re = np . eye ( <NUM_LIT:2> ) <EOL> free . vcomp = np . zeros ( <NUM_LIT:0> ) <EOL> model1 = MixedLM ( endog , exog , groups , exog_re = exog_re ) <EOL> result1 = model1 . fit ( free = free ) <EOL> exog_vc = { "<STR_LIT:a>" : { } , "<STR_LIT:b>" : { } } <EOL> for k , group in enumerate ( model1 . group_labels ) : <EOL> ix = model1 . row_indices [ group ] <EOL> exog_vc [ "<STR_LIT:a>" ] [ group ] = exog_re [ ix , <NUM_LIT:0> : <NUM_LIT:1> ] <EOL> exog_vc [ "<STR_LIT:b>" ] [ group ] = exog_re [ ix , <NUM_LIT:1> : <NUM_LIT:2> ] <EOL> model2 = MixedLM ( endog , exog , groups , exog_vc = exog_vc ) <EOL> result2 = model2 . fit ( ) <EOL> result2 . summary ( ) <EOL> assert_allclose ( result1 . fe_params , result2 . fe_params , atol = <NUM_LIT> ) <EOL> assert_allclose ( np . diag ( result1 . cov_re ) , <EOL> result2 . vcomp , atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result1 . bse [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] ] , <EOL> result2 . bse , atol = <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> def test_vcomp_2 ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> n = <NUM_LIT> <EOL> exog = np . random . normal ( size = ( n , <NUM_LIT:2> ) ) <EOL> groups = np . kron ( np . arange ( n / <NUM_LIT:16> ) , np . ones ( <NUM_LIT:16> ) ) <EOL> errors = <NUM_LIT:0> <EOL> exog_re = np . random . normal ( size = ( n , <NUM_LIT:2> ) ) <EOL> slopes = np . random . normal ( size = ( n / <NUM_LIT:16> , <NUM_LIT:2> ) ) <EOL> slopes = np . kron ( slopes , np . ones ( ( <NUM_LIT:16> , <NUM_LIT:1> ) ) ) * exog_re <EOL> errors += slopes . sum ( <NUM_LIT:1> ) <EOL> subgroups1 = np . kron ( np . arange ( n / <NUM_LIT:4> ) , np . ones ( <NUM_LIT:4> ) ) <EOL> errors += np . kron ( <NUM_LIT:2> * np . random . normal ( size = n / <NUM_LIT:4> ) , np . ones ( <NUM_LIT:4> ) ) <EOL> subgroups2 = np . kron ( np . arange ( n / <NUM_LIT:2> ) , np . ones ( <NUM_LIT:2> ) ) <EOL> errors += np . kron ( <NUM_LIT:2> * np . random . normal ( size = n / <NUM_LIT:2> ) , np . ones ( <NUM_LIT:2> ) ) <EOL> errors += np . random . normal ( size = n ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + errors <EOL> df = pd . DataFrame ( index = range ( n ) ) <EOL> df [ "<STR_LIT:y>" ] = endog <EOL> df [ "<STR_LIT>" ] = groups <EOL> df [ "<STR_LIT>" ] = exog [ : , <NUM_LIT:0> ] <EOL> df [ "<STR_LIT>" ] = exog [ : , <NUM_LIT:1> ] <EOL> df [ "<STR_LIT>" ] = exog_re [ : , <NUM_LIT:0> ] <EOL> df [ "<STR_LIT>" ] = exog_re [ : , <NUM_LIT:1> ] <EOL> df [ "<STR_LIT>" ] = subgroups1 <EOL> df [ "<STR_LIT>" ] = subgroups2 <EOL> vcf = { "<STR_LIT:a>" : "<STR_LIT>" , "<STR_LIT:b>" : "<STR_LIT>" } <EOL> model1 = MixedLM . from_formula ( "<STR_LIT>" , groups = groups , <EOL> re_formula = "<STR_LIT>" , <EOL> vc_formula = vcf , data = df ) <EOL> result1 = model1 . fit ( ) <EOL> assert_allclose ( result1 . fe_params , [ <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result1 . cov_re , [ <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result1 . vcomp , [ <NUM_LIT> , <NUM_LIT> ] , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result1 . bse . iloc [ <NUM_LIT:0> : <NUM_LIT:3> ] , [ <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , rtol = <NUM_LIT> ) <EOL> def test_vcomp_3 ( self ) : <EOL> import scipy <EOL> v = scipy . __version__ . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> ] <EOL> v = int ( v ) <EOL> if v < <NUM_LIT:16> : <EOL> return <EOL> np . random . seed ( <NUM_LIT> ) <EOL> x1 = np . random . normal ( size = <NUM_LIT> ) <EOL> groups = np . kron ( np . arange ( <NUM_LIT:100> ) , np . ones ( <NUM_LIT:4> ) ) <EOL> slopes = np . random . normal ( size = <NUM_LIT:100> ) <EOL> slopes = np . kron ( slopes , np . ones ( <NUM_LIT:4> ) ) * x1 <EOL> y = slopes + np . random . normal ( size = <NUM_LIT> ) <EOL> vc_fml = { "<STR_LIT:a>" : "<STR_LIT>" } <EOL> df = pd . DataFrame ( { "<STR_LIT:y>" : y , "<STR_LIT>" : x1 , "<STR_LIT>" : groups } ) <EOL> model = MixedLM . from_formula ( "<STR_LIT>" , groups = "<STR_LIT>" , <EOL> vc_formula = vc_fml , <EOL> data = df ) <EOL> result = model . fit ( ) <EOL> result . summary ( ) <EOL> assert_allclose ( result . resid . iloc [ <NUM_LIT:0> : <NUM_LIT:4> ] , <EOL> np . r_ [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] , <EOL> rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . fittedvalues . iloc [ <NUM_LIT:0> : <NUM_LIT:4> ] , <EOL> np . r_ [ - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> ] , <EOL> rtol = <NUM_LIT> ) <EOL> def test_sparse ( self ) : <EOL> import scipy <EOL> v = scipy . __version__ . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> ] <EOL> v = int ( v ) <EOL> if v < <NUM_LIT:16> : <EOL> return <EOL> cur_dir = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> rdir = os . path . join ( cur_dir , '<STR_LIT>' ) <EOL> fname = os . path . join ( rdir , '<STR_LIT>' ) <EOL> data = pd . read_csv ( fname ) <EOL> vcf = { "<STR_LIT>" : "<STR_LIT>" } <EOL> model = MixedLM . from_formula ( "<STR_LIT>" , groups = "<STR_LIT>" , <EOL> re_formula = "<STR_LIT:1>" , vc_formula = vcf , <EOL> data = data ) <EOL> result = model . fit ( ) <EOL> model2 = MixedLM . from_formula ( "<STR_LIT>" , groups = "<STR_LIT>" , <EOL> re_formula = "<STR_LIT:1>" , vc_formula = vcf , <EOL> use_sparse = True , data = data ) <EOL> result2 = model2 . fit ( ) <EOL> assert_allclose ( result . params , result2 . params ) <EOL> assert_allclose ( result . bse , result2 . bse ) <EOL> def test_pastes_vcomp ( self ) : <EOL> cur_dir = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> rdir = os . path . join ( cur_dir , '<STR_LIT>' ) <EOL> fname = os . path . join ( rdir , '<STR_LIT>' ) <EOL> data = pd . read_csv ( fname ) <EOL> vcf = { "<STR_LIT>" : "<STR_LIT>" } <EOL> model = MixedLM . from_formula ( "<STR_LIT>" , groups = "<STR_LIT>" , <EOL> re_formula = "<STR_LIT:1>" , vc_formula = vcf , <EOL> data = data ) <EOL> result = model . fit ( ) <EOL> assert_allclose ( result . fe_params . iloc [ <NUM_LIT:0> ] , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . bse . iloc [ <NUM_LIT:0> ] , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . cov_re . iloc [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . scale , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . llf , - <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_equal ( result . aic , np . nan ) <EOL> assert_equal ( result . bic , np . nan ) <EOL> resid = np . r_ [ <NUM_LIT> , - <NUM_LIT> , - <EOL> <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] <EOL> assert_allclose ( result . resid [ <NUM_LIT:0> : <NUM_LIT:5> ] , resid , rtol = <NUM_LIT> ) <EOL> fit = np . r_ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> assert_allclose ( result . fittedvalues [ <NUM_LIT:0> : <NUM_LIT:5> ] , fit , rtol = <NUM_LIT> ) <EOL> data = pd . read_csv ( fname ) <EOL> vcf = { "<STR_LIT>" : "<STR_LIT>" } <EOL> model = MixedLM . from_formula ( "<STR_LIT>" , groups = "<STR_LIT>" , <EOL> re_formula = "<STR_LIT:1>" , vc_formula = vcf , <EOL> data = data ) <EOL> result = model . fit ( reml = False ) <EOL> assert_allclose ( result . fe_params . iloc [ <NUM_LIT:0> ] , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . bse . iloc [ <NUM_LIT:0> ] , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . cov_re . iloc [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . scale , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . llf , - <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . aic , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result . bic , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> def test_vcomp_formula ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> n = <NUM_LIT> <EOL> exog = np . random . normal ( size = ( n , <NUM_LIT:2> ) ) <EOL> exog [ : , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> ex_vc = [ ] <EOL> groups = np . kron ( np . arange ( n / <NUM_LIT:4> ) , np . ones ( <NUM_LIT:4> ) ) <EOL> errors = <NUM_LIT:0> <EOL> exog_re = np . random . normal ( size = ( n , <NUM_LIT:2> ) ) <EOL> slopes = np . random . normal ( size = ( n / <NUM_LIT:4> , <NUM_LIT:2> ) ) <EOL> slopes = np . kron ( slopes , np . ones ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) ) * exog_re <EOL> errors += slopes . sum ( <NUM_LIT:1> ) <EOL> ex_vc = np . random . normal ( size = ( n , <NUM_LIT:4> ) ) <EOL> slopes = np . random . normal ( size = ( n / <NUM_LIT:4> , <NUM_LIT:4> ) ) <EOL> slopes [ : , <NUM_LIT:2> : ] *= <NUM_LIT:2> <EOL> slopes = np . kron ( slopes , np . ones ( ( <NUM_LIT:4> , <NUM_LIT:1> ) ) ) * ex_vc <EOL> errors += slopes . sum ( <NUM_LIT:1> ) <EOL> errors += np . random . normal ( size = n ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + errors <EOL> exog_vc = { "<STR_LIT:a>" : { } , "<STR_LIT:b>" : { } } <EOL> for k , group in enumerate ( range ( int ( n / <NUM_LIT:4> ) ) ) : <EOL> ix = np . flatnonzero ( groups == group ) <EOL> exog_vc [ "<STR_LIT:a>" ] [ group ] = ex_vc [ ix , <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> exog_vc [ "<STR_LIT:b>" ] [ group ] = ex_vc [ ix , <NUM_LIT:2> : ] <EOL> model1 = MixedLM ( endog , exog , groups , exog_re = exog_re , exog_vc = exog_vc ) <EOL> result1 = model1 . fit ( ) <EOL> df = pd . DataFrame ( exog [ : , <NUM_LIT:1> : ] , columns = [ "<STR_LIT>" , ] ) <EOL> df [ "<STR_LIT:y>" ] = endog <EOL> df [ "<STR_LIT>" ] = exog_re [ : , <NUM_LIT:0> ] <EOL> df [ "<STR_LIT>" ] = exog_re [ : , <NUM_LIT:1> ] <EOL> df [ "<STR_LIT>" ] = ex_vc [ : , <NUM_LIT:0> ] <EOL> df [ "<STR_LIT>" ] = ex_vc [ : , <NUM_LIT:1> ] <EOL> df [ "<STR_LIT>" ] = ex_vc [ : , <NUM_LIT:2> ] <EOL> df [ "<STR_LIT>" ] = ex_vc [ : , <NUM_LIT:3> ] <EOL> vc_formula = { "<STR_LIT:a>" : "<STR_LIT>" , "<STR_LIT:b>" : "<STR_LIT>" } <EOL> model2 = MixedLM . from_formula ( "<STR_LIT>" , groups = groups , <EOL> re_formula = "<STR_LIT>" , <EOL> vc_formula = vc_formula , data = df ) <EOL> result2 = model2 . fit ( ) <EOL> assert_allclose ( result1 . fe_params , result2 . fe_params , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result1 . cov_re , result2 . cov_re , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result1 . vcomp , result2 . vcomp , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result1 . params , result2 . params , rtol = <NUM_LIT> ) <EOL> assert_allclose ( result1 . bse , result2 . bse , rtol = <NUM_LIT> ) <EOL> def test_formulas ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> exog = np . random . normal ( size = ( <NUM_LIT> , <NUM_LIT:4> ) ) <EOL> exog_re = np . random . normal ( size = <NUM_LIT> ) <EOL> groups = np . kron ( np . arange ( <NUM_LIT:100> ) , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> g_errors = exog_re * np . kron ( np . random . normal ( size = <NUM_LIT:100> ) , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + g_errors + np . random . normal ( size = <NUM_LIT> ) <EOL> mod1 = MixedLM ( endog , exog , groups , exog_re ) <EOL> assert_ ( mod1 . data . xnames == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert_ ( mod1 . data . exog_re_names == [ "<STR_LIT>" ] ) <EOL> assert_ ( mod1 . data . exog_re_names_full == [ "<STR_LIT>" ] ) <EOL> rslt1 = mod1 . fit ( ) <EOL> df = pd . DataFrame ( { "<STR_LIT>" : endog } ) <EOL> for k in range ( exog . shape [ <NUM_LIT:1> ] ) : <EOL> df [ "<STR_LIT>" % k ] = exog [ : , k ] <EOL> df [ "<STR_LIT>" ] = exog_re <EOL> fml = "<STR_LIT>" <EOL> re_fml = "<STR_LIT>" <EOL> mod2 = MixedLM . from_formula ( fml , df , re_formula = re_fml , <EOL> groups = groups ) <EOL> assert_ ( mod2 . data . xnames == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert_ ( mod2 . data . exog_re_names == [ "<STR_LIT>" ] ) <EOL> assert_ ( mod2 . data . exog_re_names_full == [ "<STR_LIT>" ] ) <EOL> rslt2 = mod2 . fit ( ) <EOL> assert_almost_equal ( rslt1 . params , rslt2 . params ) <EOL> df [ "<STR_LIT>" ] = groups <EOL> mod3 = MixedLM . from_formula ( fml , df , re_formula = re_fml , <EOL> groups = "<STR_LIT>" ) <EOL> assert_ ( mod3 . data . xnames == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert_ ( mod3 . data . exog_re_names == [ "<STR_LIT>" ] ) <EOL> assert_ ( mod3 . data . exog_re_names_full == [ "<STR_LIT>" ] ) <EOL> rslt3 = mod3 . fit ( start_params = rslt2 . params ) <EOL> assert_allclose ( rslt1 . params , rslt3 . params , rtol = <NUM_LIT> ) <EOL> exog_re = np . ones ( len ( endog ) , dtype = np . float64 ) <EOL> mod4 = MixedLM ( endog , exog , groups , exog_re ) <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> rslt4 = mod4 . fit ( ) <EOL> from statsmodels . formula . api import mixedlm <EOL> mod5 = mixedlm ( fml , df , groups = "<STR_LIT>" ) <EOL> assert_ ( mod5 . data . exog_re_names == [ "<STR_LIT>" ] ) <EOL> assert_ ( mod5 . data . exog_re_names_full == [ "<STR_LIT>" ] ) <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> rslt5 = mod5 . fit ( ) <EOL> assert_almost_equal ( rslt4 . params , rslt5 . params ) <EOL> def test_regularized ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> exog = np . random . normal ( size = ( <NUM_LIT> , <NUM_LIT:5> ) ) <EOL> groups = np . kron ( np . arange ( <NUM_LIT:100> ) , np . ones ( <NUM_LIT:4> ) ) <EOL> expected_endog = exog [ : , <NUM_LIT:0> ] - exog [ : , <NUM_LIT:2> ] <EOL> endog = expected_endog + np . kron ( np . random . normal ( size = <NUM_LIT:100> ) , np . ones ( <NUM_LIT:4> ) ) + np . random . normal ( size = <NUM_LIT> ) <EOL> md = MixedLM ( endog , exog , groups ) <EOL> mdf1 = md . fit_regularized ( alpha = <NUM_LIT:1.> ) <EOL> mdf1 . summary ( ) <EOL> md = MixedLM ( endog , exog , groups ) <EOL> mdf2 = md . fit_regularized ( alpha = <NUM_LIT:10> * np . ones ( <NUM_LIT:5> ) ) <EOL> mdf2 . summary ( ) <EOL> pen = penalties . L2 ( ) <EOL> mdf3 = md . fit_regularized ( method = pen , alpha = <NUM_LIT:0.> ) <EOL> mdf3 . summary ( ) <EOL> pen = penalties . L2 ( ) <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> mdf4 = md . fit_regularized ( method = pen , alpha = <NUM_LIT> ) <EOL> mdf4 . summary ( ) <EOL> pen = penalties . PseudoHuber ( <NUM_LIT> ) <EOL> mdf5 = md . fit_regularized ( method = pen , alpha = <NUM_LIT:1.> ) <EOL> mdf5 . summary ( ) <EOL> def do1 ( self , reml , irf , ds_ix ) : <EOL> if irf and ds_ix < <NUM_LIT:6> : <EOL> return <EOL> irfs = "<STR_LIT>" if irf else "<STR_LIT>" <EOL> meth = "<STR_LIT>" if reml else "<STR_LIT>" <EOL> rslt = R_Results ( meth , irfs , ds_ix ) <EOL> md = MixedLM ( rslt . endog , rslt . exog_fe , rslt . groups , <EOL> rslt . exog_re ) <EOL> if not irf : <EOL> if np . any ( np . diag ( rslt . cov_re_r ) < <NUM_LIT> ) : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> mdf = md . fit ( gtol = <NUM_LIT> , reml = reml ) <EOL> else : <EOL> mdf = md . fit ( gtol = <NUM_LIT> , reml = reml ) <EOL> else : <EOL> k_fe = rslt . exog_fe . shape [ <NUM_LIT:1> ] <EOL> k_re = rslt . exog_re . shape [ <NUM_LIT:1> ] <EOL> free = MixedLMParams ( k_fe , k_re , <NUM_LIT:0> ) <EOL> free . fe_params = np . ones ( k_fe ) <EOL> free . cov_re = np . eye ( k_re ) <EOL> free . vcomp = np . array ( [ ] ) <EOL> if np . any ( np . diag ( rslt . cov_re_r ) < <NUM_LIT> ) : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> mdf = md . fit ( reml = reml , gtol = <NUM_LIT> , free = free ) <EOL> else : <EOL> mdf = md . fit ( reml = reml , gtol = <NUM_LIT> , free = free ) <EOL> assert_almost_equal ( mdf . fe_params , rslt . coef , decimal = <NUM_LIT:4> ) <EOL> assert_almost_equal ( mdf . cov_re , rslt . cov_re_r , decimal = <NUM_LIT:4> ) <EOL> assert_almost_equal ( mdf . scale , rslt . scale_r , decimal = <NUM_LIT:4> ) <EOL> k_fe = md . k_fe <EOL> assert_almost_equal ( rslt . vcov_r , mdf . cov_params ( ) [ <NUM_LIT:0> : k_fe , <NUM_LIT:0> : k_fe ] , <EOL> decimal = <NUM_LIT:3> ) <EOL> assert_almost_equal ( mdf . llf , rslt . loglike [ <NUM_LIT:0> ] , decimal = <NUM_LIT:2> ) <EOL> if not irf : <EOL> assert_almost_equal ( mdf . random_effects [ <NUM_LIT:0> ] , rslt . ranef_postmean , <EOL> decimal = <NUM_LIT:3> ) <EOL> assert_almost_equal ( mdf . random_effects_cov [ <NUM_LIT:0> ] , <EOL> rslt . ranef_condvar , <EOL> decimal = <NUM_LIT:3> ) <EOL> def test_r ( self ) : <EOL> cur_dir = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> rdir = os . path . join ( cur_dir , '<STR_LIT>' ) <EOL> fnames = os . listdir ( rdir ) <EOL> fnames = [ x for x in fnames if x . startswith ( "<STR_LIT>" ) <EOL> and x . endswith ( "<STR_LIT>" ) ] <EOL> for fname in fnames : <EOL> for reml in False , True : <EOL> for irf in False , True : <EOL> ds_ix = int ( fname [ <NUM_LIT:3> : <NUM_LIT:5> ] ) <EOL> yield self . do1 , reml , irf , ds_ix <EOL> def test_mixed_lm_wrapper ( ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> exog = np . random . normal ( size = ( <NUM_LIT> , <NUM_LIT:4> ) ) <EOL> exog_re = np . random . normal ( size = <NUM_LIT> ) <EOL> groups = np . kron ( np . arange ( <NUM_LIT:100> ) , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> g_errors = exog_re * np . kron ( np . random . normal ( size = <NUM_LIT:100> ) , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + g_errors + np . random . normal ( size = <NUM_LIT> ) <EOL> df = pd . DataFrame ( { "<STR_LIT>" : endog } ) <EOL> for k in range ( exog . shape [ <NUM_LIT:1> ] ) : <EOL> df [ "<STR_LIT>" % k ] = exog [ : , k ] <EOL> df [ "<STR_LIT>" ] = exog_re <EOL> fml = "<STR_LIT>" <EOL> re_fml = "<STR_LIT>" <EOL> mod2 = MixedLM . from_formula ( fml , df , re_formula = re_fml , <EOL> groups = groups ) <EOL> result = mod2 . fit ( ) <EOL> result . summary ( ) <EOL> xnames = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> re_names = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> re_names_full = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> assert_ ( mod2 . data . xnames == xnames ) <EOL> assert_ ( mod2 . data . exog_re_names == re_names ) <EOL> assert_ ( mod2 . data . exog_re_names_full == re_names_full ) <EOL> params = result . params <EOL> assert_ ( params . index . tolist ( ) == xnames + re_names_full ) <EOL> bse = result . bse <EOL> assert_ ( bse . index . tolist ( ) == xnames + re_names_full ) <EOL> tvalues = result . tvalues <EOL> assert_ ( tvalues . index . tolist ( ) == xnames + re_names_full ) <EOL> cov_params = result . cov_params ( ) <EOL> assert_ ( cov_params . index . tolist ( ) == xnames + re_names_full ) <EOL> assert_ ( cov_params . columns . tolist ( ) == xnames + re_names_full ) <EOL> fe = result . fe_params <EOL> assert_ ( fe . index . tolist ( ) == xnames ) <EOL> bse_fe = result . bse_fe <EOL> assert_ ( bse_fe . index . tolist ( ) == xnames ) <EOL> cov_re = result . cov_re <EOL> assert_ ( cov_re . index . tolist ( ) == re_names ) <EOL> assert_ ( cov_re . columns . tolist ( ) == re_names ) <EOL> cov_re_u = result . cov_re_unscaled <EOL> assert_ ( cov_re_u . index . tolist ( ) == re_names ) <EOL> assert_ ( cov_re_u . columns . tolist ( ) == re_names ) <EOL> bse_re = result . bse_re <EOL> assert_ ( bse_re . index . tolist ( ) == re_names_full ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import nose <EOL> nose . runmodule ( argv = [ __file__ , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> exit = False ) </s>
