Module Expr Str ImportFrom alias Import alias identifier ImportFrom alias alias Assign NameStore ListLoad Str If CompareIn Str NameLoad body Expr Call AttributeLoad AttributeLoad NameLoad attr attr Num Assign NameStore Num Assign NameStore ListLoad Assign NameStore Num Assign NameStore Num Assign NameStore Num Assign NameStore Call AttributeLoad NameLoad attr NameLoad Assign NameStore BinOpMult Num Call AttributeLoad NameLoad attr NameLoad Assign SubscriptStore NameLoad Index Num Num Assign NameStore ListLoad For NameStore Call NameLoad NameLoad body Assign NameStore BinOpAdd NameLoad BinOpMult Num Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad Expr Call AttributeLoad NameLoad attr NameLoad If CompareGt NameLoad BinOpFloorDiv NameLoad Num body Assign NameStore Num Assign NameStore Call AttributeLoad AttributeLoad NameLoad attr attr TupleLoad NameLoad NameLoad Assign NameStore Call AttributeLoad AttributeLoad NameLoad attr attr TupleLoad NameLoad BinOpSub NameLoad Num Assign NameStore Call AttributeLoad NameLoad attr TupleLoad Call AttributeLoad NameLoad attr NameLoad NameLoad Assign NameStore BinOpMult Num Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad Assign NameStore BinOpAdd BinOpAdd Call AttributeLoad NameLoad attr NameLoad NameLoad Call AttributeLoad NameLoad attr NameLoad NameLoad NameLoad Assign NameStore Call AttributeLoad NameLoad attr TupleLoad NameLoad NameLoad Assign NameStore Call NameLoad NameLoad NameLoad NameLoad Expr Call AttributeLoad NameLoad attr NameLoad Assign NameStore Call NameLoad NameLoad Import alias Assign NameStore Call AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr keyword Num keyword Num keyword Num keyword Num Assign NameStore Call AttributeLoad NameLoad attr Expr Call NameLoad Str BinOpSub NameLoad NameLoad Expr Call NameLoad Str AttributeLoad NameLoad attr Expr Call NameLoad Str Expr Call NameLoad AttributeLoad NameLoad attr Expr Call NameLoad AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr Expr Call NameLoad Str Expr Call NameLoad Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr NameLoad Expr Call NameLoad AttributeLoad NameLoad attr Assign NameStore AttributeLoad NameLoad attr Expr Call NameLoad Str Call AttributeLoad NameLoad attr Num Expr Call NameLoad Str Call AttributeLoad NameLoad attr Num Expr Call NameLoad Str Expr Call NameLoad Call AttributeLoad NameLoad attr NameLoad keyword Num Expr Call NameLoad Str Expr Call NameLoad Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr NameLoad keyword Num Expr Call NameLoad Str Expr Call NameLoad Call AttributeLoad NameLoad attr Expr Call NameLoad Str Expr Call NameLoad Call AttributeLoad NameLoad attr Expr Call NameLoad Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr Expr Call NameLoad Str Expr Call NameLoad SubscriptLoad SubscriptLoad AttributeLoad NameLoad attr Index Str Slice Num Expr Call NameLoad Str Expr Call NameLoad Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr SubscriptLoad SubscriptLoad AttributeLoad NameLoad attr Index Str Slice Num keyword Num Expr Call NameLoad Str Expr Call NameLoad Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr SubscriptLoad SubscriptLoad AttributeLoad NameLoad attr Index Str Slice Num keyword Num Assign NameStore Call AttributeLoad NameLoad attr ListComp Call AttributeLoad BinOpMult AttributeLoad NameLoad attr SubscriptLoad AttributeLoad NameLoad attr ExtSlice Index NameLoad Slice attr Num comprehension NameStore AttributeLoad NameLoad attr Expr Str Expr Call NameLoad Str Assign NameStore Call AttributeLoad NameLoad attr NameLoad Expr Call NameLoad Str Call AttributeLoad NameLoad attr Num Expr Call NameLoad Str SubscriptLoad AttributeLoad NameLoad attr Slice Num Expr Call NameLoad Str Call AttributeLoad NameLoad attr Num Expr Call NameLoad Str Assign NameStore Call AttributeLoad Call AttributeLoad NameLoad attr NameLoad attr Num Assign NameStore BinOpMult Call AttributeLoad BinOpSub BinOpDiv BinOpAdd SubscriptLoad AttributeLoad NameLoad attr Slice Num NameLoad NameLoad Num attr Num Num Assign NameStore BinOpMult BinOpDiv Call AttributeLoad Call AttributeLoad NameLoad attr BinOpSub BinOpAdd SubscriptLoad AttributeLoad NameLoad attr Slice Num NameLoad NameLoad attr Num NameLoad Num Assign NameStore BinOpMult BinOpDiv Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr BinOpSub BinOpAdd SubscriptLoad AttributeLoad NameLoad attr Slice Num NameLoad NameLoad Num NameLoad Num Assign NameStore BinOpMult BinOpDiv Call AttributeLoad BinOpSub BinOpAdd SubscriptLoad AttributeLoad NameLoad attr Slice Num NameLoad NameLoad attr Num NameLoad Num Expr Call NameLoad Str NameLoad Expr Call NameLoad Str NameLoad Expr Call NameLoad Str NameLoad Expr Call NameLoad Str NameLoad ImportFrom alias Expr Call NameLoad AttributeLoad NameLoad attr Expr Call NameLoad AttributeLoad NameLoad attr Expr Call NameLoad AttributeLoad NameLoad attr Expr Call NameLoad Call AttributeLoad NameLoad attr ListLoad Num Num Num Num Num Num Expr Call NameLoad Str Expr Call NameLoad Call AttributeLoad NameLoad attr ListLoad ListLoad Num Num Num Num Num Num ListLoad Num Num Num Num Num Num Assign NameStore Call AttributeLoad NameLoad attr keyword Num Assign NameStore Call AttributeLoad NameLoad attr Num Num Import alias identifier Expr Call AttributeLoad NameLoad attr
N/A N/A Example using OneWayMixed


