皮尔逊相关SQL Server
我有两张桌子: ID、YRMO、计数 2013年12月1日,4 2014年1月1日,6 2014年2月1日,7 2014年1月2日,6 2014年2月2日,8 ID、YRMO、计数 2013年12月1日,10 2014年1月1日,8 2014年3月1日,12 2014年1月2日,6 2014年2月2日,10 我想找出每一组ID的pearson相关系数。大约有200多个不同的ID 皮尔逊相关性是两个变量X和Y之间线性相关性(相关性)的度量,给出的值介于+1和+1之间−1包括在内 更多信息可在此处找到:皮尔逊相关SQL Server,sql,sql-server,Sql,Sql Server,我有两张桌子: ID、YRMO、计数 2013年12月1日,4 2014年1月1日,6 2014年2月1日,7 2014年1月2日,6 2014年2月2日,8 ID、YRMO、计数 2013年12月1日,10 2014年1月1日,8 2014年3月1日,12 2014年1月2日,6 2014年2月2日,10 我想找出每一组ID的pearson相关系数。大约有200多个不同的ID 皮尔逊相关性是两个变量X和Y之间线性相关性(相关性)的度量,给出的值介于+1和+1之间−1包括在内 更多信息可在此处找
在计算相关部分计算皮尔逊相关系数;您需要首先计算
平均值
,然后计算标准偏差
,然后计算相关系数
,如下所述
1.计算平均数
2.计算标准差
3.最后Pearson相关系数
在您发布的数据中,哪些会导致
这里有小提琴演示吗
编辑:
有点精致。您可以使用下面的函数获取PCC,但我没有得到与您的完全相同的结果,而是得到ID=1
的0.99990000000
这对你来说可能是一个很好的切入点。您可以从此处进一步细化计算
create function calculate_PCC(@id int)
returns decimal(16,15)
as
begin
declare @mean numeric(16,5);
declare @stddev numeric(16,5);
declare @count numeric(16,5);
declare @pcc numeric(16,12);
declare @store numeric(16,7);
select @count = CONVERT(numeric(16,5), count(case when Id=@id then 1 end)) from tab1;
select @mean = convert(numeric(16,5),sum([Counts])) / @count
from tab1 WHERE ID = @id;
select @store = (sum(counts * counts) / @count) from tab1 WHERE ID = @id;
set @stddev = sqrt(@store - (@mean * @mean));
set @pcc = ((@store - (@mean * @mean)) / (@stddev * @stddev));
return @pcc;
end
调用如下函数
select db_name.dbo.calculate_PCC(1)
单程解决方案:
皮尔逊相关系数有两种类型,一种用于样本,另一种用于整个人群。这些都是简单的单程公式,我相信这两个公式都是正确的:
-- Methods for calculating the two Pearson correlation coefficients
SELECT
-- For Population
(avg(x * y) - avg(x) * avg(y)) /
(sqrt(avg(x * x) - avg(x) * avg(x)) * sqrt(avg(y * y) - avg(y) * avg(y)))
AS correlation_coefficient_population,
-- For Sample
(count(*) * sum(x * y) - sum(x) * sum(y)) /
(sqrt(count(*) * sum(x * x) - sum(x) * sum(x)) * sqrt(count(*) * sum(y * y) - sum(y) * sum(y)))
AS correlation_coefficient_sample
FROM (
-- The following generates a table of sample data containing two columns with a luke-warm and tweakable correlation
-- y = x for 0 thru 99, y = x - 100 for 100 thru 199, etc. Execute it as a stand-alone to see for yourself
-- x and y are CAST as DECIMAL to avoid integer math, you should definitely do the same
-- Try TOP 100 or less for full correlation (y = x for all cases), TOP 200 for a PCC of 0.5, TOP 300 for one near 0.33, etc.
-- The superfluous "+ 0" is where you could apply various offsets to see that they have no effect on the results
SELECT TOP 200
CAST(ROW_NUMBER() OVER (ORDER BY [object_id]) - 1 + 0 AS DECIMAL) AS x,
CAST((ROW_NUMBER() OVER (ORDER BY [object_id]) - 1) % 100 AS DECIMAL) AS y
FROM sys.all_objects
) AS a
正如我在评论中所指出的,您可以尝试前100名或以下的示例,以获得完全相关性(所有情况下y=x);前200名产生的相关性非常接近0.5;前300名,约0.33;等。如果您愿意,有一个地方(“+0”)可以添加偏移量;扰流板警报,它没有效果。确保将值强制转换为十进制-整数数学可以显著影响这些计算
create function calculate_PCC(@id int)
returns decimal(16,15)
as
begin
declare @mean numeric(16,5);
declare @stddev numeric(16,5);
declare @count numeric(16,5);
declare @pcc numeric(16,12);
declare @store numeric(16,7);
select @count = CONVERT(numeric(16,5), count(case when Id=@id then 1 end)) from tab1;
select @mean = convert(numeric(16,5),sum([Counts])) / @count
from tab1 WHERE ID = @id;
select @store = (sum(counts * counts) / @count) from tab1 WHERE ID = @id;
set @stddev = sqrt(@store - (@mean * @mean));
set @pcc = ((@store - (@mean * @mean)) / (@stddev * @stddev));
return @pcc;
end
select db_name.dbo.calculate_PCC(1)
-- Methods for calculating the two Pearson correlation coefficients
SELECT
-- For Population
(avg(x * y) - avg(x) * avg(y)) /
(sqrt(avg(x * x) - avg(x) * avg(x)) * sqrt(avg(y * y) - avg(y) * avg(y)))
AS correlation_coefficient_population,
-- For Sample
(count(*) * sum(x * y) - sum(x) * sum(y)) /
(sqrt(count(*) * sum(x * x) - sum(x) * sum(x)) * sqrt(count(*) * sum(y * y) - sum(y) * sum(y)))
AS correlation_coefficient_sample
FROM (
-- The following generates a table of sample data containing two columns with a luke-warm and tweakable correlation
-- y = x for 0 thru 99, y = x - 100 for 100 thru 199, etc. Execute it as a stand-alone to see for yourself
-- x and y are CAST as DECIMAL to avoid integer math, you should definitely do the same
-- Try TOP 100 or less for full correlation (y = x for all cases), TOP 200 for a PCC of 0.5, TOP 300 for one near 0.33, etc.
-- The superfluous "+ 0" is where you could apply various offsets to see that they have no effect on the results
SELECT TOP 200
CAST(ROW_NUMBER() OVER (ORDER BY [object_id]) - 1 + 0 AS DECIMAL) AS x,
CAST((ROW_NUMBER() OVER (ORDER BY [object_id]) - 1) % 100 AS DECIMAL) AS y
FROM sys.all_objects
) AS a