【发布时间】:2018-03-03 16:54:53
【问题描述】:
我正在尝试使用 python 和 stata 构建多项 logit 模型。我的数据如下:
ses_type prog_type read write math prog ses
0 low Diploma 39.2 40.2 46.2 0 0
1 middle general 39.2 38.2 46.2 1 1
2 high Diploma 44.5 44.5 49.5 0 2
3 low Diploma 43.0 43.0 48.0 0 0
4 middle Diploma 44.5 36.5 45.5 0 1
5 high general 47.3 41.3 47.3 1 2
我正在尝试使用 ses 读写和数学来预测 prog。其中 ses 代表社会经济地位并且是一个名义变量,因此我使用以下命令在 stata 中创建了我的模型:
mlogit prog i.ses read write math, base(2)
Stata输出如下:
Iteration 0: log likelihood = -204.09667
Iteration 1: log likelihood = -171.90258
Iteration 2: log likelihood = -170.13513
Iteration 3: log likelihood = -170.11071
Iteration 4: log likelihood = -170.1107
Multinomial logistic regression Number of obs = 200
LR chi2(10) = 67.97
Prob > chi2 = 0.0000
Log likelihood = -170.1107 Pseudo R2 = 0.1665
------------------------------------------------------------------------------
prog | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
0 |
ses |
1 | .6197969 .5059335 1.23 0.221 -.3718146 1.611408
2 | -.5131952 .6280601 -0.82 0.414 -1.74417 .7177799
|
read | -.0405302 .0289314 -1.40 0.161 -.0972346 .0161742
write | -.0459711 .0270153 -1.70 0.089 -.09892 .0069779
math | -.0990497 .0331576 -2.99 0.003 -.1640373 -.0340621
_cons | 9.544131 1.738404 5.49 0.000 6.136921 12.95134
-------------+----------------------------------------------------------------
1 |
ses |
1 | -.3350861 .4607246 -0.73 0.467 -1.23809 .5679176
2 | -.8687013 .5363968 -1.62 0.105 -1.92002 .182617
|
read | -.0226249 .0264534 -0.86 0.392 -.0744726 .0292228
write | -.011618 .0266782 -0.44 0.663 -.0639063 .0406703
math | -.0591301 .0299996 -1.97 0.049 -.1179283 -.000332
_cons | 5.041193 1.524174 3.31 0.001 2.053866 8.028519
-------------+----------------------------------------------------------------
2 | (base outcome)
------------------------------------------------------------------------------
我尝试在 python 中使用 scikit learn 模块复制相同的结果。以下是代码:
data = pd.read_csv("C://Users/Furqan/Desktop/random_data.csv")
train_x = np.array(data[['read', 'write', 'math','ses ']])
train_y = np.array(data['prog'])
mul_lr = linear_model.LogisticRegression(multi_class='multinomial',
solver='newton-cg').fit(train_x, train_y)
print(mul_lr.intercept_)
print(mul_lr.coef_)
输出值(截距和系数)如下:
[ 4.76438772 0.19347405 -4.95786177]
[[-0.01735513 -0.02731273 -0.04463257 0.01721334]
[-0.00319366 0.00783135 -0.00689664 -0.24480926]
[ 0.02054879 0.01948137 0.05152921 0.22759592]]
结果是不同的值。
我的第一个问题是为什么结果往往不同?
我的第二个问题是,在名义预测变量的情况下,我们如何指示 python ses 是 指示变量?
编辑:
Link 到数据文件
【问题讨论】:
-
这是倒退的。除非正确地指示了有关 ses 的 python,否则此处预计会出现不同的结果。你的第二个问题仍然存在。
-
如果你想模仿Stata的输出,你需要为
ses==1和ses==2构造虚拟代码。参见,例如,pandas.pydata.org/pandas-docs/stable/generated/… -
使用过的 sklearn.preprocessing.LabelEncoder 最终得到了相同的结果。尝试使用 pandas get_dummies,但它为三个类别创建了三个单独的列,而不是创建一个用 ses==1 ses==2 编码的列。仍在努力复制相同的结果。
-
您附加的数据的编码方式不同:ses 具有“低”、“中”和“高”三个级别。请提供相同格式的数据和代码!
标签: python scikit-learn statistics stata mlogit