——————————————-

CONTOH ANALISIS KORELASI

Prodi Matematika - Statistik Dasar

——————————————-

# Import data CSV
data <- read.csv(file.choose("C:/Users/Asus/Downloads/archive"))
Y <- data[c("Shares","LastPrice")]
head(Y)
##        Shares LastPrice
## 1  1924688333      8000
## 2  3935892857       142
## 3   620806680      6700
## 4  2753165000      3050
## 5 17150000000       490
## 6 12675160000       156
# Statistik deskriptif sederhana
summary(Y)
##      Shares            LastPrice      
##  Min.   :3.600e+06   Min.   :   25.0  
##  1st Qu.:1.231e+09   1st Qu.:  101.8  
##  Median :3.128e+09   Median :  287.0  
##  Mean   :1.094e+10   Mean   : 1363.5  
##  3rd Qu.:9.327e+09   3rd Qu.:  975.0  
##  Max.   :1.180e+12   Max.   :38000.0  
##                      NA's   :5
# Standar deviasi
sd(Y$Shares)
## [1] 45257987781
sd(Y$LastPrice)
## [1] NA
# Uji korelasi Pearson
hasil_korelasi <- cor.test(Y$Shares, 
                           Y$LastPrice, 
                           method = "pearson")

# Menampilkan hasil
print(hasil_korelasi)
## 
##  Pearson's product-moment correlation
## 
## data:  Y$Shares and Y$LastPrice
## t = -0.82001, df = 822, p-value = 0.4124
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.09669745  0.03978488
## sample estimates:
##         cor 
## -0.02858953
# Membuat scatter plot
plot(Y$Shares, Y$LastPrice,
     main = "Scatter Plot Shares vs Last Price",
     xlab = "Shares",
     ylab = "Last Price",
     pch = 19,
     col = "blue")

# Menambahkan garis regresi
abline(lm(LastPrice ~ Shares, data = Y),
       col = "red",
       lwd = 2)

# Uji korelasi Spearman Data Tdk Normal
hasil_spearman <- cor.test(Y$Shares,
                           Y$LastPrice,
                           method = "spearman",
                           exact = FALSE)

print(hasil_spearman)
## 
##  Spearman's rank correlation rho
## 
## data:  Y$Shares and Y$LastPrice
## S = 104483847, p-value = 0.0005264
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##        rho 
## -0.1205195
# -------------------------------------------
# DATA SAHAM
# -------------------------------------------

set.seed(123)

Shares      <- Y$Shares
LastPrice   <- Y$LastPrice

data_saham  <- data.frame(Shares, LastPrice)

plot(Shares, LastPrice,
     main = "Scatter Plot Shares vs Last Price",
     xlab = "Shares",
     ylab = "Last Price",
     pch = 19)

abline(lm(LastPrice ~ Shares), lwd = 2)

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
ggplot(data_saham, aes(x = Shares, y = LastPrice)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", formula = y ~ x, se = TRUE)
## Warning: Removed 5 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

  labs(title = "Hubungan Shares dan Last Price",
       x = "Shares",
       y = "Last Price") +
  theme_minimal()
## NULL
# Menghitung matriks korelasi
matriks_korelasi <- cor(data_saham, use = "complete.obs")

# Heatmap
heatmap(matriks_korelasi)

# Uji korelasi Kendall
cor.test(Shares, LastPrice, method = "kendall")
## 
##  Kendall's rank correlation tau
## 
## data:  Shares and LastPrice
## z = -3.4799, p-value = 0.0005016
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
##         tau 
## -0.08147288