x
import statsmodels.api as sm
import statsmodels.formula.api as smf
data = sm.datasets.get_rdataset("dietox", "geepack").data
xxxxxxxxxx
training = data.iloc[0:500,]
testing = data.iloc[501:-1,]["Time"].to_frame()
md = smf.mixedlm("Weight ~ Time", training, groups=training["Pig"])
mdf = md.fit()
mdf.predict(testing)
--------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) <ipython-input-20-38abca506908> in <module>() ----> 1 mdf.predict(testing) /Users/wangya/anaconda/lib/python2.7/site-packages/statsmodels/base/model.pyc in predict(self, exog, transform, *args, **kwargs) 747 exog = np.atleast_2d(exog) # needed in count model shape[1] 748 --> 749 return self.model.predict(self.params, exog, *args, **kwargs) 750 751 /Users/wangya/anaconda/lib/python2.7/site-packages/statsmodels/base/model.pyc in predict(self, params, exog, *args, **kwargs) 175 This is a placeholder intended to be overwritten by individual models. 176 """ --> 177 raise NotImplementedError 178 179 NotImplementedError:
x
print(mdf.summary())
Mixed Linear Model Regression Results ======================================================== Model: MixedLM Dependent Variable: Weight No. Observations: 500 Method: REML No. Groups: 42 Scale: 9.6607 Min. group size: 10 Likelihood: -1351.6411 Max. group size: 12 Converged: Yes Mean group size: 11.9 -------------------------------------------------------- Coef. Std.Err. z P>|z| [0.025 0.975] -------------------------------------------------------- Intercept 14.905 0.862 17.296 0.000 13.216 16.594 Time 6.969 0.041 171.925 0.000 6.889 7.048 Intercept RE 27.506 2.129 ========================================================
testing
Time | |
---|---|
501 | 12 |
502 | 1 |
503 | 2 |
504 | 3 |
505 | 4 |
506 | 5 |
507 | 6 |
508 | 7 |
509 | 8 |
510 | 9 |
511 | 10 |
512 | 11 |
513 | 12 |
514 | 1 |
515 | 2 |
516 | 3 |
517 | 4 |
518 | 5 |
519 | 6 |
520 | 7 |
521 | 8 |
522 | 9 |
523 | 10 |
524 | 11 |
525 | 12 |
526 | 1 |
527 | 2 |
528 | 3 |
529 | 4 |
530 | 5 |
... | ... |
830 | 6 |
831 | 7 |
832 | 8 |
833 | 9 |
834 | 10 |
835 | 11 |
836 | 12 |
837 | 1 |
838 | 2 |
839 | 3 |
840 | 4 |
841 | 5 |
842 | 6 |
843 | 7 |
844 | 8 |
845 | 9 |
846 | 10 |
847 | 11 |
848 | 12 |
849 | 1 |
850 | 2 |
851 | 3 |
852 | 4 |
853 | 5 |
854 | 6 |
855 | 7 |
856 | 8 |
857 | 9 |
858 | 10 |
859 | 11 |
359 rows × 1 columns
mdf.predict(data["Time"])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-19-cbc33a4b31b4> in <module>() ----> 1 mdf.predict(data["Time"]) /Users/wangya/anaconda/lib/python2.7/site-packages/statsmodels/base/model.py in predict(self, exog, transform, *args, **kwargs) 738 from patsy import dmatrix 739 exog = dmatrix(self.model.data.orig_exog.design_info.builder, --> 740 exog) 741 742 if exog is not None: /Users/wangya/anaconda/lib/python2.7/site-packages/patsy/highlevel.py in dmatrix(formula_like, data, eval_env, NA_action, return_type) 289 eval_env = EvalEnvironment.capture(eval_env, reference=1) 290 (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env, --> 291 NA_action, return_type) 292 if lhs.shape[1] != 0: 293 raise PatsyError("encountered outcome variables for a model " /Users/wangya/anaconda/lib/python2.7/site-packages/patsy/highlevel.py in _do_highlevel_design(formula_like, data, eval_env, NA_action, return_type) 167 return build_design_matrices(design_infos, data, 168 NA_action=NA_action, --> 169 return_type=return_type) 170 else: 171 # No builders, but maybe we can still get matrices /Users/wangya/anaconda/lib/python2.7/site-packages/patsy/build.py in build_design_matrices(design_infos, data, NA_action, return_type, dtype) 886 for factor_info in six.itervalues(design_info.factor_infos): 887 if factor_info not in factor_info_to_values: --> 888 value, is_NA = _eval_factor(factor_info, data, NA_action) 889 factor_info_to_isNAs[factor_info] = is_NA 890 # value may now be a Series, DataFrame, or ndarray /Users/wangya/anaconda/lib/python2.7/site-packages/patsy/build.py in _eval_factor(factor_info, data, NA_action) 61 def _eval_factor(factor_info, data, NA_action): 62 factor = factor_info.factor ---> 63 result = factor.eval(factor_info.state, data) 64 # Returns either a 2d ndarray, or a DataFrame, plus is_NA mask 65 if factor_info.type == "numerical": /Users/wangya/anaconda/lib/python2.7/site-packages/patsy/eval.py in eval(self, memorize_state, data) 564 return self._eval(memorize_state["eval_code"], 565 memorize_state, --> 566 data) 567 568 __getstate__ = no_pickling /Users/wangya/anaconda/lib/python2.7/site-packages/patsy/eval.py in _eval(self, code, memorize_state, data) 549 memorize_state["eval_env"].eval, 550 code, --> 551 inner_namespace=inner_namespace) 552 553 def memorize_chunk(self, state, which_pass, data): /Users/wangya/anaconda/lib/python2.7/site-packages/patsy/compat.py in call_and_wrap_exc(msg, origin, f, *args, **kwargs) 115 def call_and_wrap_exc(msg, origin, f, *args, **kwargs): 116 try: --> 117 return f(*args, **kwargs) 118 except Exception as e: 119 if sys.version_info[0] >= 3: /Users/wangya/anaconda/lib/python2.7/site-packages/patsy/eval.py in eval(self, expr, source_name, inner_namespace) 164 code = compile(expr, source_name, "eval", self.flags, False) 165 return eval(code, {}, VarLookupDict([inner_namespace] --> 166 + self._namespaces)) 167 168 @classmethod <string> in <module>() NameError: name 'Time' is not defined