【问题标题】:2x2 contingency table constructing in sql在sql中构建2x2列联表
【发布时间】:2017-02-13 01:05:49
【问题描述】:

我正在尝试构建一个 2x2 列联表,如下面的链接所述:

Ad hoc 2x2 contingency tables SQL Server 2008 (试图理解代码,但无法理解)

创建一个循环来构造对,如 C1,C1 C1,C2 C2,C1 C2,C2。 (笛卡尔积)

这些对作为参数提供给 sql 代码。对于这个例子,我已经给了 sql 代码一对 --> C1,C1

当为不同的对构造它时,它们在 C1,C2 C2,C1 中是正确的(经过下面解释的一些修改)。在制作 C1,C1 或 C2,C2 对时,它会构建错误的列联表。

例如(表名是 alpha_occurence):

id   concept_uri   document_uri

1       C1      D1

2       C2      D1

C1,C1 对的 2x2 列联表应该从上面给定的表格中给出:

       C1     not C1
    C1  1     0
not C1  0     -

而是给出(经过一些修改):

       C1    not C1
    C1  0    1
not C1  1    -

请注意,我已经为非 C1 和非 C1 的值添加了 -。因为要计算使用了其他方法。

此 sql 代码用于检索值:

SELECT count(*) AS total FROM  
(SELECT document_uri,count(DISTINCT concept_uri) AS count_conc FROM mydb.alpha_occurence 
WHERE concept_uri IN ('C1','C1') 
GROUP BY document_uri 
HAVING count_conc >=2 ) 
AS amount_of_concept_co_occurence #value of both X and Y

UNION ALL 

SELECT count(*) AS total FROM 
(SELECT concept_uri,document_uri FROM mydb.alpha_occurence
WHERE concept_uri IN ('C1'))
AS only_concept_A #value of Only X not Y

UNION ALL 

SELECT count(*) AS total FROM
(SELECT concept_uri,document_uri FROM mydb.alpha_occurence 
WHERE concept_uri IN ('C1'))
AS only_concept_B #value of Not X only Y

在检索到值后,一个小脚本会在这些值上运行以更正它们。 完成以下操作:

To get Only X and not Y            = only_concept_A - amount_of_concept_co_occurence 
To get Not X and Only Y            = Only_concept_B - amount_of_concept_co_occurence
To get the value of neither X or Y = total # of documents (which is not given here as the sample data only has data of which concept occurce in which document) - (amount_of_concept_co_occurence + Only X and not Y + Not X and Only Y) 

【问题讨论】:

  • 请编辑您的问题和数据样本。您有诸如 not in ('c1', 'c1') 之类的无意义结构并提及“A”和“B”,但在查询中没有此类引用。
  • 我已经编辑了我的问题,希望我让它更容易理解
  • 如果您只显示一些示例数据和您想要的结果,您的问题会更清楚。我知道什么是列联表。我不知道您的数据是什么样的。您也可以考虑关闭此问题并询问另一个可以更好地传达您要使用数据完成什么的问题。

标签: mysql sql contingency


【解决方案1】:

我用过这个脚本

select concept_uri, document_uri, count(*) as count 
from table
group by concept_uri, document_uri

他们准备好了..

【讨论】:

  • 欢迎来到 Stack Overflow!虽然您可能已经解决了这个用户的问题,但纯代码的答案对于将来遇到这个问题的用户来说并不是很有帮助。请编辑您的答案以解释为什么您的代码解决了原始问题。
猜你喜欢
  • 2012-08-22
  • 1970-01-01
  • 2021-05-15
  • 2022-01-08
  • 2018-02-14
  • 2020-07-04
  • 2020-03-06
  • 1970-01-01
  • 2017-03-02
相关资源
最近更新 更多