计算Spearman等级相关系数的VBA函数
来源:互联网 发布:转发小视频软件 编辑:程序博客网 时间:2024/05/30 22:50
公式:
其中
因为次序
以下代码适用于Excel不同版本(老版本只提供了rank,不能取平均,2007以后功能得到了加强,有多选择),
Function Spearman(Rng1 As Range, Rng2 As Range) As Double Dim WF As WorksheetFunction Dim dSquared() As Long Dim r As Long Set WF = WorksheetFunction ReDim Preserve dSquared(1 To Rng1.Cells.Count) If Rng1.Columns.Count < 2 Then For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank(Rng1.Cells(r, 1), Rng1) - WF.Rank(Rng2.Cells(r, 1), Rng2)) ^ 2 Next r Else For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank(Rng1.Cells(1, r), Rng1) - WF.Rank(Rng2.Cells(1, r), Rng2)) ^ 2 Next r End If Spearman = 1 - ((6 * WF.Sum(dSquared)) / ((Rng1.Cells.Count ^ 3) - Rng1.Cells.Count))End FunctionFunction SpearmanAvg(Rng1 As Range, Rng2 As Range) As Double Dim WF As WorksheetFunction Dim dSquared() As Double Dim r As Long Set WF = WorksheetFunction ReDim Preserve dSquared(1 To Rng1.Cells.Count) If Rng1.Columns.Count < 2 Then For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank_Avg(Rng1.Cells(r, 1), Rng1) - WF.Rank_Avg(Rng2.Cells(r, 1), Rng2)) ^ 2 Next r Else For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank_Avg(Rng1.Cells(1, r), Rng1) - WF.Rank_Avg(Rng2.Cells(1, r), Rng2)) ^ 2 Next r End If SpearmanAvg = 1 - ((6 * WF.Sum(dSquared)) / ((Rng1.Cells.Count ^ 3) - Rng1.Cells.Count))End FunctionFunction SpearmanEq(Rng1 As Range, Rng2 As Range) As Double Dim WF As WorksheetFunction Dim dSquared() As Long Dim r As Long Set WF = WorksheetFunction ReDim Preserve dSquared(1 To Rng1.Cells.Count) If Rng1.Columns.Count < 2 Then For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank_Eq(Rng1.Cells(r, 1), Rng1) - WF.Rank_Eq(Rng2.Cells(r, 1), Rng2)) ^ 2 Next r Else For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank_Eq(Rng1.Cells(1, r), Rng1) - WF.Rank_Eq(Rng2.Cells(1, r), Rng2)) ^ 2 Next r End If SpearmanEq = 1 - ((6 * WF.Sum(dSquared)) / ((Rng1.Cells.Count ^ 3) - Rng1.Cells.Count))End Function
以下为原始代码
Function Spearman(Rng1 As Range, Rng2 As Range) As Double Dim WF As WorksheetFunction Dim dSquared() As Long Dim r As Long Set WF = WorksheetFunction ReDim Preserve dSquared(1 To Rng1.Rows.Count) For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank(Rng1.Cells(r, 1), Rng1) - WF.Rank(Rng2.Cells(r, 1), Rng2)) ^ 2 Next r Spearman = 1 - ((6 * WF.Sum(dSquared)) / ((Rng1.Rows.Count ^ 3) - Rng1.Rows.Count))End FunctionFunction SpearmanAvg(Rng1 As Range, Rng2 As Range) As Double Dim WF As WorksheetFunction Dim dSquared() As Long Dim r As Long Set WF = WorksheetFunction ReDim Preserve dSquared(1 To Rng1.Rows.Count) For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank_Avg(Rng1.Cells(r, 1), Rng1) - WF.Rank_Avg(Rng2.Cells(r, 1), Rng2)) ^ 2 Next r SpearmanAvg = 1 - ((6 * WF.Sum(dSquared)) / ((Rng1.Rows.Count ^ 3) - Rng1.Rows.Count))End FunctionFunction SpearmanEq(Rng1 As Range, Rng2 As Range) As Double Dim WF As WorksheetFunction Dim dSquared() As Long Dim r As Long Set WF = WorksheetFunction ReDim Preserve dSquared(1 To Rng1.Rows.Count) For r = LBound(dSquared) To UBound(dSquared) dSquared(r) = (WF.Rank_Eq(Rng1.Cells(r, 1), Rng1) - WF.Rank_Eq(Rng2.Cells(r, 1), Rng2)) ^ 2 Next r SpearmanEq = 1 - ((6 * WF.Sum(dSquared)) / ((Rng1.Rows.Count ^ 3) - Rng1.Rows.Count))End Function
0 0
- 计算Spearman等级相关系数的VBA函数
- Spearman Rank相关系数计算
- 利用matlab计算Pearson和Spearman相关系数
- spearman 相关系数
- spearman相关系数
- Pearson相关系数, Spearman相关系数,Kendall相关系数的区别
- 三大统计相关系数:Pearson、Spearman秩相关系数、kendall等级相关系数
- 统计相关系数(2)——Spearman Rank(斯皮尔曼等级)相关系数及MATLAB实现
- 统计相关系数(2)——Spearman Rank(斯皮尔曼等级)相关系数
- 统计相关系数(2)——Spearman Rank(斯皮尔曼等级)相关系数及MATLAB实现
- 统计相关系数(2)——Spearman Rank(斯皮尔曼等级)相关系数及MATLAB实现
- [秩相关] Spearman秩相关系数计算及假设检验
- Spearman秩相关系数
- 相关性研究的非参数方法:Spearman的秩相关系数和Kendall tau秩相关系数
- Spearman秩相关系数和Pearson皮尔森相关系数
- Spearman秩相关系数和Pearson皮尔森相关系数
- pearson相关系数与spearman秩相关系数
- 【matlab】matlab相关系数计算公式(Pearson和Spearman,以及Kendall Rank)
- iOS开发中常用字符串验证处理操作
- 配置spark源码阅读windows环境
- 从事C和C++开发者快速学python(三)
- 一个Linux下C线程池的实现
- Linux学习总结(6)——CenterOS7安装mysql5.5的方法
- 计算Spearman等级相关系数的VBA函数
- leetcode 173. Binary Search Tree Iterator-二叉搜索树迭代|中序遍历
- 如何截取VTK中window的场景
- 数组
- Xml解析
- HTTPS 接入优化建议
- IntelliJ中的main函数和System.out.println()快捷键
- ajax学习小结
- 1 查看文件和目录ls