R Markdown

#Paketleri kütüphaneden çekmek için;
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── 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
library(ggplot2)
#Veri setini okutmak için; 
library(readxl)
student_mat <- read_xlsx("C:/Users/Huawei/Desktop/OLC731/student-mat(in).xlsx")
#Veri setine göz atmak için glimpse() fonksiyonu;
glimpse(student_mat)
## Rows: 395
## Columns: 33
## $ school     <chr> "GP", "GP", "GP", "GP", "GP", "GP", "GP", "GP", "GP", "GP",…
## $ sex        <chr> "F", "F", "F", "F", "F", "M", "M", "F", "M", "M", "F", "F",…
## $ age        <dbl> 18, 17, 15, 15, 16, 16, 16, 17, 15, 15, 15, 15, 15, 15, 15,…
## $ address    <chr> "U", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U",…
## $ famsize    <chr> "GT3", "GT3", "LE3", "GT3", "GT3", "LE3", "LE3", "GT3", "LE…
## $ Pstatus    <chr> "A", "T", "T", "T", "T", "T", "T", "A", "A", "T", "T", "T",…
## $ Medu       <dbl> 4, 1, 1, 4, 3, 4, 2, 4, 3, 3, 4, 2, 4, 4, 2, 4, 4, 3, 3, 4,…
## $ Fedu       <dbl> 4, 1, 1, 2, 3, 3, 2, 4, 2, 4, 4, 1, 4, 3, 2, 4, 4, 3, 2, 3,…
## $ Mjob       <chr> "at_home", "at_home", "at_home", "health", "other", "servic…
## $ Fjob       <chr> "teacher", "other", "other", "services", "other", "other", …
## $ reason     <chr> "course", "course", "other", "home", "home", "reputation", …
## $ guardian   <chr> "mother", "father", "mother", "mother", "father", "mother",…
## $ traveltime <dbl> 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 3, 1, 2, 1, 1, 1, 3, 1, 1,…
## $ studytime  <dbl> 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 1, 2, 3, 1, 3, 2, 1, 1,…
## $ failures   <dbl> 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,…
## $ schoolsup  <chr> "yes", "no", "yes", "no", "no", "no", "no", "yes", "no", "n…
## $ famsup     <chr> "no", "yes", "no", "yes", "yes", "yes", "no", "yes", "yes",…
## $ paid       <chr> "no", "no", "yes", "yes", "yes", "yes", "no", "no", "yes", …
## $ activities <chr> "no", "no", "no", "yes", "no", "yes", "no", "no", "no", "ye…
## $ nursery    <chr> "yes", "no", "yes", "yes", "yes", "yes", "yes", "yes", "yes…
## $ higher     <chr> "yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes", "ye…
## $ internet   <chr> "no", "yes", "yes", "yes", "no", "yes", "yes", "no", "yes",…
## $ romantic   <chr> "no", "no", "no", "yes", "no", "no", "no", "no", "no", "no"…
## $ famrel     <dbl> 4, 5, 4, 3, 4, 5, 4, 4, 4, 5, 3, 5, 4, 5, 4, 4, 3, 5, 5, 3,…
## $ freetime   <dbl> 3, 3, 3, 2, 3, 4, 4, 1, 2, 5, 3, 2, 3, 4, 5, 4, 2, 3, 5, 1,…
## $ goout      <dbl> 4, 3, 2, 2, 2, 2, 4, 4, 2, 1, 3, 2, 3, 3, 2, 4, 3, 2, 5, 3,…
## $ Dalc       <dbl> 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1,…
## $ Walc       <dbl> 1, 1, 3, 1, 2, 2, 1, 1, 1, 1, 2, 1, 3, 2, 1, 2, 2, 1, 4, 3,…
## $ health     <dbl> 3, 3, 3, 5, 5, 5, 3, 1, 1, 5, 2, 4, 5, 3, 3, 2, 2, 4, 5, 5,…
## $ absences   <dbl> 6, 4, 10, 2, 4, 10, 0, 6, 0, 0, 0, 4, 2, 2, 0, 4, 6, 4, 16,…
## $ G1         <dbl> 5, 5, 7, 15, 6, 15, 12, 6, 16, 14, 10, 10, 14, 10, 14, 14, …
## $ G2         <dbl> 6, 5, 8, 14, 10, 15, 12, 5, 18, 15, 8, 12, 14, 10, 16, 14, …
## $ G3         <dbl> 6, 6, 10, 15, 10, 15, 11, 6, 19, 15, 9, 12, 14, 11, 16, 14,…

