add_constant() in statsmodels not working
Asked Answered
C

1

19

I try to use the add_constant() function with an array of dataset. At index 59 it works (the column is created) but at index 60 it isn't created. Initially, testmat[59] returns a shape of (24, 54) and testmat[60] a shape of (9, 54). Hereafter is what I get when I run the add_constant function:

In: Xnew = sm.add_constant(testmat[59])
Out:
     const  TRYSIL_PO   TRYSIL_TA
6142    1   985.7       271.65
6143    1   984.9       271.85

In: Xnew = sm.add_constant(testmat[60]):
Out:
     TRYSIL_PO  TRYSIL_TA
6166    983.6   272.75
6167    983.1   272.85

I already checked other sources but it seems that this one hasn't been addressed. Would you have an explanation ?

Clova answered 10/4, 2016 at 16:41 Comment(0)
C
44

It seems that add_constant() doesn't works if there is already a column with variance=0, i.e. a column with all identical values. It is the case in my dataset (not mentioned in the example above (54 columns)).

The solution is to add has_constant option in the add_constant() function, like this:

sm.add_constant(testmat[60], has_constant='add')

More information: http://www.statsmodels.org/dev/generated/statsmodels.tools.tools.add_constant.html

Clova answered 10/4, 2016 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.