Korelasi: Abdillah Khoirul Amar 2024-03-08

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

KORELASI

Abdillah Khoirul Amar

2024-03-08

library(readxl)
rstat24y <- read_excel("rstat24y.xlsx", col_types = c("numeric",
"text", "numeric", "numeric", "numeric",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"numeric", "numeric", "numeric", "numeric",
"numeric", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text", "text",
"text", "text", "text", "text"))

MEMILIH DAN MENGELUARKAN VARIABEL


library(dplyr) #aktivasi paket

##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#mengeluarkan variabel berat badan (weight)
bb <- rstat24y$weight
#mengeluarkan variabel tinggi badan (height)
tb <- rstat24y$height
#membuat dataframe BMI berbentuk tibble
bmi <- tibble(tb,bb)
head(bmi)

## # A tibble: 6 x 2
## tb bb
## <dbl> <dbl>
## 1 166 51
## 2 157 41

1
## 3 172 55
## 4 150 60
## 5 150 42
## 6 154 42
MEMBUAT DATA FRAME
#ordinal bb
bb <- data.frame(bb)
library(dplyr) #aktivasi paket dplyr ()
#membuat kolom weight berbentuk ordinal
bb <- bb %>%
mutate(bmi)
library(psych)
describe(bmi$bb)

## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 60 54.9 11.64 52.5 53.5 11.12 39 95 56 1.19 1.54 1.5
library(dplyr) #aktivasi paket dplyr ()
#membuat kolom weight berbentuk ordinal
bb <- bb %>%
mutate(bmi.ord = case_when(
data.frame(bb) < 50 ~ "Kurus",
between(bb, 50, 65) ~ "Normal",
tb > 65 ~ "Gemuk")
)

VISUALISASI DALAM BENTUK HISTOGRAM


library(ggplot2)

##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
g1 <- ggplot(bb, aes(bmi.ord))
g1 + geom_bar(fill = "pink",
color = "darkred",
alpha = 0.5) +
labs(title = "Grafik 1 Histogram Berat Badan Ordinal",
x = "Kategori Berat Badan",
y = "Frekuensi",
caption = "Sumber: analisis data primer")

2
Grafik 1 Histogram Berat Badan Ordinal

30

20
Frekuensi

10

Gemuk Kurus Normal


Kategori Berat Badan
Sumber: analisis data primer

library(dplyr)
#Data tinggi badan
tb <- data.frame(tb)
#membuat kolom weight berbentuk ordinal
tb <- tb %>%
mutate(bmi)
library(psych)
describe(bmi$tb)

## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 60 159.72 7.75 158 159.17 5.93 143 180 37 0.53 -0.24 1
library(dplyr) #aktivasi paket dplyr ()
#membuat kolom weight berbentuk ordinal
tb <- tb %>%
mutate(bmi.ord = case_when(
data.frame(tb) < 155 ~ "Pendek",
between(tb, 155, 175) ~ "Normal",
tb > 175 ~ "Tinggi")
)
library(ggplot2)
g3 <- ggplot(tb, aes(bmi.ord))
g3 + geom_bar(fill = "darkgrey",
color = "green",
alpha = 0.5) +
labs(title = "Grafik 2 Histogram Tinggi Tubuh Ordinal",

3
x = "Kategori Tinggi Tubuh",
y = "Frekuensi",
caption = "Sumber: analisis data primer")

Grafik 2 Histogram Tinggi Tubuh Ordinal

40

30
Frekuensi

20

10

Normal Pendek Tinggi


Kategori Tinggi Tubuh
Sumber: analisis data primer

#mengubah data dari karakter ke factor bb


bmi.ord1 <- bb$bmi.ord
bb1 <- as.factor(bmi.ord1)
#memeriksa level
levels(bb1)

## [1] "Gemuk" "Kurus" "Normal"


#mengubah variabel data dari karakter ke factor tb
bmi.ord1 <- tb$bmi.ord
tb1 <- as.factor (bmi.ord1)
#memeriksa level
levels(tb1)

## [1] "Normal" "Pendek" "Tinggi"


bb2 <- as.numeric(bb1)
#mengubah objek dari numeric ke dataframe
hist(bb2,
main = "Grafik 3 Histogram bb",
xlab = "berat badan",
ylab = "Frekuensi",
col = "yellow",
border = "darkred")
#menambahkan grid di histogram

4
grid()

30
25
20 Grafik 3 Histogram bb
Frekuensi

15
10
5
0

1.0 1.5 2.0 2.5 3.0

berat badan
tb2 <- as.numeric(tb1)
#mengubah objek dari numeric ke dataframe
hist(tb2,
main = "Grafik 4 Histogram tb",
xlab = "berat badan",
ylab = "Frekuensi",
col = "lightblue",
border = "brown")
#menambahkan grid di histogram
grid()

5
Grafik 4 Histogram tb
40
30
Frekuensi

20
10
0

1.0 1.5 2.0 2.5 3.0

berat badan
MENGHITUNG KORELASI BERAT BADAN DAN TINGGI BADAN
library(dplyr)
#mengeluarkan objek bb dan tb
kor1 <- select(bmi, tb, bb)
#menghitung korelasi dan menyimpannya dalam kor2
kor2 <- cor(kor1)
#mencetak hasil perhitungan korelasi
print(kor2)

## tb bb
## tb 1.0000000 0.3884597
## bb 0.3884597 1.0000000
bb2 <- as.numeric(bb1)
tb2 <- as.numeric(tb1)
# Menghitung korelasi Kendall
kendall_result <- cor.test(bb2, tb2, method = "kendall")
#mencetak
print(kendall_result)

##
## Kendall's rank correlation tau
##
## data: bb2 and tb2
## z = 0.46774, p-value = 0.64
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## 0.05738525
VISUALISASI KORELASI BERAT BADAN DAN TINGGI BADAN

6
#aktivasi paket
library(ggplot2)
#membuat scatter plot
ggplot(bmi, aes(x = bb, y = tb)) +
geom_point() +
labs(x = "Tinggi badan",
y = "Berat Badan",
title = "Scatter plot Tinggi Badan dan Berat Badan ") +
theme_minimal()

Scatter plot Tinggi Badan dan Berat Badan


180

170
Berat Badan

160

150

40 60 80
Tinggi badan
bb2 <- as.numeric(bb1)
tb2 <- as.numeric(tb1)
# Menghitung korelasi Kendall
kendall_result <- cor.test(bb2, tb2, method = "kendall")
#mencetak
print(kendall_result)

##
## Kendall's rank correlation tau
##
## data: bb2 and tb2
## z = 0.46774, p-value = 0.64
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## 0.05738525

7
#visualisasi cara ke dua
plot(bb2, tb2)
abline(lm(tb2 ~ bb2, data = bmi), col = "brown")
3.0
2.5
tb2

2.0
1.5
1.0

1.0 1.5 2.0 2.5 3.0

bb2 VISU-
ALISASI DENGAN PAKET CAR
#instalasi dan aktivasi paket
library(car)

## Loading required package: carData


##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
## The following object is masked from 'package:dplyr':
##
## recode
#membuat scatter plot
scatterplot(tb ~ bb | tb, data = bmi,
ellipse = FALSE,
grid = FALSE,
frame = FALSE)

8
180
170
tb

160
150

40 50 60 70 80 90

bb

R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and
MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the
output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)

## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00

Including Plots
You can also embed plots, for example:

9
800
600
pressure

400
200
0

0 50 100 150 200 250 300 350

temperature
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that
generated the plot.

10

You might also like