Last answered:

07 Jun 2022

Posted on:

06 Jun 2022

1

Resolved: Error with sm.add_constant()

Hi,

I get the following error when coding the example from the video in my Jupyter Notebook:

image

Is there something I have done wrong? I get it right when performing simple linear regression but I get the error with multiple linear regression. I have tried with Jupyter Notebook from Google Colab and also in my usual VIM editor. I get the error with both.

Thank you in advance,

4 answers ( 2 marked as helpful)
Instructor
Posted on:

06 Jun 2022

0

Hey María,

Thank you for your question!

Unfortunately, I don't immediately see where the trouble comes from. Is this the same Jupyter notebook as the one in the resources?

If you are using the same notebook, then I can suggest the following:

1. Try to restart the notebook and run the cells consecutively.
2. If you still get the error, could you please add and run the following line of code:
image.png
Do you get the same output as me?
3. Lastly, could you tell me which version of statsmodels you are using?

Looking forward to your answer!

Kind regards,
365 Hristina

Posted on:

07 Jun 2022

0

Hi Hristina,

Thank you very much for your answer. I am not using the same Jupyter notebook. I have opened a new one in Google Colab and copied the code. I don't have it installed. Could this be the problem? I have also made a copy with VIM and run it from the Linux terminal and I get the exact same error when running the multiple linear regression. No problem at all with he simple one. Here you have my answers:

1. Done. I still get the error.
2. Yes, I get the same output >>> (numpy.int64, numpy.int64)
3. My version of "statsmodels" is 0.13.2.

Kind regards,

Instructor
Posted on:

07 Jun 2022

0

Hey María,

Thank you for your response!

I now realized where the problem is. The following line of code:

x1 = [['SAT','Rand 1,2,3']]

should be substituted with

x1 = data[['SAT','Rand 1,2,3']]

Notice there is the word data in front. Without it, the variable x1 becomes simply a list, containing another list, containing two strings :)

Hope this helps!

Kind regards,
365 Hristina

Posted on:

07 Jun 2022

0

Oh, it's true! Hahaha. Thank you very much. Today I couldn't understand why the rest of the exercises on multiple linear regression were working properly. :) Sorry for the inconvenience.

Kind regards,

Submit an answer