![](/img/trans.png)
[英]I am using Naive Bayes in R. I have the titanic set, but predict() function produces error
[英]New to R. I have a set of codes from Stata that I wanted to reproduce in R
我在 Stata 中编写了以下代码,我想在 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)
关于如何做的任何见解? 我是 R 的新用户。
数据 - 示例:
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"))
像那样的东西。 我们需要评估 model 并执行插补过程。 在选择方程式中,您必须使用适当的 I() 或新变量
您的其他问题在 R 中实施时如何更正赫克曼选择模型中的此错误?
因此,尽管 0 或 sth 其他情况下,您的工资可能没有 NA,然后使用 eq,如: I(wage != 0)
讨论后编辑:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.