Alıştırma 1 Veri Düzenleme (dplyr)

Belirli sütunları seçin (select())

#Okul, cinsiyet, yaş ve öğrencinin ev adresi türü değişkenlerini seçme;
student_mat_1 <- select(student_mat, school, sex, age, address)

Belirli bir koşula göre filtreleme yapın (filter())

#Cinsiyeti kadın olanları seçme;
student_mat_2 <- student_mat %>%
  filter(sex %in% c("F"))
#ya da
student_mat %>% filter(sex == "F")
## # A tibble: 208 × 33
##    school sex     age address famsize Pstatus  Medu  Fedu Mjob     Fjob   reason
##    <chr>  <chr> <dbl> <chr>   <chr>   <chr>   <dbl> <dbl> <chr>    <chr>  <chr> 
##  1 GP     F        18 U       GT3     A           4     4 at_home  teach… course
##  2 GP     F        17 U       GT3     T           1     1 at_home  other  course
##  3 GP     F        15 U       LE3     T           1     1 at_home  other  other 
##  4 GP     F        15 U       GT3     T           4     2 health   servi… home  
##  5 GP     F        16 U       GT3     T           3     3 other    other  home  
##  6 GP     F        17 U       GT3     A           4     4 other    teach… home  
##  7 GP     F        15 U       GT3     T           4     4 teacher  health reput…
##  8 GP     F        15 U       GT3     T           2     1 services other  reput…
##  9 GP     F        16 U       GT3     T           4     4 health   other  home  
## 10 GP     F        16 U       GT3     T           4     4 services servi… reput…
## # ℹ 198 more rows
## # ℹ 22 more variables: guardian <chr>, traveltime <dbl>, studytime <dbl>,
## #   failures <dbl>, schoolsup <chr>, famsup <chr>, paid <chr>,
## #   activities <chr>, nursery <chr>, higher <chr>, internet <chr>,
## #   romantic <chr>, famrel <dbl>, freetime <dbl>, goout <dbl>, Dalc <dbl>,
## #   Walc <dbl>, health <dbl>, absences <dbl>, G1 <dbl>, G2 <dbl>, G3 <dbl>

Yeni bir sütun oluşturun (mutate())

#G1, G2 ve G3 puanlarının toplamından "total" adında yeni bir sütun oluşturma;
student_mat_3 <- mutate(student_mat, total=G1+G2+G3)

Özetleme işlemleri yapın (group_by() + summarise())

#Hesaplanan "total" puanına göre gruplama yapma 
student_mat_3 %>%
  group_by(total) %>%
  summarise(mean(G3))
## # A tibble: 54 × 2
##    total `mean(G3)`
##    <dbl>      <dbl>
##  1     4      0    
##  2     5      0    
##  3     6      0    
##  4     7      0    
##  5     8      0    
##  6     9      0    
##  7    10      0    
##  8    11      0    
##  9    12      0    
## 10    13      0.833
## # ℹ 44 more rows

Veriyi sıralayın (arrange())

student_mat_4 <- arrange(student_mat, desc(G3))
#ya da
student_mat %>%
  arrange(desc(G3))
