简体   繁体   English

R 中 MICE 包的被动插补语法

[英]passive imputation syntax for MICE package in R

Suppose we have a numeric variable age , which is sometimes missing.假设我们有一个数字变量age ,它有时会丢失。 In using it to predict other variables, we want to allow for non-linearity, so we create age_factor .在使用它来预测其他变量时,我们希望允许非线性,因此我们创建了age_factor We should impute age_factor passively: https://www.gerkovink.com/miceVignettes/Passive_Post_processing/Passive_imputation_post_processing.html我们应该被动age_factorhttps : age_factor

But how would the syntax work ?但是语法如何工作? Something like:就像是:

method["age_factor"] <- "~ 18_34 if age <= 34,
                           35_49 if age <= 49... "

Thank you !谢谢 !

Why do you can't do it like that:为什么你不能这样做:

age_factor <- function(x){
     if (x <= 34) {
         y <- "18_34"
     } else {
         y <- "35_49"
     }
return(y)    
}

age_factor(32)
[1] "18_34"

Using syntax which is close to what you have written:使用与您所写内容接近的语法:

method["age_factor"] <- "~ ifelse(is.na(age), NA,  
   ifelse(age < 20, 'under 20', 
   ifelse(age < 25, '20 to 24', 
   ifelse(age < 35, '25 to 34',
   '35 and over'))))"

NB I have included NA in the possible results because imputations can sometimes return NA.注意,我在可能的结果中包含了 NA,因为插补有时会返回 NA。 (Also, you need to enclose your factor level names in single quotes, because the expression as a whole is enclosed in double quotes, otherwise R will think your code for age_factor ends prematurely.) (此外,您需要将因子级别名称用引号括起来,因为整个表达式都用双引号括起来,否则 R 会认为您的 age_factor 代码过早结束。)

However--I would avoid ifelse() in this instance, preferring cutr::smart_cut() for its simplicity and resistance to hard-to-spot semantic errors:然而——在这种情况下,我会避免 ifelse(),更喜欢 cutr::smart_cut() ,因为它的简单性和对难以发现的语义错误的抵抗力:

require(cutr)
cuts <- c(-Inf, 20, 25, 35, Inf)
imp$age_factor <- with(imp, smart_cut(age, cuts, 
    labels = c('under 20', '20 to 24', '25 to 34', '35 and over'), output = "factor"))

NB the argument output = "factor" will give you an unordered factor;注意参数 output = "factor" 会给你一个无序因子; you can set this to other values eg ordered factor, integer, etc.您可以将其设置为其他值,例如有序因子、整数等。

Happy imputing!快乐归化! :-) :-)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM