【问题标题】:Calculate the probability of people that have x "conditions" have the same conditions in R?计算具有x个“条件”的人在R中具有相同条件的概率?
【发布时间】:2018-11-24 11:13:47
【问题描述】:

我试图理解这个理论以及这个术语的名称。我想用 R 编写代码。

在数据集中有 n 个人数,所有这些人最多可以有 z 个条件。

例如,我想知道有 3 个条件的人,他们最可能有哪些条件组。人 A 有条件 {1,2,3},人 B 有条件 {4,7,8},人 C 有条件 {2,5,8},我想展示他们最可能的条件集群本来可以的。

我希望将此问题扩展到具有 n 个条件的人,因此具有 4 个条件、5 个等条件的人。

【问题讨论】:

  • 这里似乎没有任何特定于编程的问题。似乎更适合Cross Validated,因为这里有关于统计的问题。
  • @MrFlick 感谢您的提醒,我会在那里发帖。
  • 如果我理解正确的话。下面的代码应该足以满足聚合要求。

标签: r statistics cluster-analysis analysis


【解决方案1】:

为了获得概率,您可以具有相同条件的人过滤具有相同条件计数的组

假设有 n 个不同的条件,并且对于每个条件:1 表示一个人患有某种疾病,否则为 0:

no_of_cond <- ncol(df)                                       # number of conditions

为每个人评估condition_setcondition_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:以上都是基本聚合。对于任何统计测试,交叉验证的推论将是一个更好的论坛。

【讨论】:

    【解决方案2】:

    您可能正在寻找频繁项集

    在您的情况下,项目是条件,因此频繁出现条件集。

    【讨论】:

    • 谢谢,你说得对。我最终使用了 aRulesaRulesViz 包来实现我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多