-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroystonTest.R
executable file
·103 lines (95 loc) · 3.01 KB
/
roystonTest.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
roystonTest <-
function (data, qqplot = FALSE)
{
dataframe=as.data.frame(data)
dname <- deparse(substitute(data))
data <- as.matrix(data)
p <- dim(data)[2]
n <- dim(data)[1]
z <- matrix(nrow <- p, ncol = 1)
z <- as.data.frame(z)
w <- matrix(nrow <- p, ncol = 1)
w <- as.data.frame(w)
data.org <- data
if (n <= 3) {
stop("n must be greater than 3")
}
else if (n >= 4 || n <= 11) {
x <- n
g <- -2.273 + 0.459 * x
m <- 0.544 - 0.39978 * x + 0.025054 * x^2 - 0.0006714 *
x^3
s <- exp(1.3822 - 0.77857 * x + 0.062767 * x^2 - 0.0020322 *
x^3)
for (i in 1:p) {
a2 <- data[, i]
{
if (kurtosis(a2) > 3) {
w <- sf.test(a2)$statistic
}
else {
w <- shapiro.test(a2)$statistic
}
}
z[i, 1] <- (-log(g - (log(1 - w))) - m)/s
}
}
if (n > 2000) {
stop("n must be less than 2000")
}
else if (n >= 12 || n <= 2000) {
x <- log(n)
g <- 0
m <- -1.5861 - 0.31082 * x - 0.083751 * x^2 + 0.0038915 *
x^3
s <- exp(-0.4803 - 0.082676 * x + 0.0030302 * x^2)
for (i in 1:p) {
a2 <- data[, i]
{
if (kurtosis(a2) > 3) {
w <- sf.test(a2)$statistic
}
else {
w <- shapiro.test(a2)$statistic
}
}
z[i, 1] <- ((log(1 - w)) + g - m)/s
}
}
else {
stop("n is not in the proper range")
}
u <- 0.715
v <- 0.21364 + 0.015124 * (log(n))^2 - 0.0018034 * (log(n))^3
l <- 5
C <- cor(data)
NC <- (C^l) * (1 - (u * (1 - C)^u)/v)
T <- sum(sum(NC)) - p
mC <- T/(p^2 - p)
edf <- p/(1 + (p - 1) * mC)
Res <- matrix(nrow = p, ncol = 1)
Res <- as.data.frame(Res)
for (i in 1:p) {
Res <- (qnorm((pnorm(-z[, ]))/2))^2
}
data <- scale(data, scale = FALSE)
Sa <- cov(data)
D <- data %*%solve(Sa)%*% t(data)
if (qqplot) {
d <- diag(D)
r <- rank(d)
chi2q <- qchisq((r-0.5)/n,p)
df = data.frame(d, chi2q)
qqPlotPrint = ggplot(df, aes(d, chi2q),environment = environment())+geom_point(shape=16, size=4)+
geom_abline(intercept =0, slope =1,color="black",size=2)+
xlab("Squared Mahalanobis Distance")+
ylab("Chi-Square Quantile")+
ggtitle("Chi-Square Q-Q Plot")+
theme(plot.title = element_text(lineheight=.8, face="bold"))
print(qqPlotPrint)
}
RH <- (edf * (sum(Res)))/p
pv <- pchisq(RH, edf, lower.tail = FALSE)
result <- new("royston", H = RH, p.value = pv, dname = dname, dataframe = dataframe)
result
}