Dataset Coffee Chain merupakan dataset relasional yang terdiri dari beberapa tabel utama, yaitu factTable, location, dan product. Setiap tabel memiliki peran yang berbeda, di mana factTable menyimpan data transaksi, sedangkan tabel lainnya berfungsi sebagai dimensi pendukung.
Dalam analisis ini digunakan pendekatan integratif dengan menggabungkan ketiga tabel tersebut untuk memperoleh pemahaman yang lebih komprehensif terhadap faktor-faktor yang memengaruhi Sales dan Profit.
Analisis tidak hanya dilakukan secara deskriptif, tetapi juga eksploratif dengan memanfaatkan visualisasi untuk mengidentifikasi pola, hubungan antar variabel, serta potensi insight yang dapat digunakan dalam pengambilan keputusan.
Tujuan analisis:
Mengintegrasikan data dari beberapa tabel menjadi satu dataset analitik
Menganalisis hubungan antara Sales dan Profit
Mengidentifikasi pola penjualan berdasarkan Market dan Product Type
Mengevaluasi distribusi dan variasi data
Menghasilkan insight yang relevan untuk strategi bisnis
# Memuat paket yang diperlukan untuk koneksi database, visualisasi, dan analisis data
library(dplyr)
library(ggplot2)
library(nortest)
library(DBI)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "MySQL ODBC 8.0 ANSI Driver",
Server = "127.0.0.1",
Database = "coffee",
UID = "root",
PWD = "admin123",
Port = 3306)
# Cek Database Berhasil atau Tidak
dbListTables(con)
## [1] "facttable" "location" "product"
# Import Tabel
location <- dbReadTable(con, "location")
product <- dbReadTable(con, "product")
fact <- dbReadTable(con, "factTable")
# Cek Data
colnames(location)
## [1] "Area.Code" "State" "Market" "Market.Size"
colnames(product)
## [1] "Product.Line" "Product.Type" "Product" "ProductId" "Type"
colnames(fact)
## [1] "Profit" "Margin" "Sales" "COGS"
## [5] "Total.Expenses" "Marketing" "Inventory" "Budget.Profit"
## [9] "Budget.COGS" "Budget.Margin" "Budget.Sales" "Area.Code"
## [13] "ProductId" "Date"
str(location)
## 'data.frame': 156 obs. of 4 variables:
## $ Area.Code : int 203 206 209 210 212 213 214 216 217 224 ...
## $ State : chr "Connecticut" "Washington" "California" "Texas" ...
## $ Market : chr "East" "West" "West" "South" ...
## $ Market.Size: chr "Small Market" "Small Market" "Major Market" "Major Market" ...
str(product)
## 'data.frame': 13 obs. of 5 variables:
## $ Product.Line: chr "Beans" "Beans" "Beans" "Beans" ...
## $ Product.Type: chr "Coffee" "Coffee" "Coffee" "Espresso" ...
## $ Product : chr "Amaretto" "Colombian" "Decaf Irish Cream" "Caffe Latte" ...
## $ ProductId : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Type : chr "Regular" "Regular" "Decaf" "Regular" ...
str(fact)
## 'data.frame': 4248 obs. of 14 variables:
## $ Profit : int 94 68 101 30 54 53 99 0 33 17 ...
## $ Margin : int 130 107 139 56 80 108 171 87 80 72 ...
## $ Sales : int 219 190 234 100 134 180 341 150 140 130 ...
## $ COGS : int 89 83 95 44 54 72 170 63 60 58 ...
## $ Total.Expenses: int 36 39 38 26 26 55 72 87 47 55 ...
## $ Marketing : int 24 27 26 14 15 23 47 57 19 22 ...
## $ Inventory : int 777 623 821 623 456 558 1091 435 336 338 ...
## $ Budget.Profit : int 100 80 110 30 70 80 110 20 40 20 ...
## $ Budget.COGS : int 90 80 100 30 60 80 140 50 50 40 ...
## $ Budget.Margin : int 130 110 140 50 90 130 160 80 70 70 ...
## $ Budget.Sales : int 220 190 240 80 150 210 300 130 120 110 ...
## $ Area.Code : int 719 970 970 303 303 720 970 719 970 719 ...
## $ ProductId : int 1 2 3 13 5 6 8 9 10 11 ...
## $ Date : chr "2012-01-01 00:00:00" "2012-01-01 00:00:00" "2012-01-01 00:00:00" "2012-01-01 00:00:00" ...
head(location)
## Area.Code State Market Market.Size
## 1 203 Connecticut East Small Market
## 2 206 Washington West Small Market
## 3 209 California West Major Market
## 4 210 Texas South Major Market
## 5 212 New York East Major Market
## 6 213 California West Major Market
head(product)
## Product.Line Product.Type Product ProductId Type
## 1 Beans Coffee Amaretto 1 Regular
## 2 Beans Coffee Colombian 2 Regular
## 3 Beans Coffee Decaf Irish Cream 3 Decaf
## 4 Beans Espresso Caffe Latte 4 Regular
## 5 Beans Espresso Caffe Mocha 5 Regular
## 6 Beans Espresso Decaf Espresso 6 Decaf
head(fact)
## Profit Margin Sales COGS Total.Expenses Marketing Inventory Budget.Profit
## 1 94 130 219 89 36 24 777 100
## 2 68 107 190 83 39 27 623 80
## 3 101 139 234 95 38 26 821 110
## 4 30 56 100 44 26 14 623 30
## 5 54 80 134 54 26 15 456 70
## 6 53 108 180 72 55 23 558 80
## Budget.COGS Budget.Margin Budget.Sales Area.Code ProductId
## 1 90 130 220 719 1
## 2 80 110 190 970 2
## 3 100 140 240 970 3
## 4 30 50 80 303 13
## 5 60 90 150 303 5
## 6 80 130 210 720 6
## Date
## 1 2012-01-01 00:00:00
## 2 2012-01-01 00:00:00
## 3 2012-01-01 00:00:00
## 4 2012-01-01 00:00:00
## 5 2012-01-01 00:00:00
## 6 2012-01-01 00:00:00
# Ambil Insight
data_full <- fact %>%
left_join(location, by = "Area.Code") %>%
left_join(product, by = "ProductId")
head(data_full)
## Profit Margin Sales COGS Total.Expenses Marketing Inventory Budget.Profit
## 1 94 130 219 89 36 24 777 100
## 2 68 107 190 83 39 27 623 80
## 3 101 139 234 95 38 26 821 110
## 4 30 56 100 44 26 14 623 30
## 5 54 80 134 54 26 15 456 70
## 6 53 108 180 72 55 23 558 80
## Budget.COGS Budget.Margin Budget.Sales Area.Code ProductId
## 1 90 130 220 719 1
## 2 80 110 190 970 2
## 3 100 140 240 970 3
## 4 30 50 80 303 13
## 5 60 90 150 303 5
## 6 80 130 210 720 6
## Date State Market Market.Size Product.Line Product.Type
## 1 2012-01-01 00:00:00 Colorado Central Major Market Beans Coffee
## 2 2012-01-01 00:00:00 Colorado Central Major Market Beans Coffee
## 3 2012-01-01 00:00:00 Colorado Central Major Market Beans Coffee
## 4 2012-01-01 00:00:00 Colorado Central Major Market Leaves Tea
## 5 2012-01-01 00:00:00 Colorado Central Major Market Beans Espresso
## 6 2012-01-01 00:00:00 Colorado Central Major Market Beans Espresso
## Product Type
## 1 Amaretto Regular
## 2 Colombian Regular
## 3 Decaf Irish Cream Decaf
## 4 Green Tea Regular
## 5 Caffe Mocha Regular
## 6 Decaf Espresso Decaf
# Statistika Deskriptif
data_insight <- data_full %>%
select("Sales", "Profit", "Market", "Market.Size", "Product.Type")
summary(data_insight)
## Sales Profit Market Market.Size
## Min. : 17 Min. :-638.0 Length:4248 Length:4248
## 1st Qu.:100 1st Qu.: 17.0 Class :character Class :character
## Median :138 Median : 40.0 Mode :character Mode :character
## Mean :193 Mean : 61.1
## 3rd Qu.:230 3rd Qu.: 92.0
## Max. :912 Max. : 778.0
## Product.Type
## Length:4248
## Class :character
## Mode :character
##
##
##
Statistika deskriptif digunakan untuk melihat gambaran umum data seperti nilai minimum, maksimum, mean, dan quartile.
# Visualisasi Hubungan Sales dan Profit
ggplot(data_full, aes(x = Sales, y = Profit, color = Product.Type)) +
geom_point(size = 2.5, alpha = 0.7) +
geom_smooth(method = "lm", se = FALSE, color = "#08306b") +
scale_color_manual(values = c("#08519c", "#3182bd", "#6baed6", "#bdd7e7")) +
labs(
title = "Hubungan Sales dan Profit",
subtitle = "Ditinjau berdasarkan Product Type",
x = "Sales",
y = "Profit"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "bottom"
)
## `geom_smooth()` using formula = 'y ~ x'
Terdapat hubungan positif antara Sales dan Profit, namun tidak sepenuhnya linear. Hal ini menunjukkan bahwa peningkatan penjualan tidak selalu diikuti peningkatan profit secara proporsional.
# Distribusi Sales per Market
ggplot(data_full, aes(x = Market, y = Sales, fill = Market)) +
geom_boxplot(alpha = 0.85) +
scale_fill_manual(values = c("#0b5394", "#3d85c6", "#6fa8dc", "#9fc5e8")) +
labs(
title = "Distribusi Sales Berdasarkan Market",
subtitle = "Perbandingan antar wilayah",
x = "Market",
y = "Sales"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "none"
)
Distribusi menunjukkan adanya perbedaan performa antar market. Beberapa market memiliki median lebih tinggi dan variasi yang lebih stabil.
# Profit Berdasarkan Market dan Product Type
data_full %>%
group_by(Market, Product.Type) %>%
summarise(avg_profit = mean(Profit)) %>%
ggplot(aes(x = Product.Type, y = Market, fill = avg_profit)) +
geom_tile(color = "white") +
scale_fill_gradient(low = "#deebf7", high = "#08519c") +
labs(
title = "Heatmap Profit Berdasarkan Market dan Product Type",
subtitle = "Identifikasi kombinasi paling menguntungkan",
x = "Product Type",
y = "Market",
fill = "Profit"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5)
)
## `summarise()` has grouped output by 'Market'. You can override using the
## `.groups` argument.
Heatmap menunjukkan kombinasi market dan produk yang memberikan profit tertinggi, sehingga dapat menjadi dasar dalam pengambilan keputusan bisnis.
data_full %>%
group_by(Market) %>%
summarise(
avg_sales = mean(Sales),
avg_profit = mean(Profit)
)
## # A tibble: 4 × 3
## Market avg_sales avg_profit
## <chr> <dbl> <dbl>
## 1 Central 197. 69.8
## 2 East 201. 66.7
## 3 South 155. 48.3
## 4 West 203. 55.1
Hasil menunjukkan adanya variasi rata-rata Sales dan Profit antar market, yang mengindikasikan perbedaan karakteristik pasar.
#Uji Liliefors pada Sales
lillie.test(data_insight$Sales)
##
## Lilliefors (Kolmogorov-Smirnov) normality test
##
## data: data_insight$Sales
## D = 0.17561, p-value < 2.2e-16
H0: Data Sales berdistribusi normal
H1: Data Sales tidak berdistribusi normal
p-value > 0.05 (Data berdistribusi normal)
p-value < 0.05 (Data tidak berdistribusi normal)
Hasil uji Lilliefors menunjukkan nilai p-value dari variabel Sales. Didapatkan p-value lebih kecil dari 0.05 maka data Sales tidak berdistribusi normal.
#Uji Liliefors pada Profit
lillie.test(data_insight$Profit)
##
## Lilliefors (Kolmogorov-Smirnov) normality test
##
## data: data_insight$Profit
## D = 0.18579, p-value < 2.2e-16
Uji ini digunakan untuk mengetahui apakah variabel Profit berdistribusi normal atau tidak. Didaptkan p-value lebih kecil dari 0.05 maka data Profit tidak berdistribusi normal.
cor(data_full$Sales, data_full$Profit)
## [1] 0.7973309
Hasil korelasi menunjukkan hubungan antara Sales dan Profit, namun tidak sepenuhnya kuat, sehingga terdapat faktor lain yang memengaruhi profit.
Sales dan Profit memiliki hubungan positif, namun tidak linear
Performa bisnis dipengaruhi oleh Market dan Product Type
Terdapat variasi signifikan antar market
Tidak semua produk menghasilkan profit optimal
Data tidak berdistribusi normal
Analisis ini menunjukkan bahwa penggabungan data dari berbagai tabel memberikan insight yang lebih mendalam dibandingkan analisis sederhana. Visualisasi yang tepat juga membantu dalam memahami pola data secara lebih jelas.
Hasil analisis dapat digunakan sebagai dasar dalam pengambilan keputusan strategis, khususnya dalam pengelolaan produk dan segmentasi pasar.
# Tutup koneksi
dbDisconnect(con)