Multiple linear regression with quantitive predictor

Hi :wave:t3:

I’m trying to fit multiple linear regression model with a quantitive predictor (has multiple values). Now in this exercise it was a matter of plugging the values in the linear regression model and plotting it.

Whenever I try to do the same in my project, I get the following error:

sns.lmplot(x= 'children', y= 'charges', data=insurance, fit_reg= False)
plt.plot(insurance.charges, model.params[0] + model.params[1]*0)
plt.plot(insurance.charges, model.params[0] + model.params[1]*1)
plt.show()

ValueError: x and y must have same first dimension, but have shapes (1338,) and (1,)

I tried converting datatypes and it didn’t work. Any suggestions?

I’m assuming the error is with one of the plt.plot lines. In the form you’ve provided them it expects plt.plot(xsequence, ysequence). Unfortunately they appear to be of different lengths so this fails.

I’m guessing this expression evaluates to a single value- model.params[0] + model.params[1]*0.

Double check your data and have a look at the call signature for that function-
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot

you are right, the second expression is equivalent to y, and as the link points out x and y must have the same dimension/size -which is why I kept having ValueError-

I wasn’t able to plot lines using one predictor, I had to include a second predictor to match y size to x size (just as in exercise)

After all it didn’t look like a helpful visualization to include in the project :upside_down_face: but thank you so much for your insightful reply!

1 Like