[英]Multiple Linear Regression - Determining Coefficients for 3 independent variables
I am struggling to find the coefficients for b1, b2 and b3.我正在努力寻找 b1、b2 和 b3 的系数。 My model has 3 independent variable x1, x2 and x3 and one dependent variable y.
我的模型有 3 个自变量 x1、x2 和 x3 以及一个因变量 y。
x1,x2,x3,y
89,4,3.84,7
66,1,3.19,5.4
78,3,3.78,6.6
111,6,3.89,7.4
44,1,3.57,4.8
77,3,3.57,6.4
80,3,3.03,7
66,2,3.51,5.6
109,5,3.54,7.3
76,3,3.25,6.4
I want to use the matrix method to find out the coefficients for b1, b2 and b3.我想使用矩阵方法找出 b1、b2 和 b3 的系数。 From the tutorial that I am following the value for b1 is 0.0141, b2 is 0.383 and b3 is -0.607.
从我遵循的教程中,b1 的值为 0.0141,b2 为 0.383,b3 为 -0.607。
I am not sure about how to achieve those values mentioned above, when I tried to inverse the matrix containing x1, x2, x3 values I am getting the below error.我不确定如何实现上面提到的那些值,当我尝试对包含 x1、x2、x3 值的矩阵求逆时,出现以下错误。
raise LinAlgError('Last 2 dimensions of the array must be square')
numpy.linalg.linalg.LinAlgError: Last 2 dimensions of the array must be square
Please someone help me solve this matrix so that I can get the desired values.请有人帮我解决这个矩阵,以便我可以获得所需的值。
In matrix form, the regression coefficients are given by在矩阵形式中,回归系数由下式给出
Where x
is your data matrix of predictors, and y
is a vector of outcome values其中
x
是您的预测变量数据矩阵, y
是结果值向量
In python (numpy), that looks something like this:在 python (numpy) 中,它看起来像这样:
import numpy as np
b = np.dot(x.T, x)
b = np.linalg.inv(b)
b = np.dot(b, x.T)
b = np.dot(b, y)
Using that on your data you get the following coefficients:在您的数据上使用它,您将获得以下系数:
0.0589514 , -0.25211869, 0.70097577
Those values don't match your expected output, and it's because the tutorial you're following must also be modelling an intercept.这些值与您的预期输出不匹配,这是因为您所遵循的教程也必须对拦截进行建模。 To do that we add a column of ones to the data matrix so it looks like this:
为此,我们将一列 1 添加到数据矩阵中,如下所示:
x.insert(loc=0, column='x0', value=np.ones(10))
x0 x1 x2 x3
0 1.0 89 4 3.84
1 1.0 66 1 3.19
2 1.0 78 3 3.78
3 1.0 111 6 3.89
4 1.0 44 1 3.57
5 1.0 77 3 3.57
6 1.0 80 3 3.03
7 1.0 66 2 3.51
8 1.0 109 5 3.54
9 1.0 76 3 3.25
Now we get the expected regression coefficients (plus an additional value for the intercept):现在我们得到预期的回归系数(加上截距的附加值):
6.21137766, 0.01412189, 0.38315024, -0.60655271
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.