Salah satu uji statistik yang digunakan untuk melihat atau mengukur kekuatan hubungan antara kedua variabel disebut korelasi. Terdapat berbagai macam jenis uji korelasi. Salah satu uji korelasi yang sering digunakan ialah korelasi Pearson Product Mement yang mensyaratkan data paling tidak berupa ukuran skala interval dan berasal dari sampel yang memilki sebaran normal (Ghozali, 2006). Untuk lebih lengkapnya bisa membaca materi di website kami www.databee.id
Untuk menyatakan ada atau tidaknya hubungan antara variabel X dengan variabel Y. Terdapat asumsi yang harus dipenuhi yakni data berdistribusi normal, dan data berskala interval atau rasio.
mydata <- mtcars
head(mydata, 6)## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
cor(mydata$mpg,mydata$wt, method = "pearson")## [1] -0.8676594
cor.test(mydata$mpg,mydata$wt, method = "pearson")##
## Pearson's product-moment correlation
##
## data: mydata$mpg and mydata$wt
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.9338264 -0.7440872
## sample estimates:
## cor
## -0.8676594
# multiple scatterplots
pairs(mydata[, c(1, 4, 6)])
#visualisasi korelasi
library("ggpubr")## Loading required package: ggplot2
ggscatter(mydata, x = "mpg", y = "wt",
add = "reg.line", conf.int = TRUE,
cor.coef = TRUE, cor.method = "pearson",
xlab = "Miles/(US) gallon", ylab = "Weight (1000 lbs)")## `geom_smooth()` using formula 'y ~ x'
Jika data berskala interval dan tidak berdistribusi normal dapat digunakan Korelasi Rank Spearman. Koefisien korelasi Spearman-rho digunakan untuk pengukuran korelasi pada data skala ordinal dan interval.
data("cars")
head(cars, 6)## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
#visualisasi korelasi
library(ggplot2)
ggplot(cars, aes(x=speed, y=dist)) +
geom_point(color='#2980B9', size = 4) +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE, color='#2C3E50')## `geom_smooth()` using formula 'y ~ x'
cor.test(x=cars$speed, y=cars$dist, method = 'spearman')## Warning in cor.test.default(x = cars$speed, y = cars$dist, method = "spearman"):
## Cannot compute exact p-value with ties
##
## Spearman's rank correlation rho
##
## data: cars$speed and cars$dist
## S = 3532.8, p-value = 8.825e-14
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.8303568
# Menghitung Rank
cars.ranked <- data.frame(cbind(rank(cars$speed, ties.method = 'average'),
rank(cars$dist, ties.method = 'average')))
colnames(cars.ranked) <- c('speed', 'dist')
rho <- cov(cars.ranked) / (sd(cars.ranked$speed) * sd(cars.ranked$dist))
rho[[2]]## [1] 0.8303568
Cramer Coefficient C adalah ukuran tingkat hubungan antara dua kelompok variabel. Uji ini berguna jika informasi atau data yang kita miliki berskala nominal atau kategorikal (Ghozali, 2006).
library(MASS)
head(Cars93,6)## Manufacturer Model Type Min.Price Price Max.Price MPG.city MPG.highway
## 1 Acura Integra Small 12.9 15.9 18.8 25 31
## 2 Acura Legend Midsize 29.2 33.9 38.7 18 25
## 3 Audi 90 Compact 25.9 29.1 32.3 20 26
## 4 Audi 100 Midsize 30.8 37.7 44.6 19 26
## 5 BMW 535i Midsize 23.7 30.0 36.2 22 30
## 6 Buick Century Midsize 14.2 15.7 17.3 22 31
## AirBags DriveTrain Cylinders EngineSize Horsepower RPM
## 1 None Front 4 1.8 140 6300
## 2 Driver & Passenger Front 6 3.2 200 5500
## 3 Driver only Front 6 2.8 172 5500
## 4 Driver & Passenger Front 6 2.8 172 5500
## 5 Driver only Rear 4 3.5 208 5700
## 6 Driver only Front 4 2.2 110 5200
## Rev.per.mile Man.trans.avail Fuel.tank.capacity Passengers Length Wheelbase
## 1 2890 Yes 13.2 5 177 102
## 2 2335 Yes 18.0 5 195 115
## 3 2280 Yes 16.9 5 180 102
## 4 2535 Yes 21.1 6 193 106
## 5 2545 Yes 21.1 4 186 109
## 6 2565 No 16.4 6 189 105
## Width Turn.circle Rear.seat.room Luggage.room Weight Origin Make
## 1 68 37 26.5 11 2705 non-USA Acura Integra
## 2 71 38 30.0 15 3560 non-USA Acura Legend
## 3 67 37 28.0 14 3375 non-USA Audi 90
## 4 70 37 31.0 17 3405 non-USA Audi 100
## 5 69 39 27.0 13 3640 non-USA BMW 535i
## 6 69 41 28.0 16 2880 USA Buick Century
library(rcompanion)
cramerV(Cars93$Type, Cars93$Origin, bias.correct = FALSE)## Cramer V
## 0.3891
Menghitung koefisien korelasi apabila data berskala nominal selain menggunakan koefisien Cramer, koefisien Phi juga bisa digunakan. Koefisien korelasi Phi menggunakan tabel kontingensi 2 × 2. Karena data bersifat dikotomous maka diasumsikan 0 dan 1 untuk masing-masing variabel (Purnomo, 2014).
library(psych)##
## Attaching package: 'psych'
## The following object is masked from 'package:rcompanion':
##
## phi
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
datap <- matrix(c(40,5,20,20),ncol=2)
datap## [,1] [,2]
## [1,] 40 20
## [2,] 5 20
phi(datap)## [1] 0.43
Koefisien korelasi yang biasa digunakan untuk mengukur kekuatan korelasi untuk data penelitian dengan skala pengukuran ordinal adalah koefisien korelasi yang dikenalkan oleh M.G. Kendall (1938) yaitu koefisien korelasi Kendall-tau.
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
cor.ken<-cor.test(x,y, method="kendall")
cor.ken##
## Kendall's rank correlation tau
##
## data: x and y
## T = 26, p-value = 0.1194
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## 0.4444444