<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Speraman on 格物堂</title><link>https://gewutang.cn/tags/speraman/</link><description>Recent content in Speraman on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Thu, 16 Aug 2012 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/speraman/index.xml" rel="self" type="application/rss+xml"/><item><title>R中的斯皮尔曼等级相关检验与异方差</title><link>https://gewutang.cn/2012/08/16/spearman-test-in-r/</link><pubDate>Thu, 16 Aug 2012 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2012/08/16/spearman-test-in-r/</guid><description>&lt;p&gt;有一种异方差表现为模型的残差与自变量相关，也就是说，模型残差随着自变量增大而变大或者反之。在这种情况下只要度量模型的残差与自变量的相关性就可以确定是否有异方差了。如果自变量是连续变量，则普通的相关性检验就可以奏效。倘若自变量是年龄、职业、气候等定型变量时就得考虑别的方法了。&lt;/p&gt;&#10;&lt;p&gt;此时，斯皮尔曼等级相关检验是个不错的选择。斯皮尔曼等级相关检验是一种非参数检验方法，它的大致原理是这样的：&lt;/p&gt;&#10;&lt;p&gt;如果变量和变量相关，那么，将两者按照大小排序，则两者的顺序应该是相仿的，若两者相关性弱，则两者的顺序没有明显的一致性。&lt;/p&gt;&#10;&lt;p&gt;举个简单的例子：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; x &amp;lt;-c(25,40,54,58,68)&#10; y &amp;lt;-c(1.6,2.9,10.7,14.8,5.7) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;上下对应，下面按照的大小进行排序，则能看到下面的结果：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;rank(x)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 2 3 4 5&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;rank(y)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 1 2 4 5 3&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;rank(x)-rank(y)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] 0 0 -1 -1 2&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;构造统计量，容易知道等于1时，两者正相关；等于-1时两者负相关。设定原假设统计量为零，该统计量近似服从均值为0，方差为的正态分布，由此可对其进行假设检验。&lt;/p&gt;&#10;&lt;p&gt;R中&lt;code&gt;pspearman&lt;/code&gt;包中的&lt;code&gt;spearman.test&lt;/code&gt;函数可以完成斯皮尔曼等级相关检验：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 安装包，如果未安装&#10;# install.packages(&amp;quot;pspearman&amp;quot;)&#10;library(pspearman)&#10;x=c(25,40,53,58,68)&#10;y=c(1.6,2.9,10.7,14.8,5.7)&#10;spearman.test(x,y)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#9;Spearman's rank correlation rho&#10;## &#10;## data: x and y&#10;## S = 6, p-value = 0.2333&#10;## alternative hypothesis: true rho is not equal to 0&#10;## sample estimates:&#10;## rho &#10;## 0.7&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;也可用&lt;code&gt;cor.test&lt;/code&gt;函数来检验：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;cor.test(x,y,method=&amp;quot;spearman&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#9;Spearman's rank correlation rho&#10;## &#10;## data: x and y&#10;## S = 6, p-value = 0.2333&#10;## alternative hypothesis: true rho is not equal to 0&#10;## sample estimates:&#10;## rho &#10;## 0.7&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item></channel></rss>