《R语言与统计分析》-非参数的假设检验

####非参数的假设检验####
####单总体位置参数的检验####
####中位数的符号检验-sign.test####
#M满足P(XM)=1/2
#R无内置函数,自己写#
sign.test<-function(x,m0,alpha=0.05,alter="two.sided"){
  p<-list()
  n<-length(x)
  sign<-as.numeric(x>=m0)
  s<-sum(sign)
  result<-binom.test(s,n,p=0.5,alternative = alter,
                     conf.level = alpha)
  p$p.value=result$p.value
  p
}
#alter的取值为two.sided双边检验/less下侧检验/greater上侧检验

####Wilcoxon符号秩检验-wilcox.test####
#M满足F(M-x)=1-F(x-M),要求X为连续型
wilcox.test(x, y = NULL,
            alternative = c("two.sided", "less", "greater"),
            mu = 0, paired = FALSE, exact = NULL, correct = TRUE,
            conf.int = FALSE, conf.level = 0.95,
            tol.root = 1e-4, digits.rank = Inf, ...)
#exact表示是否算出精确的p值,correct表示大样本时是否做连续型修正

####分布的一致性检验-chisq.test####
chisq.test(x, y = NULL, correct = TRUE,
           p = rep(1/length(x), length(x)), rescale.p = FALSE,
           simulate.p.value = FALSE, B = 2000)
#x是一维的,y为空值,用于拟合优度检验
#x,y同时给出进行列联表检验

####卢瑟福和盖革的著名实验P184####
#试问分布规律是否服从泊松分布
x<-0:10
y<-c(57,203,383,525,532,408,273,139,45,27,16)
options(digits = 3)
likely<-function(lambda=3){
  -sum(y*dpos(x,lambda=lambda,log=T))
}
library(stats4)
mle(likely)
#由于函数chisq.test()无法调整因参数估计引起的自由度调整,
#因此需要编程计算检验统计量及p值,如下:
chisq.fit<-function(x,y,r){   #?r是什么?
  options(digits = 4)
  result<-list()
  n=sum(y)
  prob<-c(prob,1-sum(prob))
  result$chisq<-sum((y-n*prob)^2/(n*prob))
  result$p.value<-pchisq(result$chisq,m-r-1,lower.tail = F)
  result
}
chisq.fit(x,y,1)

####两总体的比较与检验####
####独立性检验-chisq.test####
chisq.test(x, y = NULL, correct = TRUE,
           p = rep(1/length(x), length(x)), rescale.p = FALSE,
           simulate.p.value = FALSE, B = 2000)

####精确检验-fisher.test####
#允许列联表中20%以上的元素小于5
fisher.test(x, y = NULL, workspace = 200000, hybrid = FALSE,
            hybridPars = c(expect = 5, percent = 80, Emin = 1),
            control = list(), or = 1, alternative = "two.sided",
            conf.int = TRUE, conf.level = 0.95,
            simulate.p.value = FALSE, B = 2000)
#workspace为整数,指定工作空间的数量,列联表的维数大于2×2时使用;
#hybird为逻辑值,指定是否计算精确的概率,列联表的维数大于2×2时使用;
#or指定假设的概率比率,只在2×2时使用

####Wilcoxon秩和检验法-wilcox.test####
####Mann-Whitney U检验-wilcox.test####
wilcox.test(x, y = NULL,
            alternative = c("two.sided", "less", "greater"),
            mu = 0, paired = FALSE, exact = NULL, correct = TRUE,
            conf.int = FALSE, conf.level = 0.95,
            tol.root = 1e-4, digits.rank = Inf, ...)
#exact表示是否算出精确的p值,correct表示大样本时是否做连续型修正

####Mood检验-mood.test####
mood.test(x, y,
          alternative = c("two.sided", "less", "greater"), ...)
#例
A<-c(321,266,388,342,363,234,233,377,237,427,226)
B<-c(341,423,314,214,532,243,352,352,342,364,239,356)
#问两个存的月收入内部差异是否相同?
diff<-median(B)-median(A)
A<-A+diff
mood.test(A,B)
#p-value = 0.8347,不拒绝原假设,认为两个村的内部差异相同

####多总体的比较与检验####
#多种投资方案在试行后效果的比较?
#不同机器在同一条件下的稳定性是否相同?
####Kruskal-Wallis秩和检验-kruskal.test####
kruskal.test(x, g, ...)
#x为向量时,g为对x分类的因子;x为列表时,g可以省略

####Ansari-Bradley检验-ansari.test####
ansari.test(x, y,
            alternative = c("two.sided", "less", "greater"),
            exact = NULL, conf.int = FALSE, conf.level = 0.95,
            ...)
#x为向量时,g为对x分类的因子;x为列表时,g可以省略

####Fligner-Killeen检验-fligner.test####
fligner.test(x, g, ...)
#x为向量时,g为对x分类的因子;x为列表时,g可以省略

你可能感兴趣的:(r语言,开发语言)