简体   繁体   English

使用R中的二项分布来估算缺失值

[英]impute missing values using binomial distribution in R

I have a column with some missing values (q1 = 9) , I would like to impute it based on q1=1(=yes) and q1 =2(=no) binomial distribution like the SPSS script below. 我有一个列有一些缺失值(q1 = 9),我想根据q1 = 1(= yes)和q1 = 2(= no)二项分布来推算它,如下面的SPSS脚本。 I couldn't find the R equivalent function 我找不到R等效函数

The SPSS code : SPSS代码:

SPSS version :IF  q_1 = 9 x=RV.BINOM(1,0.976) .
if q_1 = 9 & x=1 q_1 = 1.
if q_d1 = 9 & x=0 q_1 = 2.

The column summary is like this 列摘要是这样的

 q_1    n    percent
    1 5868   97.56%
    2  142   2.36%
    9    5   0.08%

You can generate the imputed values with sample . 您可以使用sample生成插补值。

Missing = which(q1 == 9)
q1[Missing] = sample(2, length(Missing), prob=c(0.976, 0.024))

What about this: 那这个呢:

library(tidyverse)

vect1 <- runif(10000, 0, 1)
vect1a <- case_when(
  vect1 < 0.9756 ~ 1,
  vect1 < 0.9756 + 0.0236 ~ 2,
  TRUE ~ 9)
df1 <- tibble(q1 = vect1a)

pct1 <- 0.9756 / (1 - 0.008)
df1a <-  df1 %>% 
  mutate(rand_id = runif(nrow(.), 0, 1),
         q1a = case_when(q1 < 9 ~ q1,
                         rand_id < pct1 ~ 1,
                         TRUE ~ 2))

Mice package can handle impute missing values as well, but not sure it uses binomial distribution. 鼠标包也可以处理错误值,但不确定它是否使用二项分布。

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

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