首页 文章

使用鼠标在data.frame的所有列中输入值

提问于
浏览
0

我试图使用鼠标使用线性模型来估算值 . 我对老鼠的理解是它遍历行 . 对于具有NA的列,它使用所有其他列作为预测变量,拟合模型,然后从该模型中采样以填充NA . 这是一个我生成一些数据的例子,而不是使用ampute引入缺失的数据 .

n <- 100
    xx<-data.frame(x = 1:n + rnorm(n,0,0.1), y =(1:n)*2 + rnorm(n,0,1))
    head(xx)
    res <- (ampute(xx))
    head(res$amp)

丢失的数据如下:

x         y
   1       NA  3.887147
   2 2.157168        NA
   3 2.965164  6.639856
   4 3.848165  8.720441
   5       NA 11.167439
   6       NA 12.835415

然后我试图估算丢失的数据:

mic <- mice(res$amp,diagnostics = FALSE )

我希望那时有非,但是其中一个列中总有NA .

colSums(is.na(complete(mic,1)))

而在这两者中哪一个是相当随机的 .

通过运行上面的代码我得到:

> colSums(is.na(complete(mic,1)))
  x  y 
  0 30

但是也 :

> colSums(is.na(complete(mic,1)))
  x  y 
 33  0

1 回答

  • 0

    我试图运行您的代码并最终遇到相同类型的问题:

    library(mice)
    n <- 100
    xx<-data.frame(x = 1:n + rnorm(n,0,0.1), y =(1:n)*2 + rnorm(n,0,1))
    head(xx)
    res <- (ampute(xx))
    head(res$amp)
    

    如果您从 mice 调用中查看 summary ,那么您会看到出现问题 . 我的数据给出了

    tempData <- mice(res$amp,m=5,maxit=50,seed=500)
    summary(tempData)
    Multiply imputed data set
    Call:
    mice(data = res$amp, m = 5, maxit = 50, seed = 500)
    Number of multiple imputations:  5
    Missing cells per column:
     x  y 
    21 23 
    Imputation methods:
        x     y 
    "pmm" "pmm" 
    VisitSequence:
    x 
    1 
    PredictorMatrix:
       x  y
    x  0  0
    y  0  0
    Random generator seed value:  500
    

    这里有两个指标 . 一个是 VisitSequence ,表明只访问了第一列, x ,而不是列 y . 此外, PreditorMatrix 仅在非对角线中包含零,因此没有预测变量使用来自任何其他预测变量的信息 .

    问题在于您的模拟数据,因为两列太过共线,this detailed answer中给出了类似的解决方案 . 因为 y 列基本上是 x 列值的两倍,所以它会从分析中静默地丢弃 .

    尝试模拟几乎不完全线性的数据,它将起作用 . 例如二次关系

    n <- 100
    xx<-data.frame(x = 1:n + rnorm(n,0,0.1), y =(1:n)**2 + rnorm(n,0,1))
    head(xx)
    res <- (ampute(xx))
    head(res$amp)
    

相关问题