## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
##
## Attaching package: 'stevemisc'
##
## The following object is masked from 'package:lubridate':
##
## dst
##
## The following object is masked from 'package:dplyr':
##
## tbl_df
##
## Attaching package: 'summarytools'
##
## The following object is masked from 'package:tibble':
##
## view
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
## Zorunlu paket yükleniyor: ggpp
## Registered S3 methods overwritten by 'ggpp':
## method from
## heightDetails.titleGrob ggplot2
## widthDetails.titleGrob ggplot2
##
## Attaching package: 'ggpp'
##
## The following object is masked from 'package:ggplot2':
##
## annotate
##
## Attaching package: 'psych'
##
## The following object is masked from 'package:outliers':
##
## outlier
##
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
##
## Attaching package: 'sur'
##
## The following object is masked from 'package:psych':
##
## skew
## corrplot 0.95 loaded
##
## Attaching package: 'olsrr'
##
## The following object is masked from 'package:datasets':
##
## rivers
## pregnant glucose pressure triceps insulin mass pedigree age diabetes
## 1 6 148 72 35 NA 33.6 0.627 50 pos
## 2 1 85 66 29 NA 26.6 0.351 31 neg
## 3 8 183 64 NA NA 23.3 0.672 32 pos
## 4 1 89 66 23 94 28.1 0.167 21 neg
## 5 0 137 40 35 168 43.1 2.288 33 pos
## 6 5 116 74 NA NA 25.6 0.201 30 neg
## [1] TRUE
## # A tibble: 9 × 3
## variable n_miss pct_miss
## <chr> <int> <num>
## 1 insulin 374 48.7
## 2 triceps 227 29.6
## 3 pressure 35 4.56
## 4 mass 11 1.43
## 5 glucose 5 0.651
## 6 pregnant 0 0
## 7 pedigree 0 0
## 8 age 0 0
## 9 diabetes 0 0
#5 farklı değişkende eksik veri var
veri <- veri %>%
mutate(insulin = ifelse(is.na(insulin), mean(insulin, na.rm =TRUE),insulin)) %>% na.omit()
any_na(veri) #en fazla eksik veriye sahip insulin değişkenini ortalama ile doldurduk ve diğer eksik verileri sildik
## [1] FALSE
## 'data.frame': 532 obs. of 9 variables:
## $ pregnant: num 6 1 1 0 3 2 1 5 0 1 ...
## $ glucose : num 148 85 89 137 78 197 189 166 118 103 ...
## $ pressure: num 72 66 66 40 50 70 60 72 84 30 ...
## $ triceps : num 35 29 23 35 32 45 23 19 47 38 ...
## $ insulin : num 156 156 94 168 88 ...
## $ mass : num 33.6 26.6 28.1 43.1 31 30.5 30.1 25.8 45.8 43.3 ...
## $ pedigree: num 0.627 0.351 0.167 2.288 0.248 ...
## $ age : num 50 31 21 33 26 53 59 51 31 33 ...
## $ diabetes: Factor w/ 2 levels "neg","pos": 2 1 1 2 2 2 2 2 2 1 ...
## - attr(*, "na.action")= 'omit' Named int [1:236] 3 6 8 10 11 12 13 16 18 22 ...
## ..- attr(*, "names")= chr [1:236] "3" "6" "8" "10" ...
veri$diabetes_say <- as.numeric(factor(veri$diabetes)) #2 değeri positive 1 değeri negative olarak değiştirildi.
## setting plain.ascii to FALSE
## ### Frequencies
## #### veri$diabetes
## **Type:** Factor
##
## | | Freq | % | % Cum. |
## |----------:|-----:|-------:|-------:|
## | **neg** | 355 | 66.73 | 66.73 |
## | **pos** | 177 | 33.27 | 100.00 |
## | **Total** | 532 | 100.00 | 100.00 |
#eksik veri almasını istemediğimiz için temizlenmiş veri seti ile aynı sonular geldi
freq(veri$diabetes_say,
round.digits=2,report.nas = FALSE,
style = "rmarkdown")
## setting plain.ascii to FALSE
## ### Frequencies
## #### veri$diabetes_say
## **Type:** Numeric
##
## | | Freq | % | % Cum. |
## |----------:|-----:|-------:|-------:|
## | **1** | 355 | 66.73 | 66.73 |
## | **2** | 177 | 33.27 | 100.00 |
## | **Total** | 532 | 100.00 | 100.00 |
Elde edilen sonuçlara bakıldığında hemen hemen değişkenlerden biri diğerinin iki katı. Sosyal alanda yapılacak çalışmalarda bu oran sorun olabilirdi ancak sağlık alanında sorun olacağını zannetmiyorum. Çünkü bu oranlar sağlık alanında oldukça yaygın oranlardır.
library(knitr)
freq(veri$diabetes,report.nas = FALSE) %>%
kable(format='markdown',
caption="Frekans Tablosu",digits = 2)
Freq | % Valid | % Valid Cum. | % Total | % Total Cum. | |
---|---|---|---|---|---|
neg | 355 | 66.73 | 66.73 | 66.73 | 66.73 |
pos | 177 | 33.27 | 100.00 | 33.27 | 100.00 |
0 | NA | NA | 0.00 | 100.00 | |
Total | 532 | 100.00 | 100.00 | 100.00 | 100.00 |
library(outliers)
z.scores <- veri %>% #z skorları hesaplandı. z.scores veri setinde görüntülendi
select(1:8) %>%
scores(type = "z") %>%
round(2)
head(z.scores)
## pregnant glucose pressure triceps insulin mass pedigree age
## 1 0.75 0.87 0.04 0.55 0.00 0.10 0.36 1.71
## 2 -0.76 -1.16 -0.45 -0.02 0.00 -0.91 -0.44 -0.06
## 3 -0.76 -1.03 -0.45 -0.59 -0.61 -0.70 -0.98 -0.99
## 4 -1.06 0.52 -2.56 0.55 0.12 1.48 5.18 0.13
## 5 -0.16 -1.39 -1.75 0.27 -0.67 -0.27 -0.74 -0.52
## 6 -0.46 2.45 -0.12 1.50 3.80 -0.35 -1.00 1.99
summarytools::descr(z.scores,
stats = c("min", "max"),
transpose = TRUE, #değişkenlerdeki max ve min z değerleri görülebilir.
headings = FALSE)
##
## Min Max
## -------------- ------- ------
## age -0.99 4.59
## glucose -2.10 2.52
## insulin -1.39 6.77
## mass -2.13 4.97
## pedigree -1.21 5.56
## pregnant -1.06 4.07
## pressure -3.86 3.13
## triceps -2.11 6.63
age, insulin, mass, pedigree, pregnant ve triceps değişkenlerin uç değerler görülmektedir. (-+4 değeri dışında)
70 ve 80 li yaşlar bu veri seti için uçdeğerdir.
700 ve üzeri insulin değerleri uç değer olarak alınabilir.
library(ggpmisc) #grafik üzerinde ortalama çizgisi gösterme
mean_age <- mean(veri$age)
mean_insulin <- mean(veri$insulin)
ggplot(veri, aes(x = age)) + geom_histogram() +
geom_vline(xintercept =31.614, color = "red",
linetype = "dashed") +
annotate("text", label = "Ort = 31.614", x = 30, y = 100, color ="black")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(veri, aes(x = insulin)) + geom_histogram() +
geom_vline(xintercept =155.922, color = "red",
linetype = "dashed") +
annotate("text", label = "Ort = 155.922", x = 250, y = 100, color ="black")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(veri, aes(x = age)) + #yoğunluk grafiği
geom_histogram(aes(y=..density..))+
geom_density(alpha=.5, fill="#0c4c8a") +
theme_minimal()
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
library(plotly)
plot_ly(x = veri$age, type = "histogram", #interaktif grafik
histnorm = "probability")
## [1] 61 63 65 81 63 62 62 70 63
## [1] 150 180 245 318 333 403 406 462 529
#hocam baktım ama bu satırlardaki değerler uç değerler değil.
## pregnant glucose pressure triceps insulin mass pedigree age diabetes
## 224 7 142 60 33 190.0000 28.8 0.687 61 neg
## 264 3 142 80 15 155.5482 32.4 0.200 63 neg
## 363 5 103 108 37 155.5482 39.2 0.305 65 neg
## 460 9 134 74 33 60.0000 25.9 0.460 81 neg
## 480 4 132 86 31 155.5482 28.0 0.419 63 neg
## 580 2 197 70 99 155.5482 34.7 0.575 62 pos
## 583 12 121 78 17 155.5482 26.5 0.259 62 neg
## 667 4 145 82 18 155.5482 32.5 0.235 70 pos
## 764 10 101 76 48 180.0000 32.9 0.171 63 neg
## diabetes_say
## 224 1
## 264 1
## 363 1
## 460 1
## 480 1
## 580 2
## 583 1
## 667 2
## 764 1
Yukarıdaki kod gerçek değerleri verdi.
out_text <- ifelse(1:nrow(veri) %in% out_ind, paste("Satır:", out_ind), NA)
plot_ly(y = veri$age, type = 'box', text = out_text,) #interaktif kutu grafiği ve satır no
plot_ly(y = veri$age, type = 'box') %>%
layout(title = 'Box Plot',
annotations = list( x = -0.01, y = boxplot.stats(veri$age)$out,
text = paste(out_ind), showarrow = FALSE, #uç değerlerin satırlarını gösterir.
xanchor = "right"))
ggplot(veri, aes(x = factor(diabetes_say),
y = age, fill = factor(diabetes_say))) +
geom_boxplot() +
theme_minimal()
Grafik incelendiğinde diyabeti olanların yaş ortalamasının yüksek olduğu
görülmektedir. Diyabetsiz kişilere bakıldığında uç değerlerin daha çok
olduğu görülmektedir. Ki bu istenilen bir durumdur. Genelde yaşlanmayla
beraber sağlık sorunları artmaktadır. Burada Diyabetsiz kişiler arasında
yaşça büyük kişiler uç değer olarak belirlenmiştir.
age, insulin, mass, pedigree, pregnant ve triceps değişkenlerin uç değerler görülmektedir. Bunlar içinde uç değer bakalım.
ggplot(veri, aes(x = factor(diabetes_say),
y = insulin, fill = factor(diabetes_say))) +
geom_boxplot() +
theme_minimal()
Diyabet olan bireylerde düşük insülin değerleri uç değer olarak
belirlenmiş. Diyabet olan bireylerde insülin seviyelerinin yüksek olması
beklenir.
ggplot(veri, aes(x = factor(diabetes_say),
y = mass, fill = factor(diabetes_say))) +
geom_boxplot() +
theme_minimal() #1 - 2 +
Diyabeti olanlarda yüksek vücut kitle indeksleri uç değer olarak belirlenmiş.
ggplot(veri, aes(x = factor(diabetes_say),
y = pedigree, fill = factor(diabetes_say))) +
geom_boxplot() +
theme_minimal()
Diyabeti olanlarda yüksek ailevi yatkınlık değeri uç değer olarak
belirlenmiş. Ailenizde bulunma oranı yüksekse zaten diyabet olma
olasılığı artar. Hasta olmayanlar için yüksek olasılık değerleri uç
değer olarak belirlenmiş.
## pregnant glucose pressure triceps insulin mass pedigree age diabetes
## 325 2 112 75 32 155.5482 35.7 0.148 21 neg
## 386 1 119 54 13 50.0000 22.3 0.205 24 neg
## 522 3 124 80 33 130.0000 33.2 0.305 26 neg
## 665 6 115 60 39 155.5482 33.7 0.245 40 pos
## 696 7 142 90 24 480.0000 30.4 0.128 43 pos
## NA NA NA NA NA NA NA NA NA <NA>
## NA.1 NA NA NA NA NA NA NA NA <NA>
## NA.2 NA NA NA NA NA NA NA NA <NA>
## NA.3 NA NA NA NA NA NA NA NA <NA>
## diabetes_say
## 325 1
## 386 1
## 522 1
## 665 2
## 696 2
## NA NA
## NA.1 NA
## NA.2 NA
## NA.3 NA
library(psych)
veri <- veri2[,1:8]
md <- mahalanobis(veri, center = colMeans(veri), cov = cov(veri))
head(md,20)
## 1 2 4 5 7 9 14 15
## 4.211623 4.202880 2.440669 39.266125 5.424962 29.199067 65.686699 8.138376
## 17 19 20 21 24 25 26 28
## 6.927587 25.564426 1.665199 4.699158 8.693667 8.003965 5.370205 3.272756
## 29 31 32 33
## 13.250736 13.073389 4.460285 4.540318
Mahalanobis uzaklığı kritik değer belirleme
## [1] 26.12448
Mahalonobis uzaklığı değerleri ki-kare ile değerlendirilir (serbestlik derecesi bağımsız değişken sayısına eşittir). Buna göre 20.51501 kritik değerinden büyük olan değerler 0.001 alfa düzeyinde istatistiksel olarak anlamlı olarak değerlendirilir.
## pregnant glucose pressure triceps insulin mass pedigree age
## 5 0 137 40 35 168.0000 43.1 2.288 33
## 9 2 197 70 45 543.0000 30.5 0.158 53
## 14 1 189 60 23 846.0000 30.1 0.398 59
## 126 1 88 30 42 99.0000 55.0 0.496 26
## 178 0 129 110 46 130.0000 67.1 0.319 26
## 229 4 197 70 39 744.0000 36.7 2.329 31
## 248 0 165 90 33 680.0000 52.3 0.427 23
## 371 3 173 82 48 465.0000 38.4 2.137 25
## 446 0 180 78 63 14.0000 59.4 2.420 25
## 460 9 134 74 33 60.0000 25.9 0.460 81
## 580 2 197 70 99 155.5482 34.7 0.575 62
## 585 8 124 76 24 600.0000 28.7 0.687 52
5-9-14-126-178-229-248-371-446-460 subno.lu bireyler için
Mahalonobis uzaklık değerleri kritik değerden büyüktür. Bu gözlemler
çok değişkenli uç değerler olarak değerlendirilir