I have programmed the following code in Stata that I wanted to reproduce in R.
heckman wage i.age i.profstat i.edlevel, select(i.profstat i.edlevel ur i.ethnicity mstatus) twostep first mills(imr)
predict simwage, xb
replace wage = simwage if missing(wage) & !missing(profstat)
Any insights on how to do it? I'm new to R.
Data - sample:
data = structure(list(ethnicity = c(6, 1, 2, 1, 7, 2, 5, 1, 2, 2, 4, 5, 4),
age = factor(c(1, 1, 2, 2, 3, 1, 4, 1, 1, 3, 1, 3, 4)),
ur = c(1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0),
edlevel = c(2, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 1, 1),
mstatus = c(1, 3, 3, 3, 2, 1, 2, 3, 3, 1, 1, 6, 6),
profstat = factor(c(4, 4, 2, NA, 1, NA, NA, 2, 2, NA, 2, 1, NA)),
income = c(10, NA, 9, NA, 10, NA, NA, 4, 4, NA, 8, 8, 4),
wage = c(1794, NA, 1483, NA, 1529, NA, NA, 415, 550, NA, 1169, 1096, 543)),
row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"))
Something like that. We need asses the model and perform a imputation process. in selection equation you have to use proper I() or a new variable
your other question How do I correct this Error in Heckman selectionmodel when implementing in R?
So it is possible that you do not have NA in wage although 0 or sth else then use eq like: I(wage != 0)
Edit after discussion:
require(sampleSelection)
#Hecman model
heckman <- selection(selection = I(!is.na(wage)) ~ ur + mstatus, outcome = wage ~ profstat,
data = data, method = "2step")
#prediction
simwage <- predict(heckman, data)
data$wage_p <- simwage
# imputation - replace NA wage with a new assesment
data$wage_org <- data$wage
data$wage[is.na(data$wage) & !is.na(data$profstat)] <- simwage[is.na(data$wage) & !is.na(data$profstat)]
head(data)
# A tibble: 6 x 10
ethnicity age ur edlevel mstatus profstat income wage wage_p wage_org
<dbl> <fct> <dbl> <dbl> <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
1 6 1 1 2 1 4 10 1794 767. 1794
2 1 1 1 2 3 4 NA 767. 767. NA
3 2 2 1 3 3 2 9 1483 305. 1483
4 1 2 1 1 3 NA NA NA NA NA
5 7 3 1 2 2 1 10 1529 792. 1529
6 2 1 1 2 1 NA NA NA NA NA
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.