## # A tibble: 395 × 33
##    school sex     age address famsize Pstatus  Medu  Fedu Mjob     Fjob   reason
##    <chr>  <chr> <dbl> <chr>   <chr>   <chr>   <dbl> <dbl> <chr>    <chr>  <chr> 
##  1 GP     M        16 U       GT3     T           4     3 health   servi… reput…
##  2 GP     M        15 U       LE3     A           3     2 services other  home  
##  3 GP     M        15 U       LE3     A           4     4 teacher  teach… course
##  4 GP     M        15 U       LE3     T           4     2 teacher  other  course
##  5 GP     F        18 U       GT3     T           2     2 at_home  at_ho… other 
##  6 MS     F        18 R       LE3     T           4     4 other    other  reput…
##  7 GP     M        15 U       LE3     T           4     3 teacher  servi… home  
##  8 GP     M        15 U       GT3     T           4     4 services teach… course
##  9 GP     F        15 U       GT3     T           4     3 services other  reput…
## 10 GP     M        15 U       GT3     A           3     4 services other  course
## # ℹ 385 more rows
## # ℹ 22 more variables: guardian <chr>, traveltime <dbl>, studytime <dbl>,
## #   failures <dbl>, schoolsup <chr>, famsup <chr>, paid <chr>,
## #   activities <chr>, nursery <chr>, higher <chr>, internet <chr>,
## #   romantic <chr>, famrel <dbl>, freetime <dbl>, goout <dbl>, Dalc <dbl>,
## #   Walc <dbl>, health <dbl>, absences <dbl>, G1 <dbl>, G2 <dbl>, G3 <dbl>

Alıştırma 2 Görselleştirme (ggplot2)

#Datadaki bireylerin yaşa göre dağılımı
student_mat %>%
count(age)
## # A tibble: 8 × 2
##     age     n
##   <dbl> <int>
## 1    15    82
## 2    16   104
## 3    17    98
## 4    18    82
## 5    19    24
## 6    20     3
## 7    21     1
## 8    22     1
#Histogram çizimi
student_mat$age
##   [1] 18 17 15 15 16 16 16 17 15 15 15 15 15 15 15 16 16 16 17 16 15 15 16 16 15
##  [26] 16 15 15 16 16 15 15 15 15 16 15 15 16 15 15 16 15 15 15 16 15 16 16 15 15
##  [51] 16 15 15 15 15 16 15 15 15 16 16 16 16 16 15 16 15 16 15 15 16 15 15 16 16
##  [76] 15 15 16 17 16 15 15 15 15 15 15 16 15 16 16 16 15 16 16 15 15 16 16 16 16
## [101] 16 16 15 15 15 15 15 16 15 16 15 16 16 15 15 16 15 16 17 15 15 15 16 16 16
## [126] 15 15 19 18 16 15 15 17 16 15 15 17 16 16 15 15 16 15 16 17 15 15 15 16 15
## [151] 18 16 15 19 17 15 17 18 16 16 17 15 16 17 17 16 16 16 16 16 16 16 17 16 16
## [176] 17 16 17 16 17 16 16 17 17 16 17 16 16 17 17 16 17 17 16 16 17 17 16 17 16
## [201] 16 16 17 17 16 17 16 16 16 17 19 17 16 18 17 17 17 18 17 17 17 17 16 18 16
## [226] 18 17 17 18 17 17 17 17 16 16 16 17 16 17 18 17 17 16 16 18 16 17 22 18 16
## [251] 18 16 18 16 17 17 17 19 18 17 18 18 18 17 18 18 17 18 18 18 19 18 18 17 17
## [276] 17 18 18 18 18 17 17 18 18 17 17 18 17 18 18 18 17 18 17 18 17 19 18 18 18
## [301] 18 17 17 17 19 18 20 19 19 19 19 19 19 19 19 19 18 18 17 18 17 17 17 17 17
## [326] 18 17 17 17 17 18 17 18 18 18 17 19 17 18 17 19 18 18 17 18 18 18 18 17 18
## [351] 19 17 18 19 17 18 17 17 18 18 18 18 18 17 17 18 18 17 18 18 19 18 17 17 18
## [376] 18 20 18 18 17 18 18 17 19 18 18 18 19 18 18 20 17 21 18 19
hist(student_mat$age,main = "Yasa Gore Dagilim-Histogram", 
     xlab = "yas", ylab = "frekans",
     col="skyblue", border = "pink", ylim = c(0, 110))

