Repository: statsmodels/statsmodels

Issue:
BUG: SARIMAXResults save_model() broken when MA order=0
#### Describe the bug

The SARIMAXResults() save_model() method breaks when the MA order is 0.  It gives:
AttributeError: 'SARIMAXResults' object has no attribute '_params_ma'

#### Code Sample, a copy-pastable example if possible

from statsmodels.tsa.statespace.sarimax import SARIMAX
import pandas as pd

ts = pd.Series(range(100))
model = SARIMAX(ts,order=(1,0,0))
model_fit = model.fit()
model_fit.save('testing.model', remove_data=True)

#### Expected Output

Should save without error.

#### Output of ``import statsmodels.api as sm; sm.show_versions()``

<details>

INSTALLED VERSIONS
------------------
Python: 3.7.10.final.0
OS: Linux 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

statsmodels
===========

Installed: 0.12.2 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/statsmodels)

Required Dependencies
=====================

cython: Not installed
numpy: 1.20.1 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/numpy)
scipy: 1.6.1 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/scipy)
pandas: 1.2.3 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/pandas)
    dateutil: 2.8.1 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/dateutil)
patsy: 0.5.1 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/patsy)

Optional Dependencies
=====================

matplotlib: 3.3.4 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/matplotlib)
    backend: module://ipykernel.pylab.backend_inline 
cvxopt: Not installed
joblib: 1.0.1 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/joblib)

Developer Tools
================

IPython: 7.21.0 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/IPython)
    jinja2: Not installed
sphinx: Not installed
    pygments: 2.8.1 (/shared/apps/anaconda37/envs/fo1/lib/python3.7/site-packages/pygments)
pytest: Not installed
virtualenv: Not installed

</details>



Discussion:

Resolving patch:
diff --git a/statsmodels/tsa/statespace/sarimax.py b/statsmodels/tsa/statespace/sarimax.py
--- a/statsmodels/tsa/statespace/sarimax.py
+++ b/statsmodels/tsa/statespace/sarimax.py
@@ -1879,6 +1879,10 @@ def __init__(self, model, params, filter_results, cov_type=None,
             end += k
             setattr(self, '_params_%s' % name, self.params[start:end])
             start += k
+        # GH7527, all terms must be defined
+        all_terms = ['ar', 'ma', 'seasonal_ar', 'seasonal_ma', 'variance']
+        for name in set(all_terms).difference(self.param_terms):
+            setattr(self, '_params_%s' % name, np.empty(0))
 
         # Handle removing data
         self._data_attr_model.extend(['orig_endog', 'orig_exog'])


Test patch:
diff --git a/statsmodels/tsa/statespace/tests/test_save.py b/statsmodels/tsa/statespace/tests/test_save.py
--- a/statsmodels/tsa/statespace/tests/test_save.py
+++ b/statsmodels/tsa/statespace/tests/test_save.py
@@ -39,6 +39,19 @@ def test_sarimax(temp_filename):
     assert_allclose(res.llf, res2.llf)
 
 
+# GH7527
+@pytest.mark.parametrize("order", ((4, 1, 0), (0, 1, 4), (0, 2, 0)))
+def test_sarimax_save_remove_data(temp_filename, order):
+    mod = sarimax.SARIMAX(macrodata["realgdp"].values, order=order)
+    res = mod.smooth(mod.start_params)
+    res.summary()
+    res.save(temp_filename, remove_data=True)
+    res2 = sarimax.SARIMAXResults.load(temp_filename)
+    assert_allclose(res.params, res2.params)
+    assert_allclose(res.bse, res2.bse)
+    assert_allclose(res.llf, res2.llf)
+
+
 def test_sarimax_pickle():
     mod = sarimax.SARIMAX(macrodata['realgdp'].values, order=(4, 1, 0))
     pkl_mod = pickle.loads(pickle.dumps(mod))