Created on Sat Dec 03 10:15:55 2011

Author: Josef Perktold

This example constructs a linear model with individual specific random
effects and random coefficients, and uses OneWayMixed to estimate it.


 __future__ print_function N/A numpy np statsmodels.sandbox.panel.mixed OneWayMixed Unit N/A examples N/A ex1 N/A N/A ex1 examples N/A N/A N/A N/A N/A np random seed 978326 N/A nsubj 2000 N/A units N/A N/A nobs_i 4 N/A nx 4 N/A nz 2 N/A beta N/A N/A np ones nx N/A gamma N/A 0.5 N/A N/A np ones nz N/A N/A gamma N/A 0 0 N/A gamma_re_true N/A N/A i N/A range nsubj N/A N/A gamma_re N/A gamma N/A 0.2 N/A N/A N/A np random standard_normal nz N/A N/A N/A gamma_re_true append gamma_re N/A N/A i N/A nsubj 4 N/A N/A nobs_i 6 N/A X N/A N/A N/A np random standard_normal N/A nobs_i nx N/A Z N/A N/A N/A np random standard_normal N/A nobs_i N/A nz 1 N/A Z N/A N/A np column_stack N/A N/A N/A np ones nobs_i Z N/A noise N/A 0.1 N/A N/A N/A np random randn nobs_i N/A Y N/A N/A N/A N/A np dot X beta N/A N/A np dot Z gamma_re noise N/A X N/A N/A np hstack N/A X Z N/A unit N/A Unit Y X Z N/A N/A N/A units append unit N/A m N/A OneWayMixed units N/A time N/A t0 N/A N/A time time N/A N/A N/A m initialize N/A res N/A N/A m fit maxiter 100 rtol 1e-05 params_rtol 1e-06 params_atol 1e-06 N/A t1 N/A N/A time time N/A N/A print time for initialize and fit N/A t1 t0 N/A N/A print number of iterations N/A m iterations N/A N/A print 
