Note that there’s no built-in Z-test in R. Use third party package BSDA (the code above written by Gemini)
# install.packages("BSDA")library(BSDA)
Loading required package: lattice
Attaching package: 'BSDA'
The following object is masked from 'package:datasets':
Orange
x <-c(1,1,1,1,1,4,3,2,1,1,1,1,1,11,1,1)z.test(x, sigma.x =0.5, conf.level =0.95)
One-sample z-Test
data: x
z = 16, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
1.755005 2.244995
sample estimates:
mean of x
2
You can also make implement from the formular: reference this passage: https://cran.r-project.org/web/packages/distributions3/vignettes/one-sample-z-test.html
5.1.2 t-test
x <-c(1,1,1,1,1,4,3,2,1,1,1,1,1,11,1,1)y <-c(1,1,1,1,1,4,3,2,1,1,1,1,1,11,1,1)t.test(x)
One Sample t-test
data: x
t = 3.1298, df = 15, p-value = 0.006884
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
0.6379832 3.3620168
sample estimates:
mean of x
2
t.test(x,y)
Welch Two Sample t-test
data: x and y
t = 0, df = 30, p-value = 1
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.845594 1.845594
sample estimates:
mean of x mean of y
2 2
t.test(x,y, mu =2, alter ="two.sided", var.equal = T)
Two Sample t-test
data: x and y
t = -2.2131, df = 30, p-value = 0.03464
alternative hypothesis: true difference in means is not equal to 2
95 percent confidence interval:
-1.845594 1.845594
sample estimates:
mean of x mean of y
2 2
5.2 variance inference
Try to type ?var.test to get the help document.
var.test(x,y)
F test to compare two variances
data: x and y
F = 1, num df = 15, denom df = 15, p-value = 1
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.3493947 2.8620925
sample estimates:
ratio of variances
1
5.3 goodness of fitness test
Test whether a sample data fit a distribution or not.
5.3.1 Normal Test
5.4 correlation
x <-c(1,1,1,1,1,4,3,2,1,1,1,1,1,11,1,1)y <-c(1,1,1,1,1,4,3,2,1,1,1,1,1,11,1,1)cor.test(x, y)
Pearson's product-moment correlation
data: x and y
t = Inf, df = 14, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
1 1
sample estimates:
cor
1
The default method, Pearson linear correlation is widely used.
5.5 median inference (non-para test)
Although it’s non-parametric test based on median, the function also require to pass parameter as mu.
x <-c(1,1,1,1,1,4,3,2,1,1,1,1,1,11,1,1)y <-c(1,1,1,1,1,4,3,2,1,1,1,1,1,11,1,1)wilcox.test(x)
Warning in wilcox.test.default(x): cannot compute exact p-value with ties
Wilcoxon signed rank test with continuity correction
data: x
V = 136, p-value = 0.0002424
alternative hypothesis: true location is not equal to 0
wilcox.test(x,y)
Warning in wilcox.test.default(x, y): cannot compute exact p-value with ties
Wilcoxon rank sum test with continuity correction
data: x and y
W = 128, p-value = 1
alternative hypothesis: true location shift is not equal to 0
wilcox.test(x,y, mu =2, alter ="two.sided", var.equal = T)
Warning in wilcox.test.default(x, y, mu = 2, alter = "two.sided", var.equal =
T): cannot compute exact p-value with ties
Wilcoxon rank sum test with continuity correction
data: x and y
W = 33.5, p-value = 0.0001599
alternative hypothesis: true location shift is not equal to 2