想试算一个是否有面试和是否获得offer的二阶段的模型,考虑到内生性问题使用了Heckman的分析方法,单独的两个模型都可以计算出来,但是在R中使用SampleSelection包联立两个模型时,无法正常运行,代码如下
heckman <- selection(selection = q21_0 ~q2_0+q3_0+q4_0+q8+q19_1_1+q20_1_1+I(q19_1_1^2)+I(q20_1_1^2)+q22_4, outcome = q23_0 ~q2_0+q3_0+q4_0+q8q21_1_1+I(q21_1_1^2)+q22_4,
data = subset(X0312_need_data_only,q9_0==1), method = "2step")
Error: Can't combine
..1<character> and
..2<double>.Run
rlang::last_error()to see where the error occurred.
参考的学习网站是这个
https://stackoverflow.com/questions/39069250/heckman-selection-model-in-r-manually
同时也尝试按照里面的方法手动计算,在第二步计算inverse Mills ratio的时候也出现了报错
seleqn1
<- glm(q21_0 q2_0+q3_0+q4_0+q8+q19_1_1+q20_1_1+I(q19_1_12)+I(q20_1_12)+q22_4, family=binomial(link="probit"), data=subset(X0312_need_data_only,q9_0==1))
#
Calculate inverse Mills ratio by hand ##X0312_need_data_only$IMR <- ``dnorm(seleqn1$linear.predictors)/pnorm(seleqn1$linear.predictors)`
Error:
Assigned data dnorm(seleqn1$linear.predictors)/pnorm(seleqn1$linear.predictors) must be compatible with existing data. x Existing data has 9844 rows. x Assigned data has 6990 rows. i Only vectors of size 1 are recycled. Run rlang::last_error() to see where the error occurred.