estimates for fixed effects N/A N/A print N/A m a N/A N/A print N/A m params N/A bfixed_cov N/A N/A m cov_fixed N/A N/A print beta fixed standard errors N/A N/A print N/A N/A np sqrt N/A N/A np diag bfixed_cov N/A N/A print N/A m bse N/A b_re N/A m params_random_units N/A N/A print RE mean: N/A N/A b_re mean 0 N/A N/A print RE columns std N/A N/A b_re std 0 N/A N/A print np.cov(b_re, rowvar=0), sample statistic N/A N/A print N/A N/A np cov b_re rowvar 0 N/A N/A print std of above N/A N/A print N/A N/A np sqrt N/A N/A np diag N/A N/A np cov b_re rowvar 0 N/A N/A print m.cov_random() N/A N/A print N/A N/A m cov_random N/A N/A print std of above N/A N/A print N/A N/A res std_random N/A N/A print N/A N/A np sqrt N/A N/A np diag N/A N/A m cov_random N/A N/A print 
(non)convergence of llf N/A N/A print N/A N/A N/A m history N/A llf N/A -4 N/A N/A print convergence of parameters N/A N/A print N/A N/A np diff N/A N/A np vstack N/A N/A N/A m history N/A params N/A -4 axis 0 N/A N/A print convergence of D N/A N/A print N/A N/A np diff N/A N/A np array N/A N/A N/A m history N/A D N/A -4 axis 0 N/A zb N/A N/A np array N/A N/A N/A N/A N/A unit Z N/A N/A unit b N/A N/A None N/A sum 0 N/A unit N/A m units N/A if Z is not included in X:
    >>> np.dot(b_re.T, b_re)/100
    array([[ 0.03270611, -0.00916051],
           [-0.00916051,  0.26432783]])
    >>> m.cov_random()
    array([[ 0.0348722 , -0.00909159],
           [-0.00909159,  0.26846254]])
    >>> #note cov_random doesn't subtract mean!
     N/A N/A print 
checking the random effects distribution and prediction N/A gamma_re_true N/A N/A np array gamma_re_true N/A N/A print mean of random effect true N/A N/A gamma_re_true mean 0 N/A N/A print mean from fixed effects    N/A N/A m params N/A -2 N/A N/A print mean of estimated RE       N/A N/A b_re mean 0 N/A N/A print N/A absmean_true N/A N/A N/A N/A np abs gamma_re_true mean 0 N/A mape N/A N/A N/A N/A N/A N/A N/A N/A m params N/A -2 b_re gamma_re_true 1 mean 0 100 N/A mean_abs_perc N/A N/A N/A N/A N/A N/A np abs N/A N/A N/A N/A m params N/A -2 b_re gamma_re_true mean 0 absmean_true 100 N/A median_abs_perc N/A N/A N/A N/A np median N/A N/A np abs N/A N/A N/A N/A m params N/A -2 b_re gamma_re_true 0 absmean_true 100 N/A rmse_perc N/A N/A N/A N/A N/A N/A N/A N/A m params N/A -2 b_re gamma_re_true std 0 absmean_true 100 N/A N/A print mape            mape N/A N/A print mean_abs_perc   mean_abs_perc N/A N/A print median_abs_perc median_abs_perc N/A N/A print rmse_perc (std) rmse_perc numpy.testing assert_almost_equal N/A N/A print N/A res llf N/A N/A print N/A res tvalues N/A N/A print N/A res pvalues N/A N/A print N/A N/A res t_test N/A 1 -1 0 0 0 0 N/A N/A print test mean of both random effects variables is zero N/A N/A print N/A N/A res f_test N/A N/A 0 0 0 0 1 0 N/A 0 0 0 0 0 1 N/A plots N/A N/A res plot_random_univariate bins 50 N/A fig N/A N/A res plot_scatter_pairs 0 1 N/A matplotlib.pyplot plt N/A N/A N/A plt show