用向量逐行替换缺失值

时间:2017-03-11 07:54:25

标签: r missing-data imputation

我正在努力在数据集中输入缺失值。我有一个预测模型,可以生成值来估算缺失。当我使用dfm [is.na(dfm)]< -impute进行插补时,它会以列方式输入值。但是我需要对行进行估算,因此我转换了数据矩阵。我的问题是,有没有一种优雅的方法可以做到这一点而无需转置矩阵?这是一个带有可重复示例的rcode。

      set.seed(1)
      r=5 
      c=4
      df<-matrix(runif(r*c), ncol=c) 
      df
           [,1]       [,2]      [,3]      [,4]
 [1,] 0.2655087 0.89838968 0.2059746 0.4976992
 [2,] 0.3721239 0.94467527 0.1765568 0.7176185
 [3,] 0.5728534 0.66079779 0.6870228 0.9919061
 [4,] 0.9082078 0.62911404 0.3841037 0.3800352
 [5,] 0.2016819 0.06178627 0.7698414 0.7774452

  d=dim(df)
  p=0.30 

  #### generate missing data matrix by replacing some values by NAs
  dfm<-df
  dfm[matrix(rbinom(prod(d), size=1,prob=p)==1,nrow=d[1])]<-NA
  dfm
           [,1]       [,2]      [,3]      [,4]
 [1,]        NA 0.89838968 0.2059746 0.4976992
 [2,] 0.3721239 0.94467527 0.1765568        NA
 [3,] 0.5728534 0.66079779 0.6870228 0.9919061
 [4,] 0.9082078         NA 0.3841037        NA
 [5,] 0.2016819 0.06178627        NA 0.7774452

  # generate values to impute the missing
 impute<-rgamma(sum(is.na(dfm)),shape=1,scale=0.5)
  impute
 [1] 0.6804725 0.6029941 0.2770577 0.6035013 0.7812393

 #imputes columnwise
  dfm[is.na(dfm)]<-impute
   dfm
          [,1]       [,2]      [,3]      [,4]
 [1,] 0.6804725 0.89838968 0.2059746 0.4976992
 [2,] 0.3721239 0.94467527 0.1765568 0.6035013
 [3,] 0.5728534 0.66079779 0.6870228 0.9919061
 [4,] 0.9082078 0.60299409 0.3841037 0.7812393
 [5,] 0.2016819 0.06178627 0.2770577 0.7774452

 #impute rowwise
       tdfm<-t(dfm)
  tdfm[is.na(tdfm)]<-impute
  tdfm
           [,1]      [,2]      [,3]      [,4]       [,5]
 [1,] 0.6804725 0.3721239 0.5728534 0.9082078 0.20168193
 [2,] 0.8983897 0.9446753 0.6607978 0.2770577 0.06178627
 [3,] 0.2059746 0.1765568 0.6870228 0.3841037 0.78123933
 [4,] 0.4976992 0.6029941 0.9919061 0.6035013 0.77744522

       dfm.fill<-t(tdfm)
       dfm.fill

           [,1]       [,2]      [,3]      [,4]
   [1,] 0.6804725 0.89838968 0.2059746 0.4976992
   [2,] 0.3721239 0.94467527 0.1765568 0.6029941
   [3,] 0.5728534 0.66079779 0.6870228 0.9919061
   [4,] 0.9082078 0.27705769 0.3841037 0.6035013
   [5,] 0.2016819 0.06178627 0.7812393 0.7774452

1 个答案:

答案 0 :(得分:4)

使用which代替arr.ind,以便您可以先按行排序。

示例:

test1 <- matrix(1:12, 3, 4, byrow = TRUE)
test1[c(1, 3, 8, 6, 10)] <- NA
test2 <- test1

impute <- c(-1, -4, -7, -9, -10)

## What you are currently doing--column-wise
test1[is.na(test1)] <- impute
test1
#      [,1] [,2] [,3] [,4]
# [1,]   -1    2    3  -10
# [2,]    5    6   -9    8
# [3,]   -4   -7   11   12

## What it sounds like you want--row-wise
nas <- which(is.na(test2), arr.ind = TRUE)
test2[nas[order(nas[, "row"]), ]] <- impute
test2
#      [,1] [,2] [,3] [,4]
# [1,]   -1    2    3   -4
# [2,]    5    6   -7    8
# [3,]   -9  -10   11   12