先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.