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
{{ message }}
Repository navigation
Erroe: fcast = mu + np.dot(arparams, endog[i:i+p]) #5258
The python code is as above, When I run this code, I got following error,
File "AR_test.py", line 8, in
output = model_fit.predict(start=10,end=12)
File "D:\anaconda\envs\tensorflow\lib\site-packages\statsmodels\base\wrapper.py", line 95, in wrapper
obj = data.wrap_output(func(results, *args, **kwargs), how)
File "D:\anaconda\envs\tensorflow\lib\site-packages\statsmodels\tsa\ar_model.py", line 795, in predict
predictedvalues = self.model.predict(params, start, end, dynamic)
File "D:\anaconda\envs\tensorflow\lib\site-packages\statsmodels\tsa\ar_model.py", line 237, in predict
out_of_sample)
File "D:\anaconda\envs\tensorflow\lib\site-packages\statsmodels\tsa\ar_model.py", line 49, in _ar_predict_out_of_sample
fcast = mu + np.dot(arparams, endog[i:i+p])
ValueError: shapes (6,) and (7,) not aligned: 6 (dim 0) != 7 (dim 0)
Could anyone suggest how to fix this issue? Thanks in advance.
My initial hypothesis about the maxlag default value was incorrect so I edited it with my new findings, although you can return the sample model prediction if you set maxlag to 6 or lower in the .fit() statement:
I believe this has to do with the add_trend that is getting called during _xstackX of the .fit() call. It may have to do with the repetitive nature of the sample data (the three repeating 0.414...) as it seems to think a constant is already added, and does not add one. Therefore the number of parameters is misshapen.
fromstatsmodels.tsa.ar_modelimportARimportnumpyasnpsample=np.array([0.46341460943222046, 0.46341460943222046, 0.39024388790130615,
0.4146341383457184, 0.4146341383457184, 0.4146341383457184,
0.3414634168148041, 0.4390243887901306, 0.46341460943222046,
0.4390243887901306])
model1=AR(sample)
model_fit1=model1.fit()
len(model_fit1.params) #returns a length of 7#Changing the data slightlysample=np.array([0.461460943222046, 0.46341460943222046, 0.39024388790130615,
0.5146341383457184, 0.2146341383457184, 0.4146341383457184,
0.3414634168148041, 0.4390243887901306, 0.46341460943222046,
0.4390243887901306])
model2=AR(sample)
model_fit2=model2.fit()
len(model_fit2.params) #returns a length of 8
This is the piece in statsmodels.tsa.tsatools.add_trend that is evaluating for the already existing constant column and by default skips if it is present.
Where x is the array of lags. For model 1 it returns true, and skips the adding constant value which leaves it one short when it does the OLS fitting which produces the model that is one value short.
Not sure if that is a bug or not, or if this is just a unique circumstance with the sample values, I also tried replacing those repeating 0.41463... with other repeating numbers and the error occurred.
Hope that helps to tease out the issue a little more clearly, and what I have found so far.
@jtweeder is right. Before the call to add_trend column 3 is constant, so add_trend silently changes trend from "c" to "nc", but that information doesn't make it back to the AR object, which still has k_trend set to 1.
Note that res.params here matches model_fit.params the OP would have gotten. The only difference is that model.k_trend (the fact that this is a model attribute is a different can of worms) attribute is correctly set to 0.
Two options for how to fix this:
in _stackX pass has_constant="raise" to add_trend, and if it raises then tell the user to try passing trend="nc"
Activity
jtweeder commentedon Oct 12, 2018
My initial hypothesis about the maxlag default value was incorrect so I edited it with my new findings, although you can return the sample model prediction if you set maxlag to 6 or lower in the
.fit()statement:I believe this has to do with the
add_trendthat is getting called during_xstackXof the.fit()call. It may have to do with the repetitive nature of the sample data (the three repeating 0.414...) as it seems to think a constant is already added, and does not add one. Therefore the number of parameters is misshapen.This is the piece in
statsmodels.tsa.tsatools.add_trendthat is evaluating for the already existing constant column and by default skips if it is present.Where x is the array of lags. For model 1 it returns true, and skips the adding constant value which leaves it one short when it does the OLS fitting which produces the model that is one value short.
Not sure if that is a bug or not, or if this is just a unique circumstance with the sample values, I also tried replacing those repeating 0.41463... with other repeating numbers and the error occurred.
Hope that helps to tease out the issue a little more clearly, and what I have found so far.
josef-pkt commentedon Dec 11, 2018
This got lost without label.
temporarily prio-high to figure out what's going on.
jbrockmendel commentedon May 28, 2019
@jtweeder is right. Before the call to
add_trendcolumn 3 is constant, soadd_trendsilently changes trend from "c" to "nc", but that information doesn't make it back to theARobject, which still hask_trendset to 1.If instead of calling
res = model.fit()we do:Note that
res.paramshere matchesmodel_fit.paramsthe OP would have gotten. The only difference is thatmodel.k_trend(the fact that this is a model attribute is a different can of worms) attribute is correctly set to 0.Two options for how to fix this:
_stackXpasshas_constant="raise"toadd_trend, and if it raises then tell the user to try passingtrend="nc"add_trendreturn an updatedk_trend