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