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

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

**ValueError: x already contains a constant**

Is there a way to fix this?

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

2 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.

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


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


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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.