为了获得概率,您可以对具有相同条件的人和过滤具有相同条件计数的组。
假设有 n 个不同的条件,并且对于每个条件:1 表示一个人患有某种疾病,否则为 0:
no_of_cond <- ncol(df) # number of conditions
为每个人评估condition_set 和condition_count:
df$condition_set <- apply(df, 1, function(x) {if (sum(x)>0) { paste(names(which(x == 1)),collapse = ", ")
} else {return(NA)}
})
df$condition_count <- rowSums(df[,1:no_of_cond])
将具有相同条件的人分组并过滤具有相同condition_count的组:
library(dplyr)
case_count_df <- function(n) { df_temp <- df %>% group_by_all() %>%
summarise(ppl_count= n()) %>%
filter(condition_count == n)
return (df_temp) }
对有2个条件的人的总结,其他类似的可以得到:
df_2_cond <- case_count_df(2) %>% ungroup()
df_2_cond$prob <- df_2_cond$ppl_count/sum(df_2_cond$ppl_count)
plot(as.factor(df_2_cond$condition_set), df_2_cond$prob, xlab = 'condition_set',
ylab = 'probability', main = "People with 2 conditions")
虚拟数据:
df <- data.frame(expand.grid( a = rep(c(0,1),2), b = rep(0,3),
c = c(0,1,0), d = c(0,0,1) ))
PS:以上都是基本聚合。对于任何统计测试,交叉验证的推论将是一个更好的论坛。