Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add_constant() in statsmodels not working

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 ?

like image 862
florian Avatar asked Apr 10 '16 16:04

florian


1 Answers

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

like image 185
florian Avatar answered Sep 27 '22 17:09

florian