VAR model ValueError: x already contains a constant

3

0

I'm using VAR model for multivariate time series. The structure is that although each variable is a linear function of past lags of itself and past lags of the other variables, one and/or two of the variables MAY NOT alter within the period under investigation. Out of 10 variables.

Below is a similar dataframe to the one I'm working on. The actual dataset has 190 rows.

x0 = [0,0,0,0,0]
x1 = [0.011866,0.013380,0.015357,0.024451,0.030889]
x2 = [0,2,2,3,3]
x3 = [1,1,2,3,3]
x4 = [0,0,0,0,0]

T = ['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04', '2000-01-05']
TDT = pd.to_datetime(T)
df = pd.DataFrame({'X0': x0, 'X1': x1, 'X2': x2, 'X3': x3, 'X4': x4})
df.index = TDT
df

model = VAR(df)
result = model.fit(1)

**ValueError: x already contains a constant**

Is there a way to fix this?

Abs

Posted 2018-09-13T12:44:06.150

Reputation: 171

1Is this the VAR object from the statsmodels package for python? Why are you passing 1 to fit(), because you only want 1 lag? – n1k31t4 – 2018-09-13T13:51:05.720

Yes. Also with lag=2, I get the same result. – Abs – 2018-09-13T13:55:18.877

Answers

2

Your two columns X0 and X4 are constants, i.e. they contain a single value throughout.

The model will be trying to find a constant during its fit, so probably has a check that you're not including one.

Once you have your entire dataframe (e.g. called df), you can remove constant columns like the two above by using:

df_no_constants = df.loc[:, (df != df.iloc[0]).any()]

Then try putting that into your model as before.

n1k31t4

Posted 2018-09-13T12:44:06.150

Reputation: 12 573

10% of the dataframe I come across has a similar feature. – Abs – 2018-09-13T13:34:49.127

Similar or constant? – n1k31t4 – 2018-09-13T13:49:29.103

up to 2 of the variables is a constant . – Abs – 2018-09-13T13:53:09.067

2A variable that is constant over time makes absolutely no sense in an autoregressive model as its variance over time is zero. Remove them and try again. – n1k31t4 – 2018-09-13T13:54:31.563

0

Method:1

In your dataset, if any column value is constant in all entries(rows) or if the variance of the data is zero then it shows this error. Just remove the column with zero variance and try it again

Method:2

If you can't remove this then generate noise data with variance 1(make sure this value should not affect the output) and replace the value with the noise data

saravanan saminathan

Posted 2018-09-13T12:44:06.150

Reputation: 117

Where might you not be able to remove a column (a variable) from a model, but adding noise is fine? – n1k31t4 – 2018-11-23T00:46:58.327

Yes.During calculation it will allocate least probability to the noise column – saravanan saminathan – 2018-11-23T06:34:13.680