R中统计假设检验总结(一)

简介:

先PS一个:
考虑到这次的题目本身的特点 尝试下把说明性内容都直接作为备注写在语句中 另外用于说明的部分例子参考了我的教授Guy Yollin在Financial Data Analysis and Modeling with R这门课课件上的例子 部分参考了相关package的帮助文档中的例子 下面正题

- 戌

 



> # Assume the predetermined significance level is 0.05.
假设预定的显着性水平是0.05。



> # 1 Shapiro-Wilk Test

> # Null hypothesis:
零假设:
> # The sample came from a normally distributed population.
样本来自正态分布总体

> install.packages("stats")
> library(stats)
> # args() function displays the argument names and corresponding default values of a function or primitive.
args()函数显示一个函数的参数名称和相应的默认值。
> args(shapiro.test)
function (x) 
NULL

> # Example 1:
> # Test random sample from a normal distribution.
测试来自正态分布的随机抽样。
> set.seed(1)
> x <- rnorm(150)
> res <- shapiro.test(x)

> res$p.value # > 0.05
[1] 0.7885523
> # Conclusion: We are unable to reject the null hypothesis.
结论:我们无法拒绝零假设。

> # Example 2:
> # Test daily observations of S&P 500 from 1981-01 to 1991-04.
测试S&P500指数从1981-01到1991-04的日观察值。
> install.packages("Ecdat")
> library(Ecdat)
> data(SP500)
> class(SP500)
[1] "data.frame"
> SPreturn = SP500$r500 # use the $ to index a column of the data.frame
用$符号取出数据框中的一列
> (res <- shapiro.test(SPreturn))
        Shapiro-Wilk normality test
data: SPreturn
W = 0.8413, p-value < 2.2e-16

> names(res)
[1] "statistic" "p.value" "method" "data.name"

> res$p.value # < 0.05
[1] 2.156881e-46
> # Conclusion: We should reject the null hypothesis.
结论:我们应该拒绝零假设。



> # 2 Jarque-Bera Test

> # Null hypothesis:
> # The skewness and the excess kurtosis of samples are zero.
样本的偏度和多余峰度均为零

> install.packages("tseries")
> library(tseries)
> args(jarque.bera.test)
function (x) 
NULL

> # Example 1: 
> # Test random sample from a normal distribution
> set.seed(1)
> x <- rnorm(150)
> res <- jarque.bera.test(x)

> names(res)
[1] "statistic" "parameter" "p.value" "method" "data.name"

> res$p.value # > 0.05
X-squared 
0.8601533 
> # Conclusion: We should not reject the null hypothesis. 

> # Example 2:
> # Test daily observations of S&P 500 from 1981–01 to 1991–04
> install.packages("Ecdat")
> library(Ecdat)
> data(SP500)
> class(SP500)
[1] "data.frame"
> SPreturn = SP500$r500 # use the $ to index a column of the data.frame
> (res <- jarque.bera.test(SPreturn))
        Jarque Bera Test
data: SPreturn
X-squared = 648508.6, df = 2, p-value < 2.2e-16

> names(res)
[1] "statistic" "parameter" "p.value" "method" "data.name"

> res$p.value # < 0.05
X-squared 
        0 
> # Conclusion: We should reject the null hypothesis.



> # 3 Correlation Test

> # Null hypothesis:
> # The correlation is zero.
样本相关性为0

> install.packages("stats")
> library(stats)
> args(getS3method("cor.test","default"))
function (x, y, alternative = c("two.sided", "less", "greater"), 
    method = c("pearson", "kendall", "spearman"), exact = NULL, 
    conf.level = 0.95, continuity = FALSE, ...) 
NULL
> # x, y: numeric vectors of the data to be tested
x, y: 进行测试的数据的数值向量
> # alternative: controls two-sided test or one-sided test
alternative: 控制进行双侧检验或单侧检验
> # method: "pearson", "kendall", or "spearman"
> # conf.level: confidence level for confidence interval
conf.level: 置信区间的置信水平

> # Example:
> # Test the correlation between the food industry and the market portfolio.
测试在食品行业的收益和市场投资组合之间的相关性。
> data(Capm,package="Ecdat")
> (res <- cor.test(Capm$rfood,Capm$rmrf))
        Pearson's product-moment correlation
皮尔逊积矩相关
data: Capm$rfood and Capm$rmrf
t = 27.6313, df = 514, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
备择假设:真正的相关性不等于0
95 percent confidence interval:
95%置信区间
 0.7358626 0.8056348
sample estimates:
样本估计
      cor 
0.7730767 

> names(res)
[1] "statistic" "parameter" "p.value" "estimate" 
[5] "null.value" "alternative" "method" "data.name" 
[9] "conf.int" 

> res$p.value # < 0.05
[1] 0
> # Conclusion: We should reject the null hypothesis.



本文转自einyboy博客园博客,原文链接:http://www.cnblogs.com/einyboy/p/3197584.html,如需转载请自行联系原作者。

目录
相关文章
|
数据处理
第8章 概率统计——8.5 统计作图
第8章 概率统计——8.5 统计作图
|
4月前
|
机器学习/深度学习 监控 数据挖掘
数据并非都是正态分布:三种常见的统计分布及其应用
这篇文章除了介绍线性模型在减肥app预测中的不切实际性,还探讨了不同统计分布在体重管理和数据分析中的应用。文章提到了正态分布和泊松分布,前者常用于描述围绕平均值对称分布的连续数据,如体重;后者适合计数数据,如体重变化次数。正态分布以其钟形曲线闻名,泊松分布则描述独立事件的数量。文章还简要介绍了卡方分布在检验分类变量关系时的作用。最后,文章指出了在线性回归中假设数据正态分布的原因,包括便于统计推断和最小化估计误差。
298 5
|
5月前
|
算法 数据挖掘
R语言中的贝叶斯统计方法
【4月更文挑战第26天】R语言在贝叶斯统计中发挥着重要作用,提供如&quot;BUGS&quot;、&quot;Stan&quot;、&quot;JAGS&quot;等包来处理复杂模型和数值计算。贝叶斯方法基于概率论,涉及先验分布、似然函数、后验分布和MCMC模拟。&quot;BUGS&quot;适用于复杂层次模型,&quot;Stan&quot;则在大规模数据和复杂模型上有优势。
64 2
|
5月前
R语言自定义两种统计量度:平均值和中位数,何时去使用?
R语言自定义两种统计量度:平均值和中位数,何时去使用?
|
5月前
R语言参数检验 :需要多少样本?如何选择样本数量
R语言参数检验 :需要多少样本?如何选择样本数量
|
5月前
R语言区间数据回归分析
R语言区间数据回归分析
|
5月前
|
数据挖掘
统计的基本概念及抽样分布
统计的基本概念及抽样分布
统计的基本概念及抽样分布
|
5月前
|
数据格式
基于SPSS的经典统计学分析与偏度峰度等常用统计学指标计算
基于SPSS的经典统计学分析与偏度峰度等常用统计学指标计算
|
12月前
初识统计学——贝叶斯统计
在数据少的情况下也可以进行推测,数据越多,推测结果越准确”,以及“对所获的信息可做出瞬时反应,自动升级推测”的学习功能 但与此同时,“贝叶斯统计在某种程度上是不可靠的”。究其原因,是由于贝叶斯统计中所涉及的概率是“主观的”,但这也是它便利性的本质体现。
71 1