#Okul türüne göre G1 ve G2 puanlarının dağılım scatter plot
ggplot(student_mat, aes(x=G1, y=G2, color=school))+
  geom_point()+
  labs(title="Okul Turune Gore G1 ve G2 Puanları", x= "G1", y="G2",
       color="Okul Turu")

#Yorum: Gabriel Pereira okulunda G2 puanı 0 olan öğrenciler mevcut iken Mousinho da Silveira mevcut değildir.
#scatter plot cizimi
library(ggplot2)
ggplot(student_mat, aes(x = studytime, y = G3, color = school)) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    title = "Okula Gore Ders Calisma Suresi ve G3 Puanlari",
    x = "studytime",
    y = "G3",
    color = "school"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

#Yorum: Ders çalışma süresi arttıkça Gabriel Pereira okulunda G3 puanında artış gözlenmekle birlikte bu artış oranı minimal düzeydedir. Bununla birlikte Mousinho da Silveira okulunda ders çalışma süresi arttığında G3 puanında daha belirgin bir artış gözlemlenmiştir. Ek olarak ousinho da Silveira okulunda on saatin üzerinde çalışan öğrenci bulunmamaktadır.
#(scatter plot)arkadaşlarla dışarı çıkma sıklığı ile G3 başarısı arasındaki ilişki
library(ggplot2)
ggplot(student_mat, aes(x=goout, y=G3))+
  geom_point()+
  geom_smooth(method="lm", se=FALSE, color="purple")+
  labs(title="Arkadaslarla dısarı cikma sikliği 
       ile G3 basarisi arasındaki iliski",x="disari cikma",
       y="G3")+
  theme_light()
## `geom_smooth()` using formula = 'y ~ x'

#Yorum:Arkadaşlarla dışarı çıkma sıklığı arttıkça G3 puanlarında düşüş gözlemlenmektedir.
#boxplot çizme
library(ggplot2)
qplot(student_mat$higher, student_mat$G2, geom = "boxplot") +
      labs(title = "Devam istegi ve G2 puanı",
           x="yuksekogrenime devam istegi", y="G2 puanı")
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#Yorum: Yükseköğrenime devam isteği olduğunda G2 puanı daha yüksektir.
#bar graph1
library(ggplot2)
ggplot(student_mat, aes(x=studytime, y=G1)) +
  geom_bar(stat="identity", fill="lightgreen", color="yellow")   

#Yorum: (1 – <2 saat, 2 – 2–5 saat,3 – 5–10 saat, 4 – >10 saat) ders çalışma süresi 5 saate kadar olduğunda G1 puanı artmakta sonrasında azalmaktadır.
#bar graph2  
library(dplyr)
library(ggplot2)
mean_table <- student_mat %>%
    group_by(studytime) %>%
    summarise(mean_G2 = mean(G2, na.rm = TRUE))
  
ggplot(mean_table, aes(x = factor(studytime), y = mean_G2)) +
    geom_col(fill = "lightblue", color = "yellow") +
    labs(
      title = "Calisma Suresine Gore Ortalama G2 Notu",
      x = "Calisma Suresi",
      y = "Ortalama G2"
    ) +
    theme_minimal()

#Yorum: Ders çalışma süresi arttıkça G2 puanlarının ortalamalarında artış görülmektedir.
#Öğrendiklerim Chunk'ı
#Farklı geom komutlarıyla farklı türde grafikler çizilebildiğini öğrendim. Aynı zamanda chunk oluşturmayı da öğrendim:) R dersi benim için keyifli. Sadece çok basit yerlerde yaptığım hataları bulmak için gereğinden fazla zaman kaybettiğim olabiliyor. Biraz daha pratik uygulamalar yapmak faydalı oluyor. Özellikle datacamp'tan yaptığımız çalışmaların kod yazmaya alışma anlamında öğretici olduğunu düşünmekteyim.