【问题标题】:Using missForest in R with categorical variables在 R 中使用带有分类变量的 missForest
【发布时间】:2018-05-14 23:18:41
【问题描述】:

我正在尝试使用 missForest 包将丢失的数据归入一个相当大的数据集中。我的大多数变量都是分类的,有很多因素。当我运行missForest 时,它会估算十进制值,有时甚至是负值。显然,我做错了什么。下面是我的过程:

第一次尝试:直接输入预测数据。我将十进制值输入到我的数据集中。我知道 missForest 只需要矩阵,但我不确定如何强制它识别哪些列是因子。另一个帖子上有人推荐了虚拟编码,所以我接下来尝试了,结果相同。代码如下。

第二次尝试:对每个预测变量进行虚拟编码(非常耗时)然后运行它。

homt_sub_dummy<-homt_sub[c("Psyprob.yes", "Psyprob.no","SUB2.2.0", "SUB2.2.1", "SUB2.2.2", "SUB2.2.3", "SUB2.2.4", "SUB2.2.5", "SUB2.2.6", "SUB2.2.7","Freq1.1", "Freq1.2", "Freq1.3", "Freq1.4","FRSTUSE1.0", "FRSTUSE1.1", "FRSTUSE1.2", "FRSTUSE1.3", "FRSTUSE1.4", "FRSTUSE1.5", "FRSTUSE1.6","FRSTUSE1.7", "FRSTUSE1.8", "FRSTUSE1.9", "FRSTUSE1.10", "FRSTUSE1.11","Freq2.1", "Freq2.2", "Freq2.3", "Freq2.4","AGEcont","Gender_male", "Gender_female", "Race2.0", "Race2.1", "Race2.2", "Arrests.0", "Arrests.1", "Arrests.2")]

homt_dummy_matrix<-data.matrix(homt_sub_dummy, rownames.force = NA)
homt_dummp.imp <- missForest(homt_dummy_matrix, verbose= TRUE, maxiter = 3, ntree = 20)
homt_dummy.imp.df<-as.data.frame(homt_dummp.imp$ximp)
View(homt_dummy.imp.df)

This is a chunk of the data.frame i saved with the imputed values

任何帮助将不胜感激。我对插补很陌生。我想将 MICE 的结果与此进行比较,但我似乎无法让 missForest 工作!!!

【问题讨论】:

    标签: r statistics imputation


    【解决方案1】:

    您可以使用as.factor 函数来转换您想要的数据类别。例如

    cleveland_t <- transform(cleveland,V2=as.factor(V2),V3 = as.factor(V3),V6 = as.factor(V6),V7=as.factor(V7),V9 = as.factor(V9),V11=as.factor(V11),V12 = as.factor(V12),V13= as.factor(V13),v14=as.factor(V14))
    

    然后使用sapply查看类

    【讨论】:

      猜你喜欢
      • 2019-06-17
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2018-02-17
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多