I tried to implement Kernel Density Estimation with Multivariate data in R, but it is first time for me, so, I'm not sure is it correct approach or not.
I got approach as like below with 176 obs. of 6 variables data.
# data structure as like below.
# If num is 0 is normal, if num is 1, it would be outlier.
str(df)
'data.frame': 176 obs. of 6 variables:
$ age : int 30 50 50 50 50 60 50 40 50 40 ...
$ trestbps: int 130 130 130 130 130 130 130 130 130 130 ...
$ chol : int 198 245 221 288 205 309 240 243 289 250 ...
$ thalach : int 130 166 164 159 184 131 154 152 124 179 ...
$ oldpeak : num 1.6 2.4 0 0.2 0 1.8 0.6 0 1 0 ...
$ num : int 0 0 0 0 0 0 0 0 0 0 ...
Because it is multivariate data, I got approach as like below.
#6th column is the answer of outlier, So do not use it.
df <- transform(df, age_s = scale(df$age), trestbps_s = scale(df$trestbps), chol_s = scale(df$chol), thalach_s = scale(df$thalach), oldpeak_s = scale(df$oldpeak))
df1 <- df[-c(1,2,3,4,5,6)]
df1 <- as.matrix(df1)
kde_df1 <- density(df1, n = 176)
kde_df1
plot(kde_df1)
densities <- as.data.frame(kde_df1$y)
names(densities) <- c("dy")
min_density <- min(densities$dy)
mean_density <- mean(densities$dy)
bench <- min_density/mean_density
> bench
[1] 0.0002983908606
> summary(densities$dy)
Min. 1st Qu. Median Mean 3rd Qu.
0.0000298944 0.0029706735 0.0217674956 0.1001852137 0.1766697744
Max.
0.4760445127
# Predict Outliers
densities$outlier <- NA
densities$outlier <- ifelse(densities$dy < bench, "1", "0")
densities1 <- densities
densities1$outlier <- as.factor(densities1$outlier)
levels(densities1$outlier)
> summary(densities1$outlier)
0 1
169 7
> densities1$outlier
[1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[37] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[73] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[109] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[145] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
Levels: 0 1
Try to check Confusion Matrix
library(caret)
dfAnswer <- df
dfAnswer$num <- as.factor(dfAnswer$num)
CM <- confusionMatrix(densities1$outlier, dfAnswer$num)
> CM
Confusion Matrix and Statistics
Reference
Prediction 0 1
0 157 12
1 3 4
Accuracy : 0.9147727
95% CI : (0.8633274, 0.9515133)
No Information Rate : 0.9090909
P-Value [Acc > NIR] : 0.4619822
Kappa : 0.3096234
Mcnemar's Test P-Value : 0.0388671
Sensitivity : 0.9812500
Specificity : 0.2500000
Pos Pred Value : 0.9289941
Neg Pred Value : 0.5714286
Prevalence : 0.9090909
Detection Rate : 0.8920455
Detection Prevalence : 0.9602273
Balanced Accuracy : 0.6156250
'Positive' Class : 0
Am I doing right? If it is not, can you give me some advices to get approach correctly?
Thank you.