简体   繁体   English

R 的新手。我有一组来自 Stata 的代码,我想在 R 中重现

[英]New to R. I have a set of codes from Stata that I wanted to reproduce in R

I have programmed the following code in 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)

Any insights on how to do it?关于如何做的任何见解? I'm new to R.我是 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.我们需要评估 model 并执行插补过程。 in selection equation you have to use proper I() or a new variable在选择方程式中,您必须使用适当的 I() 或新变量

your other question How do I correct this Error in Heckman selectionmodel when implementing in R?您的其他问题在 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)因此,尽管 0 或 sth 其他情况下,您的工资可能没有 NA,然后使用 eq,如: 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在R中使用朴素贝叶斯(Naive Bayes)。 - I am using Naive Bayes in R. I have the titanic set, but predict() function produces error R.处理导入的Stata文件中的日期和宽格式 - R. Handling dates and wide format from an imported Stata file R的新手。我正在尝试从mlbench软件包的Glass $ Fe中将0.00值添加.01。 - New to R. I am trying add .01 the 0.00 values in the Glass$Fe from the mlbench package 我是 r 的新手。 如何将数据框变量值从数字转换为名称? 请参阅下面的代码 - I am new to r. How can I convert a data frame variable value from a number to a name? See code below R. 如何创建一个新列,根据 R 中的另一列返回 i - R. How to create a new column, returning i based on another column in R 在R,二项GLM中重现Stata代码 - Reproduce Stata code in R, binomial GLM 尝试在 R 中使用 plm 在 stata 中重现 xtreg - Trying to reproduce xtreg in stata with plm in R 我有 2 个 csv 文件,我想将它们合并,然后使用 R。 我怎样才能做到这一点? - I have 2 csv files which i would like to combine then using R. how can i achieve this? 我在 R 上有 2 个图表。它们有不同的 x 轴,但趋势概况相似。 我如何将它们覆盖在 r 上? - I have 2 graphs on R. They have different x axis, but similar trend profile. how do I overlay them on r? 我是 r 的新手。我试图通过使用 for 循环使我的代码不那么复杂 - I am new to r. I am trying to make my code less complicated by using a for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM