X <- c(159, 280, 101, 212, 224, 379, 179, 264, 222, 362, 168, 250, 149, 260,
485, 170)
t.test(X, alternative = "greater", mu = 225)
##
## One Sample t-test
##
## data: X
## t = 0.6685, df = 15, p-value = 0.257
## alternative hypothesis: true mean is greater than 225
## 95 percent confidence interval:
## 198.2321 Inf
## sample estimates:
## mean of x
## 241.5
N(mu1,sigma^2)
和N(mu2,sigma^2)
,其中mu1,mu2,sigma未知,问新的操作能否提高得率?X <- c(78.1, 72.4, 76.2, 74.3, 77.4, 78.4, 76, 75.5, 76.7, 77.3)
Y <- c(79.1, 81, 77.3, 79.1, 80, 79.1, 79.1, 77.3, 80.2, 82.1)
t.test(X, Y, var.equal = TRUE, alternative = "less")
##
## Two Sample t-test
##
## data: X and Y
## t = -4.2957, df = 18, p-value = 0.0002176
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf -1.908255
## sample estimates:
## mean of x mean of y
## 76.23 79.43
# t.test(X,Y,var.equal = FALSE,alternative = 'less')
X <- c(78.1, 72.4, 76.2, 74.3, 77.4, 78.4, 76, 75.5, 76.7, 77.3)
Y <- c(79.1, 81, 77.3, 79.1, 80, 79.1, 79.1, 77.3, 80.2, 82.1)
t.test(X - Y, alternative = "less")
##
## One Sample t-test
##
## data: X - Y
## t = -4.2018, df = 9, p-value = 0.00115
## alternative hypothesis: true mean is less than 0
## 95 percent confidence interval:
## -Inf -1.803943
## sample estimates:
## mean of x
## -3.2
# 等价于t.test(X,Y,alternative = 'less',paired=TRUE)
H0:mu=149,H1:mu<>149
H0:sigma^2=75,sigma^2<>75
X <- c(136, 144, 143, 157, 137, 159, 135, 158, 147, 165, 158, 142, 159, 150,
156, 152, 140, 149, 148, 155)
# 使用均值检验
t.test(X, mu = 149, var.equal = TRUE) #认为方差已知,做均值检验,p-value = 0.8025>0.05,无法拒绝原假设,认为mu=149
##
## One Sample t-test
##
## data: X
## t = 0.2536, df = 19, p-value = 0.8025
## alternative hypothesis: true mean is not equal to 149
## 95 percent confidence interval:
## 145.3736 153.6264
## sample estimates:
## mean of x
## 149.5
t.test(X, mu = 149, alternative = "two.sided", var.equal = FALSE) #认为sigma未知,做均值检验
##
## One Sample t-test
##
## data: X
## t = 0.2536, df = 19, p-value = 0.8025
## alternative hypothesis: true mean is not equal to 149
## 95 percent confidence interval:
## 145.3736 153.6264
## sample estimates:
## mean of x
## 149.5
# R没有单个总体的方差假设检验函数
X1...Xn1
来自总体X~N(mu1,sigma1^2)
的样本,Y1...Yn1
来自总体Y~N(mu2,sigma2^2)
的样本,且两样本独立,其检验问题为H0: sigma1^2 = sigma2^2,H1: sigma1^2 != sigma2^2
H0: sigma1^2 <= sigma2^2,H1: sigma1^2 > sigma2^2
H0: sigma1^2 >= sigma2^2,H1: sigma1^2 < sigma2^2
X <- c(78.1, 72.4, 76.2, 74.3, 77.4, 78.4, 76, 75.5, 76.7, 77.3)
Y <- c(79.1, 81, 77.3, 79.1, 80, 79.1, 79.1, 77.3, 80.2, 82.1)
var.test(X, Y)
##
## F test to compare two variances
##
## data: X and Y
## F = 1.4945, num df = 9, denom df = 9, p-value = 0.559
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.3712079 6.0167710
## sample estimates:
## ratio of variances
## 1.494481
binom.test(x, n, p=0.5,alternative=c("two.sided","less", "greater"),conf.level=0.95)
,x为指定成功的次数,n为实验的总次数,p为每次实验成功的概率。binom.test(445, 500, p = 0.85)
##
## Exact binomial test
##
## data: 445 and 500
## number of successes = 445, number of trials = 500, p-value =
## 0.01207
## alternative hypothesis: true probability of success is not equal to 0.85
## 95 percent confidence interval:
## 0.8592342 0.9160509
## sample estimates:
## probability of success
## 0.89
binom.test(7, 12, p = 0.4)
##
## Exact binomial test
##
## data: 7 and 12
## number of successes = 7, number of trials = 12, p-value = 0.2417
## alternative hypothesis: true probability of success is not equal to 0.4
## 95 percent confidence interval:
## 0.2766697 0.8483478
## sample estimates:
## probability of success
## 0.5833333
prop.test(7, 12, p = 0.4, correct = TRUE)
## Warning in prop.test(7, 12, p = 0.4, correct = TRUE): Chi-squared
## approximation may be incorrect
##
## 1-sample proportions test with continuity correction
##
## data: 7 out of 12, null probability 0.4
## X-squared = 1.0035, df = 1, p-value = 0.3165
## alternative hypothesis: true p is not equal to 0.4
## 95 percent confidence interval:
## 0.2859928 0.8350075
## sample estimates:
## p
## 0.5833333
prop.test(35, 120, p = 0.25, correct = TRUE)
##
## 1-sample proportions test with continuity correction
##
## data: 35 out of 120, null probability 0.25
## X-squared = 0.9, df = 1, p-value = 0.3428
## alternative hypothesis: true p is not equal to 0.25
## 95 percent confidence interval:
## 0.2141070 0.3828007
## sample estimates:
## p
## 0.2916667
binom.test(c(1, 399), p = 0.01, alternative = "less")
##
## Exact binomial test
##
## data: c(1, 399)
## number of successes = 1, number of trials = 400, p-value = 0.09048
## alternative hypothesis: true probability of success is less than 0.01
## 95 percent confidence interval:
## 0.0000000 0.0118043
## sample estimates:
## probability of success
## 0.0025
binom.test(1, 400, p = 0.01, alternative = "less")
##
## Exact binomial test
##
## data: 1 and 400
## number of successes = 1, number of trials = 400, p-value = 0.09048
## alternative hypothesis: true probability of success is less than 0.01
## 95 percent confidence interval:
## 0.0000000 0.0118043
## sample estimates:
## probability of success
## 0.0025
success <- c(23, 25)
total <- c(102, 135)
prop.test(success, total)
##
## 2-sample test for equality of proportions with continuity
## correction
##
## data: success out of total
## X-squared = 0.3615, df = 1, p-value = 0.5477
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.07256476 0.15317478
## sample estimates:
## prop 1 prop 2
## 0.2254902 0.1851852
X <- c(210, 312, 170, 85, 223)
chisq.test(X)
##
## Chi-squared test for given probabilities
##
## data: X
## X-squared = 136.49, df = 4, p-value < 2.2e-16
X <- c(25, 45, 50, 54, 55, 61, 64, 68, 72, 75, 75, 78, 79, 81, 83, 84, 84, 84,
85, 86, 86, 86, 87, 89, 89, 89, 90, 91, 91, 92, 100)
A <- table(cut(X, br = c(0, 69, 79, 89, 100))) #分组计数
A
##
## (0,69] (69,79] (79,89] (89,100]
## 8 5 13 5
p <- pnorm(c(70, 80, 90, 100), mean(X), sd(X))
p
## [1] 0.3439036 0.5781068 0.7869659 0.9184654
p <- c(p[1], p[2] - p[1], p[3] - p[2], 1 - p[3])
p
## [1] 0.3439036 0.2342032 0.2088591 0.2130341
chisq.test(A, p = p)
##
## Chi-squared test for given probabilities
##
## data: A
## X-squared = 8.334, df = 3, p-value = 0.03959
chisq.test(c(335, 125, 160), p = c(9, 3, 4)/16)
##
## Chi-squared test for given probabilities
##
## data: c(335, 125, 160)
## X-squared = 1.362, df = 2, p-value = 0.5061
X <- 0:6
Y <- c(7, 10, 12, 8, 3, 2, 0)
q <- ppois(X, mean(rep(X, Y)))
n <- length(X)
p[1] <- q[1]
p[n] <- 1 - q[n - 1]
for (i in 2:(n - 1)) p[i] <- q[i] - q[i - 1]
chisq.test(Y, p = p)
## Warning in chisq.test(Y, p = p): Chi-squared approximation may be
## incorrect
##
## Chi-squared test for given probabilities
##
## data: Y
## X-squared = 1.5057, df = 6, p-value = 0.9591
Z <- c(7, 10, 12, 8, 5)
n <- length(Z)
p <- p[1:n - 1]
p[n] <- 1 - q[n - 1]
chisq.test(Z, p = p)
##
## Chi-squared test for given probabilities
##
## data: Z
## X-squared = 0.5389, df = 4, p-value = 0.9696
X <- c(420, 500, 920, 1380, 1510, 1650, 1760, 2100, 2300, 2350)
ks.test(X, "pexp", 1/1500)
##
## One-sample Kolmogorov-Smirnov test
##
## data: X
## D = 0.3015, p-value = 0.2654
## alternative hypothesis: two-sided
X <- c(0.61, 0.29, 0.06, 0.59, -1.73, -0.74, 0.51, -0.56, 0.39, 1.64, 0.05,
-0.06, 0.64, -0.82, 0.37, 1.77, 1.09, -1.28, 2.36, 1.31, 1.05, -0.32, -0.4,
1.06, -2.47)
Y <- c(2.2, 1.66, 1.38, 0.2, 0.36, 0, 0.96, 1.56, 0.44, 1.5, -0.3, 0.66, 2.31,
3.29, -0.27, -0.37, 0.38, 0.7, 0.52, -0.71)
ks.test(X, Y)
##
## Two-sample Kolmogorov-Smirnov test
##
## data: X and Y
## D = 0.23, p-value = 0.5286
## alternative hypothesis: two-sided
x <- c(60, 3, 32, 11)
dim(x) <- c(2, 2)
x
## [,1] [,2]
## [1,] 60 32
## [2,] 3 11
chisq.test(x, correct = FALSE) #不带连续校正,p-value = 0.00188
##
## Pearson's Chi-squared test
##
## data: x
## X-squared = 9.6636, df = 1, p-value = 0.00188
chisq.test(x, correct = TRUE) #带连续校正
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: x
## X-squared = 7.9327, df = 1, p-value = 0.004855
x <- c(20, 24, 80, 82, 22, 38, 104, 125, 13, 28, 81, 113, 7, 18, 54, 92)
dim(x) <- c(4, 4)
x
## [,1] [,2] [,3] [,4]
## [1,] 20 22 13 7
## [2,] 24 38 28 18
## [3,] 80 104 81 54
## [4,] 82 125 113 92
chisq.test(x)
##
## Pearson's Chi-squared test
##
## data: x
## X-squared = 11.9886, df = 9, p-value = 0.214
x <- c(4, 5, 18, 6)
dim(x) <- c(2, 2)
x
## [,1] [,2]
## [1,] 4 18
## [2,] 5 6
fisher.test(x)
##
## Fisher's Exact Test for Count Data
##
## data: x
## p-value = 0.121
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.03974151 1.76726409
## sample estimates:
## odds ratio
## 0.2791061
x <- c(60, 3, 32, 11)
dim(x) <- c(2, 2)
x
## [,1] [,2]
## [1,] 60 32
## [2,] 3 11
fisher.test(x)
##
## Fisher's Exact Test for Count Data
##
## data: x
## p-value = 0.00282
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 1.626301 40.358904
## sample estimates:
## odds ratio
## 6.74691
X <- c(49, 21, 25, 107)
dim(X) <- c(2, 2)
X
## [,1] [,2]
## [1,] 49 25
## [2,] 21 107
mcnemar.test(X, correct = FALSE)
##
## McNemar's Chi-squared test
##
## data: X
## McNemar's chi-squared = 0.3478, df = 1, p-value = 0.5553
X <- c(66, 75, 78, 80, 81, 81, 82, 83, 83, 83, 83, 84, 85, 85, 86, 86, 86, 86,
87, 87, 88, 88, 88, 88, 88, 89, 89, 89, 89, 90, 90, 91, 91, 91, 91, 92,
93, 93, 96, 96, 96, 97, 99, 100, 101, 102, 103, 103, 104, 104, 104, 105,
106, 109, 109, 110, 110, 110, 111, 113, 115, 116, 117, 118, 155, 192)
binom.test(sum(X > 99), length(X), alternative = "less")
##
## Exact binomial test
##
## data: sum(X > 99) and length(X)
## number of successes = 23, number of trials = 66, p-value =
## 0.009329
## alternative hypothesis: true probability of success is less than 0.5
## 95 percent confidence interval:
## 0.0000000 0.4563087
## sample estimates:
## probability of success
## 0.3484848
x <- c(25, 30, 28, 23, 27, 35, 30, 28, 32, 29, 30, 30, 31, 16)
y <- c(19, 32, 21, 19, 25, 31, 31, 26, 30, 25, 28, 31, 25, 25)
binom.test(sum(x < y), length(x))
##
## Exact binomial test
##
## data: sum(x < y) and length(x)
## number of successes = 4, number of trials = 14, p-value = 0.1796
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
## 0.08388932 0.58103526
## sample estimates:
## probability of success
## 0.2857143
binom.test(3, 12, p = 1/2, alternative = "less", conf.level = 0.1) #p-value = 0.073<0.1,因此拒绝原假设,认为喜欢咖啡的人超过喜欢奶茶的人
##
## Exact binomial test
##
## data: 3 and 12
## number of successes = 3, number of trials = 12, p-value = 0.073
## alternative hypothesis: true probability of success is less than 0.5
## 10 percent confidence interval:
## 0.0000000 0.1541877
## sample estimates:
## probability of success
## 0.25
binom.test(3, 12, p = 1/2, alternative = "less", conf.level = 0.05)
##
## Exact binomial test
##
## data: 3 and 12
## number of successes = 3, number of trials = 12, p-value = 0.073
## alternative hypothesis: true probability of success is less than 0.5
## 5 percent confidence interval:
## 0.0000000 0.1228507
## sample estimates:
## probability of success
## 0.25
x <- c(1, 2, 3, 4, 5, 6)
y <- c(6, 5, 4, 3, 2, 1)
cor.test(x, y, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: x and y
## S = 70, p-value = 0.002778
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -1
X <- c(86, 77, 68, 91, 70, 71, 85, 87, 63)
Y <- c(88, 76, 64, 96, 65, 80, 81, 72, 60)
cor.test(X, Y, method = "kendall")
##
## Kendall's rank correlation tau
##
## data: X and Y
## T = 31, p-value = 0.005886
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## 0.7222222
X <- c(137, 140, 138.3, 139, 144.3, 139.1, 141.7, 137.3, 133.5, 138.2, 141.1,
139.2, 136.5, 136.5, 135.6, 138, 140.9, 140.6, 136.3, 134.1)
wilcox.test(X, mu = 140, alternative = "less", exact = FALSE, correct = FALSE,
conf.int = TRUE)
##
## Wilcoxon signed rank test
##
## data: X
## V = 34, p-value = 0.007034
## alternative hypothesis: true location is less than 140
## 95 percent confidence interval:
## -Inf 139.2
## sample estimates:
## (pseudo)median
## 138.2
x <- c(459, 367, 303, 392, 310, 342, 421, 446, 430, 412)
y <- c(414, 306, 321, 443, 281, 301, 353, 391, 405, 390)
wilcox.test(x, y, alternative = "greater", paired = TRUE)
##
## Wilcoxon signed rank test
##
## data: x and y
## V = 47, p-value = 0.02441
## alternative hypothesis: true location shift is greater than 0
x <- c(24, 26, 29, 34, 43, 58, 63, 72, 87, 101)
y <- c(82, 87, 97, 121, 164, 208, 213)
wilcox.test(x, y, alternative = "less", exact = FALSE, correct = FALSE) #不采用连续修正
##
## Wilcoxon rank sum test
##
## data: x and y
## W = 4.5, p-value = 0.001449
## alternative hypothesis: true location shift is less than 0
wilcox.test(x, y, alternative = "less", exact = FALSE, correct = TRUE) #采用连续修正
##
## Wilcoxon rank sum test with continuity correction
##
## data: x and y
## W = 4.5, p-value = 0.001698
## alternative hypothesis: true location shift is less than 0
x <- c(3, 5, 7, 9, 10)
y <- c(1, 2, 4, 6, 8)
wilcox.test(x, y, alternative = "greater")
##
## Wilcoxon rank sum test
##
## data: x and y
## W = 19, p-value = 0.1111
## alternative hypothesis: true location shift is greater than 0
x <- c(4, 6, 7, 9, 10)
y <- c(1, 2, 3, 5, 8)
wilcox.test(x, y, alternative = "greater")
##
## Wilcoxon rank sum test
##
## data: x and y
## W = 21, p-value = 0.04762
## alternative hypothesis: true location shift is greater than 0
x <- rep(1:4, c(62, 41, 14, 11))
y <- rep(1:4, c(20, 37, 16, 15))
wilcox.test(x, y, exact = FALSE)
##
## Wilcoxon rank sum test with continuity correction
##
## data: x and y
## W = 3994, p-value = 0.0001242
## alternative hypothesis: true location shift is not equal to 0
A <- c(321, 266, 256, 388, 330, 329, 303, 334, 299, 221, 365, 250, 258, 342,
343, 298, 238, 317, 354)
B <- c(488, 598, 507, 428, 807, 342, 512, 350, 672, 589, 665, 549, 451, 481,
514, 391, 366, 468)
diff <- median(B) - median(A)
A <- A + diff
mood.test(A, B)
##
## Mood two-sample test of scale
##
## data: A and B
## Z = -2.4846, p-value = 0.01297
## alternative hypothesis: two.sided
x <- list(swim = c(306, 385, 300, 319, 320), basketball = c(311, 364, 315, 338,
398), bicycle = c(289, 198, 201, 302, 289))
kruskal.test(x)
##
## Kruskal-Wallis rank sum test
##
## data: x
## Kruskal-Wallis chi-squared = 9.1564, df = 2, p-value = 0.01027
worker.a <- c(18, 17.1, 16.4, 16.9, 16.9, 16.7, 16.7, 17.2, 17.5, 16.9)
worker.b <- c(17, 16.9, 17, 16.9, 17.2, 17.1, 16.8, 17.1, 17.1, 17.2)
ansari.test(worker.a, worker.b)
## Warning in ansari.test.default(worker.a, worker.b): cannot compute exact
## p-value with ties
##
## Ansari-Bradley test
##
## data: worker.a and worker.b
## AB = 41.5, p-value = 0.04232
## alternative hypothesis: true ratio of scales is not equal to 1
x <- list(A = c(8, 7, 9, 10, 9, 6, 5, 8, 10, 5), B = c(8, 7, 9, 6, 8, 9, 10,
7, 8, 9), C = c(10, 10, 9, 6, 8, 3, 5, 6, 7, 4))
fligner.test(x)
##
## Fligner-Killeen test of homogeneity of variances
##
## data: x
## Fligner-Killeen:med chi-squared = 5.1905, df = 2, p-value =
## 0.07463