# -------------------------------------------
# CONTOH ANALISIS KORELASI
# Prodi Matematika - Statistik Dasar
# -------------------------------------------
library(readr)
# Membaca data
data_raw <- read_csv("C:/Users/Hype GLK/Downloads//Gaming and Mental Health.csv")
## Rows: 1000 Columns: 27
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): record_id, gender, game_genre, primary_game, gaming_platform, slee...
## dbl (11): age, daily_gaming_hours, sleep_hours, grades_gpa, work_productivit...
## lgl (5): withdrawal_symptoms, loss_of_other_interests, continued_despite_pr...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Membuat data frame sesuai permintaan
daily_game_hours = data_raw$daily_gaming_hours
sleep_hours = data_raw$sleep_hours
data <- data.frame(
daily_game_hours = data_raw$daily_gaming_hours,
sleep_hours = data_raw$sleep_hours
)
# Menampilkan data
head(data)
## daily_game_hours sleep_hours
## 1 11.1 3.7
## 2 3.0 7.2
## 3 7.6 4.4
## 4 7.2 5.1
## 5 6.8 3.4
## 6 2.1 7.5
# Statistik deskriptif sederhana
summary(data)
## daily_game_hours sleep_hours
## Min. : 0.500 Min. :3.000
## 1st Qu.: 4.100 1st Qu.:4.800
## Median : 6.000 Median :5.700
## Mean : 6.151 Mean :5.738
## 3rd Qu.: 8.025 3rd Qu.:6.600
## Max. :15.100 Max. :9.000
# Standar deviasi
sd(daily_game_hours, na.rm = TRUE)
## [1] 2.867194
sd(sleep_hours, na.rm = TRUE)
## [1] 1.441213
hasil_korelasi <- cor.test(daily_game_hours,
sleep_hours,
method = "pearson",
use = "complete.obs")
print(hasil_korelasi)
##
## Pearson's product-moment correlation
##
## data: daily_game_hours and sleep_hours
## t = -34.733, df = 998, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.7666105 -0.7103595
## sample estimates:
## cor
## -0.7397749
# Membuat scatter plot
plot(daily_game_hours, sleep_hours,
main = "Scatter Plot Daily Game Hours vs Sleep Hours",
xlab = "Daily Gaming Hours",
ylab = "Sleep Hours",
pch = 19,
col = "blue")
abline(lm(sleep_hours ~ daily_game_hours, data = data),
col = "red",
lwd = 2)

# Uji korelasi Spearman Data Tdk Normal
cor.test(daily_game_hours,
sleep_hours,
method = "spearman",
use = "complete.obs")
## Warning in cor.test.default(daily_game_hours, sleep_hours, method = "spearman",
## : Cannot compute exact p-value with ties
##
## Spearman's rank correlation rho
##
## data: daily_game_hours and sleep_hours
## S = 296281085, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.7776883
# -------------------------------------------
# DATA SIMULASI
# -------------------------------------------
set.seed(123)
plot(daily_game_hours, sleep_hours,
main = "Scatter Plot Daily Game Hours vs Sleep",
xlab = "Daily Game Hours",
ylab = "Sleep Hours",
pch = 19)
abline(lm(sleep_hours ~ daily_game_hours), lwd = 2)

library(ggplot2)
ggplot(data, aes(x = daily_game_hours, y = sleep_hours)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = TRUE) +
labs(title = "Hubungan Daily Game Hours dan Sleep Hours",
x = "Daily Game Hours",
y = "Sleep Hours") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# Menghitung matriks korelasi
matriks_korelasi <- cor(data, use = "complete.obs")
# Heatmap
heatmap(matriks_korelasi)

# Uji Kendall
cor.test(daily_game_hours, sleep_hours,
method = "kendall",
use = "complete.obs")
##
## Kendall's rank correlation tau
##
## data: daily_game_hours and sleep_hours
## z = -26.737, p-value < 2.2e-16
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## -0.5732781
#library(ppcor)
#pcor.test(daily_Game_Hours, sleep_Hours)