# Heckman procedure on a complex survey data in R

Any idea on how to run the Heckman correction on a complex survey data in R?

I've tried doing it manually, but no success so far...

For the first stage I ran the svyglm() function from the survey package which works well and I was able to estimate the probit model . However, for the second stage I'm having trouble including the predicted inverse Mills ratio (λ) in the svyglm() function.

are you looking for something like the two-step approach described here?

https://m-clark.github.io/models-by-example/heckman-selection.html#two-step-approach

library(survey)

set.seed(123456)

N = 10000
educ = sample(1:16, N, replace = TRUE)
age  = sample(18:64, N, replace = TRUE)
wgt = sample( 1:10, N, replace = TRUE)

covmat = matrix(c(.46^2, .25*.46, .25*.46, 1), ncol = 2)
errors = mvtnorm::rmvnorm(N, sigma = covmat)
z = rnorm(N)
e = errors[, 1]
v = errors[, 2]

wearnl = 4.49 + .08 * educ + .012 * age + e

d_star = -1.5 + 0.15 * educ + 0.01 * age + 0.15 * z + v

observed_index  = d_star > 0

d_df = data.frame(wearnl, educ, age, z, observed_index)

d_surv <- svydesign( ~ 1 , data = d_df , weights = ~ wgt )

probit = svyglm(observed_index ~ educ + age + z,
design   = d_surv,

probit_lp = predict(probit)

d_surv <- update( d_surv , mills0 = dnorm(probit_lp)/pnorm(probit_lp))

lm_select <-
svyglm(
wearnl ~ educ + age + mills0,
design = subset( d_surv , observed_index )
)

summary(lm_select)

• Exactly! But for complex survey data... which estimates the first and second stage as svyglm() does (i.e. considering the sample design). The functions in the sampleSelection package which do the Heckman procedure automatically only support 'data' as either environment, data.frame, or list. However, mine is a tbl_svysurvey.design2survey.design. I tried doing the procedure manually, calculating the invMillsRatio() and adding it as a indep var in the second stage. However, I haven't been able to add the IMR to survey design as it has less rows than the original data.
– RCM
Commented Sep 28, 2021 at 15:20
• @RCM i added some code..do you want the update() function to add that column to the dataset? Commented Sep 29, 2021 at 16:24
• Wow! Thanks! Yes. The update function is pretty useful.
– RCM
Commented Sep 30, 2021 at 17:28