如何用 Python 中的 Survey Weights 计算 OLS 回归。
How calculate OLS regression with Survey Weights in Python.
我想用调查权重对调查数据进行线性回归。
调查数据来自欧盟,每个观察值都有权重。 (一位受访者为 .4,另一位受访者为 1.5。)
这个权重被描述为:
"The European Weight, variable 6, produces a representative sample of
the European Community as a whole when used in analysis. This variable
adjusts the size of each national sample according to each nation's
contribution to the population of the European Community."
我正在使用 sklearn 进行计算。
from sklearn import linear_model
regr = linear_model.LinearRegression()
regr.fit(X,y, sample_weight = weights)
X 是一个 pandas 数据框。 y 是 numpy.ndarray。 weights 是一个 pandas 系列。
我使用 'sample_weight' 是否正确,这是在 scikit 中处理调查权重的正确方法吗?
TL 博士;是的
这是一个非常简单的工作示例,
import numpy as np
import matplotlib.pylab as plt
from sklearn import linear_model
regr = linear_model.LinearRegression()
X = np.array([1, 2, 4]).reshape(-1, 1)
y = np.array([10, 20, 60]).reshape(-1, 1)
weights = np.array([1, 1, 1])
def weighted_lr(X, y, weights):
"""Quick function to run weighted linear regression and return a
plot and some predictions"""
regr.fit(X,y, sample_weight=weights)
y_pred = regr.predict(X)
plt.scatter(X, y)
plt.plot(X, y_pred)
plt.title('Weights: %s' % ', '.join(str(i) for i in weights))
plt.show()
return y_pred
y_pred = weighted_lr(X, y, weights)
print(y_pred)
weights = np.array([1000, 1000, 1])
y_pred = weighted_lr(X, y, weights)
print(y_pred)
[[ 7.14285714]
[ 24.28571429]
[ 58.57142857]]
[[ 9.96051333]
[ 20.05923001]
[ 40.25666338]]
在第一个具有偶数权重的线性回归模型中,我们看到该模型的行为与正常线性回归模型的预期一致。
然而,接下来,我们看到在第二个模型中,对最后一个值的权重较低,几乎忽略了最后一个值。此处的大部分训练已加权到其他两个值。
我想用调查权重对调查数据进行线性回归。
调查数据来自欧盟,每个观察值都有权重。 (一位受访者为 .4,另一位受访者为 1.5。)
这个权重被描述为:
"The European Weight, variable 6, produces a representative sample of the European Community as a whole when used in analysis. This variable adjusts the size of each national sample according to each nation's contribution to the population of the European Community."
我正在使用 sklearn 进行计算。
from sklearn import linear_model
regr = linear_model.LinearRegression()
regr.fit(X,y, sample_weight = weights)
X 是一个 pandas 数据框。 y 是 numpy.ndarray。 weights 是一个 pandas 系列。
我使用 'sample_weight' 是否正确,这是在 scikit 中处理调查权重的正确方法吗?
TL 博士;是的
这是一个非常简单的工作示例,
import numpy as np
import matplotlib.pylab as plt
from sklearn import linear_model
regr = linear_model.LinearRegression()
X = np.array([1, 2, 4]).reshape(-1, 1)
y = np.array([10, 20, 60]).reshape(-1, 1)
weights = np.array([1, 1, 1])
def weighted_lr(X, y, weights):
"""Quick function to run weighted linear regression and return a
plot and some predictions"""
regr.fit(X,y, sample_weight=weights)
y_pred = regr.predict(X)
plt.scatter(X, y)
plt.plot(X, y_pred)
plt.title('Weights: %s' % ', '.join(str(i) for i in weights))
plt.show()
return y_pred
y_pred = weighted_lr(X, y, weights)
print(y_pred)
weights = np.array([1000, 1000, 1])
y_pred = weighted_lr(X, y, weights)
print(y_pred)
[[ 7.14285714]
[ 24.28571429]
[ 58.57142857]]
[[ 9.96051333]
[ 20.05923001]
[ 40.25666338]]
在第一个具有偶数权重的线性回归模型中,我们看到该模型的行为与正常线性回归模型的预期一致。
然而,接下来,我们看到在第二个模型中,对最后一个值的权重较低,几乎忽略了最后一个值。此处的大部分训练已加权到其他两个值。