# VAR model ValueError: x already contains a constant

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?

• Is this the VAR object from the statsmodels package for python? Why are you passing 1 to fit(), because you only want 1 lag? Sep 13, 2018 at 13:51
• Yes. Also with lag=2, I get the same result.
– Abs
Sep 13, 2018 at 13:55

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.

• 10% of the dataframe I come across has a similar feature.
– Abs
Sep 13, 2018 at 13:34
• Similar or constant? Sep 13, 2018 at 13:49
• up to 2 of the variables is a constant .
– Abs
Sep 13, 2018 at 13:53
• A 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. Sep 13, 2018 at 13:54

### 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

• Where might you not be able to remove a column (a variable) from a model, but adding noise is fine? Nov 23, 2018 at 0:46
• Yes.During calculation it will allocate least probability to the noise column Nov 23, 2018 at 6:34