Veri bilimcilerin ya da veri analizleriyle uğraşan akademi ve saha çalışanların karşılaştığı problemlerin başında veri setindeki değişkenlerde eksik gözlemlerle diğer bir deyişle missing data gelmektedir. Literatürde eksik veri atama yöntemleri arasında bir çok yöntem bulunmakla birlikte öne çıkan yöntemlerden bazıları şöyledir: 1. Eksik gözlemlere ortalama değer atama 2. Eksik gözlemlere 0 değeri atama 3. Eksik gözlemlere median değeri atama 4. Eksik gözlemlere PMM yöntemi ile atama 5. Eksik gözlemlere regresyon yöntemi ile atama 6. Eksik gözlemlere EM (Expectation-Maximization) yöntemi ile atama
Bahsedilen bu yöntemlerin dışında eksik veri atama yöntemlerinden makine öğrenme algoritmalarından ya da yöntemlerinden de yararlanabilmektedir. Bunlardan bazıları ise şöyle özetlenebilir: 1. Karar ağaçları (decision trees) yöntemi ile eksik gözlemlere atama yapma 2. KNN (K Nearest Neighbor) yöntemi ile eksik gözlemlere atama yapma 3. Rastgele Orman (RF) Algoritması ile eksik gözlemlere atama
Sayılan bu yöntemleri artırmak mümkündür. Bu çalışma kapsamında eksik veri atama yöntemi olarak ele alınacak ve üzerinde uygulama yapılacak yöntemler ise karar ağaçları, KNN ve RF makine öğrenme yöntemleri olacaktır. Çalışmada örnek uygulama yapılacak veri seti sentetik olarak üretilecektir. Diğer bir deyişle hipotetik olacaktır. Şimdi sırasıyla uygulama adımlarına geçebiliriz.
Aşağıda görüleceği üzere Kütüphane sayısını oldukça fazla tuttum. Bunun nedeni diğer çalışmalarda bu kütüphaneleri kullanmamdır.
library<-c("reactable","crosstalk","dplyr","tibble","tidyr","ggplot2","formattable","ggthemes","readr","readxl","ggpubr","formattable", "ggstance", "stringr","explore", "lubridate","leaflet", "tidytext", "scales", "data.table", "CCA", "pastecs", "gtools", "rmarkdown", "knitr", "gtsummary", "writexl", "ppcor", "reshape2", "GGally", "CCP", "Hmisc", "VIM", "philentropy", "haven","mice")
loading<-sapply(library, require, character.only = TRUE)
## Loading required package: reactable
## Loading required package: crosstalk
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Loading required package: tibble
## Loading required package: tidyr
## Loading required package: ggplot2
## Loading required package: formattable
## Loading required package: ggthemes
## Loading required package: readr
## Loading required package: readxl
## Loading required package: ggpubr
## Loading required package: ggstance
##
## Attaching package: 'ggstance'
## The following objects are masked from 'package:ggplot2':
##
## geom_errorbarh, GeomErrorbarh
## Loading required package: stringr
## Loading required package: explore
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
## Loading required package: leaflet
## Loading required package: tidytext
## Loading required package: scales
##
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
##
## col_factor
## The following objects are masked from 'package:formattable':
##
## comma, percent, scientific
## Loading required package: data.table
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:lubridate':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
## The following objects are masked from 'package:dplyr':
##
## between, first, last
## Loading required package: CCA
## Loading required package: fda
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
## Attaching package: 'fda'
## The following object is masked from 'package:graphics':
##
## matplot
## Loading required package: fields
## Loading required package: spam
## Loading required package: dotCall64
## Loading required package: grid
## Spam version 2.5-1 (2019-12-12) is loaded.
## Type 'help( Spam)' or 'demo( spam)' for a short introduction
## and overview of this package.
## Help for individual functions is also obtained by adding the
## suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
##
## Attaching package: 'spam'
## The following object is masked from 'package:Matrix':
##
## det
## The following objects are masked from 'package:base':
##
## backsolve, forwardsolve
## See https://github.com/NCAR/Fields for
## an extensive vignette, other supplements and source code
##
## Attaching package: 'fields'
## The following object is masked from 'package:explore':
##
## describe
## Loading required package: pastecs
##
## Attaching package: 'pastecs'
## The following objects are masked from 'package:data.table':
##
## first, last
## The following object is masked from 'package:tidyr':
##
## extract
## The following objects are masked from 'package:dplyr':
##
## first, last
## Loading required package: gtools
## Loading required package: rmarkdown
## Loading required package: knitr
## Loading required package: gtsummary
## Loading required package: writexl
## Loading required package: ppcor
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:gtsummary':
##
## select
## The following object is masked from 'package:formattable':
##
## area
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: reshape2
##
## Attaching package: 'reshape2'
## The following objects are masked from 'package:data.table':
##
## dcast, melt
## The following object is masked from 'package:tidyr':
##
## smiths
## Loading required package: GGally
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
##
## Attaching package: 'GGally'
## The following object is masked from 'package:explore':
##
## rescale01
## Loading required package: CCP
## Loading required package: Hmisc
## Loading required package: lattice
##
## Attaching package: 'lattice'
## The following object is masked from 'package:fda':
##
## melanoma
## Loading required package: survival
## Loading required package: Formula
##
## Attaching package: 'Hmisc'
## The following object is masked from 'package:fields':
##
## describe
## The following object is masked from 'package:explore':
##
## describe
## The following objects are masked from 'package:dplyr':
##
## src, summarize
## The following objects are masked from 'package:base':
##
## format.pval, units
## Loading required package: VIM
## Loading required package: colorspace
## VIM is ready to use.
## Suggestions and bug-reports can be submitted at: https://github.com/statistikat/VIM/issues
##
## Attaching package: 'VIM'
## The following object is masked from 'package:datasets':
##
## sleep
## Loading required package: philentropy
## Loading required package: haven
## Loading required package: mice
##
## Attaching package: 'mice'
## The following object is masked from 'package:CCA':
##
## cc
## The following objects are masked from 'package:spam':
##
## cbind, rbind
## The following object is masked from 'package:stats':
##
## filter
## The following objects are masked from 'package:base':
##
## cbind, rbind
as.vector(loading)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [31] TRUE TRUE TRUE TRUE TRUE TRUE
** Bu kısımda 4 değişkenli bir veri seti basit tekrarlı örneklem yöntemi kullanılarak üretilecektir.
atama=c(1:10000, rep(NA, 10000))
set.seed(61)#aynı sonuçları almak için
v1=sample(atama, 1000, replace=TRUE)
v2=sample(atama, 1000, replace=TRUE)
v3=sample(atama, 1000, replace=TRUE)
v4=sample(atama, 1000, replace=TRUE)
veri<-cbind(v1, v2,v3,v4) %>% as_tibble()
kable(head(veri, 20))#ilk 20 gözlem
v1 | v2 | v3 | v4 |
---|---|---|---|
1066 | 4703 | NA | 6745 |
2794 | 4877 | 6961 | NA |
NA | NA | 5195 | NA |
8980 | NA | NA | NA |
4962 | 475 | NA | 7929 |
5451 | NA | NA | NA |
NA | NA | NA | 8980 |
NA | NA | 7764 | 5097 |
NA | 9736 | 2428 | 5648 |
NA | 9363 | NA | 1057 |
7758 | 820 | 726 | 1659 |
6592 | 38 | 5296 | NA |
8060 | 184 | NA | NA |
4891 | NA | 9332 | NA |
4879 | 6856 | 2118 | NA |
NA | NA | NA | 9575 |
NA | NA | NA | 2694 |
6555 | 3404 | NA | NA |
3901 | NA | 8710 | 3917 |
9358 | NA | 8097 | 1597 |
Burada veri setinde değişkene göre eksik veri olan gözlem sayısı ile ilgili değişkendeki gözlem sayıları içindeki oranı verilmiştir.
explore_all(veri)
dt<-mice(veri, method = "cart")
##
## iter imp variable
## 1 1 v1 v2 v3 v4
## 1 2 v1 v2 v3 v4
## 1 3 v1 v2 v3 v4
## 1 4 v1 v2 v3 v4
## 1 5 v1 v2 v3 v4
## 2 1 v1 v2 v3 v4
## 2 2 v1 v2 v3 v4
## 2 3 v1 v2 v3 v4
## 2 4 v1 v2 v3 v4
## 2 5 v1 v2 v3 v4
## 3 1 v1 v2 v3 v4
## 3 2 v1 v2 v3 v4
## 3 3 v1 v2 v3 v4
## 3 4 v1 v2 v3 v4
## 3 5 v1 v2 v3 v4
## 4 1 v1 v2 v3 v4
## 4 2 v1 v2 v3 v4
## 4 3 v1 v2 v3 v4
## 4 4 v1 v2 v3 v4
## 4 5 v1 v2 v3 v4
## 5 1 v1 v2 v3 v4
## 5 2 v1 v2 v3 v4
## 5 3 v1 v2 v3 v4
## 5 4 v1 v2 v3 v4
## 5 5 v1 v2 v3 v4
kable(head(mice::complete(dt,1),20))# Atama sonrası ilk 20 gözlem
v1 | v2 | v3 | v4 |
---|---|---|---|
1066 | 4703 | 3503 | 6745 |
2794 | 4877 | 6961 | 9634 |
7876 | 9736 | 5195 | 96 |
8980 | 4480 | 8791 | 2613 |
4962 | 475 | 7395 | 7929 |
5451 | 5640 | 3672 | 2689 |
4024 | 1206 | 5292 | 8980 |
6209 | 7395 | 7764 | 5097 |
7829 | 9736 | 2428 | 5648 |
5534 | 9363 | 196 | 1057 |
7758 | 820 | 726 | 1659 |
6592 | 38 | 5296 | 8980 |
8060 | 184 | 7034 | 2368 |
4891 | 9478 | 9332 | 7387 |
4879 | 6856 | 2118 | 883 |
2731 | 3737 | 7388 | 9575 |
6460 | 9599 | 2776 | 2694 |
6555 | 3404 | 4282 | 4687 |
3901 | 5895 | 8710 | 3917 |
9358 | 9037 | 8097 | 1597 |
KNN algortiması hem sınıflandırma hem de regresyon problemlerinin çözümünde kullanılabilmektedir. Buradan bakıldığında hem kategorik hem de sürekli ve kesikli nitelikte olan değişkenlerde eksik veri gözlemlerinin atanmasında alternatif non-parametrik denetimli (supervised) yöntemlerden birisidir.
Esas olarak özellik benzerliğine dayanmaktadır. KNN, bir veri noktasının komşusuna ne kadar benzediğini kontrol eder ve veri noktasını en çok benzediği sınıfa göre sınıflandırır.
library(VIM)
eksikknn <- kNN(veri)
eksikknn[,1:4] %>% head(20) %>% kable() #ilk 20 gözlem
v1 | v2 | v3 | v4 |
---|---|---|---|
1066 | 4703 | 4437 | 6745 |
2794 | 4877 | 6961 | 1974 |
6324 | 2115 | 5195 | 2802 |
8980 | 3487 | 6443 | 2413 |
4962 | 475 | 9037 | 7929 |
5451 | 7332 | 1474 | 5638 |
6828 | 5071 | 3840 | 8980 |
7829 | 8593 | 7764 | 5097 |
4711 | 9736 | 2428 | 5648 |
5348 | 9363 | 3117 | 1057 |
7758 | 820 | 726 | 1659 |
6592 | 38 | 5296 | 2689 |
8060 | 184 | 6363 | 8145 |
4891 | 8661 | 9332 | 5710 |
4879 | 6856 | 2118 | 6167 |
6988 | 2044 | 8365 | 9575 |
4756 | 2465 | 3815 | 2694 |
6555 | 3404 | 9751 | 6620 |
3901 | 2139 | 8710 | 3917 |
9358 | 3880 | 8097 | 1597 |
rfdf <- mice(veri, meth = "rf", ntree = 50, seed = 61)#ntree yetiştirilecek ağaç sayısını göstermektedir.
##
## iter imp variable
## 1 1 v1 v2 v3 v4
## 1 2 v1 v2 v3 v4
## 1 3 v1 v2 v3 v4
## 1 4 v1 v2 v3 v4
## 1 5 v1 v2 v3 v4
## 2 1 v1 v2 v3 v4
## 2 2 v1 v2 v3 v4
## 2 3 v1 v2 v3 v4
## 2 4 v1 v2 v3 v4
## 2 5 v1 v2 v3 v4
## 3 1 v1 v2 v3 v4
## 3 2 v1 v2 v3 v4
## 3 3 v1 v2 v3 v4
## 3 4 v1 v2 v3 v4
## 3 5 v1 v2 v3 v4
## 4 1 v1 v2 v3 v4
## 4 2 v1 v2 v3 v4
## 4 3 v1 v2 v3 v4
## 4 4 v1 v2 v3 v4
## 4 5 v1 v2 v3 v4
## 5 1 v1 v2 v3 v4
## 5 2 v1 v2 v3 v4
## 5 3 v1 v2 v3 v4
## 5 4 v1 v2 v3 v4
## 5 5 v1 v2 v3 v4
kable(head(mice::complete(rfdf,1),20))# Atama sonrası ilk 20 gözlem
v1 | v2 | v3 | v4 |
---|---|---|---|
1066 | 4703 | 4691 | 6745 |
2794 | 4877 | 6961 | 3231 |
5555 | 9750 | 5195 | 2707 |
8980 | 2271 | 2724 | 3566 |
4962 | 475 | 6859 | 7929 |
5451 | 4321 | 657 | 6520 |
9074 | 5923 | 5824 | 8980 |
4866 | 4318 | 7764 | 5097 |
4641 | 9736 | 2428 | 5648 |
8799 | 9363 | 4960 | 1057 |
7758 | 820 | 726 | 1659 |
6592 | 38 | 5296 | 6106 |
8060 | 184 | 7318 | 8948 |
4891 | 8292 | 9332 | 2302 |
4879 | 6856 | 2118 | 6498 |
7736 | 836 | 5296 | 9575 |
9008 | 602 | 8097 | 2694 |
6555 | 3404 | 2792 | 4343 |
3901 | 6124 | 8710 | 3917 |
9358 | 682 | 8097 | 1597 |
Faydalı olması dileğiyle. Bilimle ve teknolojiyle kalınız. Saygılarımla…