如何估算 NA 值或创建所有可能的组合?

问题描述 投票:0回答:1
data.frame(
  group = c("a", "b", "c", "d", "e", "total"),
  count = c(NA, NA, 10, 21, 49, 85)
)
> 
  group count
1     a    NA
2     b    NA
3     c    10
4     d    21
5     e    49
6   total  85

鉴于上述数据框,我如何估算 NA 值,以便

  1. a-e
    比赛总分
    total
  2. 每个估算的 NA 是 <10?

解决方案可以是生成所有可能性的嵌套数据框,或者用分布或……替换

NA
......谢谢!

r imputation
1个回答
0
投票

一种方法是使用

RcppAlgos::permuteGeneral()
生成总和为目标的所有排列。从那里,可以随机选择一组来替换
NA
s。

library(RcppAlgos)

# Count NAs 
n <- sum(is.na(dat$count))

# Find sum target
target <- dat$count[dat$group == "total"] - sum(dat$count[dat$group != "total"], na.rm = TRUE)

# Generate permutations of n values that sum to target
res <- permuteGeneral(
  0:min(9, target),  # Ensure all values are less than 10
  n,
  repetition = TRUE,
  constraintFun = "sum",
  comparisonFun = "==",
  limitConstraints = target
  )

# Permutations that meet the constraints:
res
     [,1] [,2]
[1,]    0    5
[2,]    5    0
[3,]    1    4
[4,]    4    1
[5,]    2    3
[6,]    3    2

# Replace NA values with random permutation
dat$count[is.na(dat$count)] <- res[sample(nrow(res), 1), ]

dat
  group count
1     a     3
2     b     2
3     c    10
4     d    21
5     e    49
6 total    85
© www.soinside.com 2019 - 2024. All rights reserved.