假设我们有3个简单矩阵如下:
X = pd.DataFrame(data=[1,0,1,1,0,0,0,0,0,0], columns=['X'])
y = pd.DataFrame(data=[0,0,0,0,0,0,0,0,0,1], columns=['y'])
w=[0.000024,0.134312,0.000024,0.000024,0.134312,0.134312,0.134312,0.134312,0.134312,0.015673]
我们要计算X上帽子矩阵的对角线。从here开始,公式为:
HAT = X(X' WX)^ - 1X'
这是我的python代码来计算它:
w = np.diag(w)
w = pd.DataFrame(data=w)
xtw = np.dot(X.T,w)
xtwx = np.dot(xtw,X)
xtwxi = np.linalg.inv(xtwx)
xtwxixtw = np.dot(xtwxi,xtw)
xxtwxixtw = np.dot(X,xtwxixtw)
print np.diag(xxtwxixtw)
print np.diag(xxtwxixtw).sum()
并返回:
[ 0.33333333 0. 0.33333333 0.33333333 0. 0. 0. 0. 0. 0. ]
1
作为参考说(" hii的总和等于p,参数的数量(回归系数包括截距")
然后我尝试了r
,这是我的r
代码:
print(lm.influence(lm(y ~ X - 1, weights = W), do.coef = FALSE)$hat)
print(sum(lm.influence(lm(y ~ X - 1, weights = W), do.coef = FALSE)$hat))
并返回:
[0.3333333 0.1634870 0.3333333 0.3333333 0.1634870 0.1634870 0.1634870 0.1634870 0.1634870 0.0190779]
2
我做错了吗?为什么会有不同的结果?