You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm doing a linear fit with statsmodels.api.GLS. I stumbled upon a case where .fit() produces a result that has only 1 value in .params. I'd expect to get 2 - beta and the intercept (y=beta*x + intercept). I checked the docs, but sadly I'm no smarter. Am I perhaps using this incorrectly?
Code Sample, a copy-pastable example if possible
importstatsmodels.apiassmfirst_normalized_pc=pd.Series(...)
second_normalized_pc=pd.Series(...)
# get rolling differencewindow_len=100differences= []
idx_start=10000idx_end=40000foriinrange(idx_start, idx_end):
window_end=i+window_lenx=first_normalized_pc[i:window_end]
S1=sm.add_constant(x.values)
y=second_normalized_pc[i:window_end]
results=sm.GLS(y.values, S1).fit()
differences.append(results.params[1] *x[-1] -y[-1] +results.params[0])
Expected Output
I'd naively expect that results.params contains 2 items.
Output of import statsmodels.api as sm; sm.show_versions()
INSTALLED VERSIONS
Python: 3.7.10.final.0
OS: Linux 5.4.0-89-generic #100-Ubuntu SMP Fri Sep 24 14:50:10 UTC 2021 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
The most likely reason is that x is constant in a window. By default, add_constant does NOT add a constant if one already exists.
In general it's better to add a constant to the dataframe, because then add_constant will not be in the loop and it avoids an additional array copy.
add_constant has an option to add the constant even if one already exists. However, in that case there will be two columns in x with constant values. In this case parameters will not be identified and the estimate by OLS/WLS/GLS is a pinv regularized solution.
Describe the bug
I'm doing a linear fit with
statsmodels.api.GLS
. I stumbled upon a case where.fit()
produces a result that has only 1 value in.params
. I'd expect to get 2 - beta and the intercept (y=beta*x + intercept
). I checked the docs, but sadly I'm no smarter. Am I perhaps using this incorrectly?Code Sample, a copy-pastable example if possible
Expected Output
I'd naively expect that
results.params
contains 2 items.Output of
import statsmodels.api as sm; sm.show_versions()
INSTALLED VERSIONS
Python: 3.7.10.final.0
OS: Linux 5.4.0-89-generic #100-Ubuntu SMP Fri Sep 24 14:50:10 UTC 2021 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
statsmodels
Installed: 0.12.2 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/statsmodels)
Required Dependencies
cython: 0.29.24 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/Cython)
numpy: 1.20.3 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/numpy)
scipy: 1.5.4 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/scipy)
pandas: 1.3.2 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/pandas)
dateutil: 2.8.2 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/dateutil)
patsy: 0.5.2 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/patsy)
Optional Dependencies
matplotlib: 3.4.3 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/matplotlib)
backend: module://matplotlib_inline.backend_inline
cvxopt: Not installed
joblib: 1.0.1 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/joblib)
Developer Tools
IPython: 7.27.0 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/IPython)
jinja2: 3.0.1 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/jinja2)
sphinx: Not installed
pygments: 2.10.0 (/home/toaster/PROGS/miniconda3/envs/puma-lab/lib/python3.7/site-packages/pygments)
pytest: Not installed
virtualenv: Not installed
The text was updated successfully, but these errors were encountered: