Description Usage Arguments Details Value Author(s) References Examples
using Weight Mahalanobis Distance to Discriminant Analysis and return a result table and accuracy report
1 |
TrnX |
matrix or data frame of training set cases. |
TrnG |
vector of factors of the samples |
Tweight |
matrix or dataframe of the weight of the parameters,if you do not define the weight,the corresponding percent contributions of the parameters based on the principal component analysis scheme will be used instead |
TstX |
matrix or data frame of test set cases. A vector will be interpreted as a row vector for a single case. |
var.equal |
whether class have the same covariance or not |
the function of wmd apply two methods,one is normal discriminant analysis and at this time Tweight should be given like diag(rep(w,n)),which w is the weight of each parameter.if Tweight is not given,the the corresponding percent contributions of the parameters based on the principal component analysis scheme will be used instead.
result of classifications of test set will be returned. (When TstX is NULL, the function will automatically consider the user is trying to test the Discriminant Analysis Methods by weight Mahalanobis distance. Hence, a test result table and accuracy report will be shown on the R-console.)
Bingpei Wu
passage: "APPLICATION OF WEIGHTED MAHALANOBIS DISTANCE DISCRIMINANT ANALYSIS METHOD TO CLASSIFICATION OF ROCK MASS QUALITY",whose author is YAO Yinpei
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 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
##do not define the weight
X=iris[,1:4]
G=gl(3,50)
wmd(X,G)
##define the weight
X=iris[,1:4]
G=gl(3,50)
wmd(X,G,diag(rep(0.25,4)))
## The function is currently defined as
function (TrnX, TrnG, Tweight = NULL, TstX = NULL, var.equal = F)
{
if (is.factor(TrnG) == FALSE) {
mx <- nrow(TrnX)
mg <- nrow(TrnG)
TrnX <- rbind(TrnX, TrnG)
TrnG <- factor(rep(1:2, c(mx, mg)))
}
if (is.null(TstX) == TRUE)
TstX <- TrnX
if (is.vector(TstX) == TRUE)
TstX <- t(as.matrix(TstX))
else if (is.matrix(TstX) != TRUE)
TstX <- as.matrix(TstX)
if (is.matrix(TrnX) != TRUE)
TrnX <- as.matrix(TrnX)
if (is.null(Tweight) == TRUE)
Tweight = cor(TstX)
nx <- nrow(TstX)
blong <- matrix(rep(0, nx), nrow = 1, dimnames = list("blong",
1:nx))
g <- length(levels(TrnG))
mu <- matrix(0, nrow = g, ncol = ncol(TrnX))
for (i in 1:g) mu[i, ] <- colMeans(TrnX[TrnG == i, ])
D <- matrix(0, nrow = g, ncol = nx)
if (var.equal == TRUE || var.equal == T) {
for (i in 1:g) D[i, ] <- mahalanobis2(TstX, mu[i, ],
var(TrnX), Tweight)
}
else {
for (i in 1:g) D[i, ] <- mahalanobis2(TstX, mu[i, ],
var(TrnX[TrnG == i, ]), Tweight)
}
for (j in 1:nx) {
dmin <- Inf
for (i in 1:g) if (D[i, j] < dmin) {
dmin <- D[i, j]
blong[j] <- i
}
}
print(blong)
print("num of wrong judgement")
print(which(blong != TrnG))
print("samples divided to")
print(blong[which(blong != TrnG)])
print("samples actually belongs to")
print(TrnG[which(blong != TrnG)])
print("percent of right judgement")
print(1 - length(which(blong != TrnG))/length(blong))
}
|
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
blong 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
blong 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
blong 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 2 2
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
blong 2 2 3 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
blong 2 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
blong 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 2 2
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
blong 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[1] "num of wrong judgement"
[1] 69 73 78 84 107 130 134 135
[1] "samples divided to"
[1] 3 3 3 3 2 2 2 2
[1] "samples actually belongs to"
[1] 2 2 2 2 3 3 3 3
Levels: 1 2 3
[1] "percent of right judgement"
[1] 0.9466667
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
blong 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
blong 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
blong 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 2 2
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
blong 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
blong 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
blong 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
blong 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[1] "num of wrong judgement"
[1] 71 73 84
[1] "samples divided to"
[1] 3 3 3
[1] "samples actually belongs to"
[1] 2 2 2
Levels: 1 2 3
[1] "percent of right judgement"
[1] 0.98
function (TrnX, TrnG, Tweight = NULL, TstX = NULL, var.equal = F)
{
if (is.factor(TrnG) == FALSE) {
mx <- nrow(TrnX)
mg <- nrow(TrnG)
TrnX <- rbind(TrnX, TrnG)
TrnG <- factor(rep(1:2, c(mx, mg)))
}
if (is.null(TstX) == TRUE)
TstX <- TrnX
if (is.vector(TstX) == TRUE)
TstX <- t(as.matrix(TstX))
else if (is.matrix(TstX) != TRUE)
TstX <- as.matrix(TstX)
if (is.matrix(TrnX) != TRUE)
TrnX <- as.matrix(TrnX)
if (is.null(Tweight) == TRUE)
Tweight = cor(TstX)
nx <- nrow(TstX)
blong <- matrix(rep(0, nx), nrow = 1, dimnames = list("blong",
1:nx))
g <- length(levels(TrnG))
mu <- matrix(0, nrow = g, ncol = ncol(TrnX))
for (i in 1:g) mu[i, ] <- colMeans(TrnX[TrnG == i, ])
D <- matrix(0, nrow = g, ncol = nx)
if (var.equal == TRUE || var.equal == T) {
for (i in 1:g) D[i, ] <- mahalanobis2(TstX, mu[i, ],
var(TrnX), Tweight)
}
else {
for (i in 1:g) D[i, ] <- mahalanobis2(TstX, mu[i, ],
var(TrnX[TrnG == i, ]), Tweight)
}
for (j in 1:nx) {
dmin <- Inf
for (i in 1:g) if (D[i, j] < dmin) {
dmin <- D[i, j]
blong[j] <- i
}
}
print(blong)
print("num of wrong judgement")
print(which(blong != TrnG))
print("samples divided to")
print(blong[which(blong != TrnG)])
print("samples actually belongs to")
print(TrnG[which(blong != TrnG)])
print("percent of right judgement")
print(1 - length(which(blong != TrnG))/length(blong))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.