# ------------------------------------------- 
# ANALISIS KORELASI 
# Prodi Matematika - Analisis Regresi
# ------------------------------------------- 
cat("\n=== Analisis Korelasi Hubungan Marketing Spend dengan Profit ===\n")
## 
## === Analisis Korelasi Hubungan Marketing Spend dengan Profit ===
# 1. MEMUAT PACKAGE DAN DATA
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.5.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.5.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.5.2
library(csv)
## Warning: package 'csv' was built under R version 4.5.2
# 2. Data yang digunakan
df <- read.csv("C:/Users/LENOVO/Downloads/business_profit_dataset.csv")
data <- df[c("Marketing_Spend","Profit")]
head(data)
##   Marketing_Spend    Profit
## 1        54967.14 102741.55
## 2        48617.36  19892.62
## 3        56476.89  74687.43
## 4        65230.30  48623.61
## 5        47658.47  88350.11
## 6        47658.63  57552.83
tail(data)
##     Marketing_Spend   Profit
## 195        51731.81 78680.70
## 196        53853.17 43249.97
## 197        41161.43 75585.89
## 198        51537.25 92791.31
## 199        50582.09 66211.17
## 200        38570.30 50462.66
# 3. Statistik deskriptif sederhana 
cat("\n=== Statistik Deskriptif Sederhana dari Data ===\n")
## 
## === Statistik Deskriptif Sederhana dari Data ===
summary(data) 
##  Marketing_Spend     Profit      
##  Min.   :23803   Min.   :  7121  
##  1st Qu.:42949   1st Qu.: 48920  
##  Median :49958   Median : 74156  
##  Mean   :49592   Mean   : 73222  
##  3rd Qu.:55009   3rd Qu.: 94816  
##  Max.   :77202   Max.   :142789
# 4. Standar deviasi 
cat("\n=== Standar Deviasi Marketing Spend ===\n")
## 
## === Standar Deviasi Marketing Spend ===
sd(df$Marketing_Spend)
## [1] 9310.039
cat("\n=== Standar Deviasi Profit ===\n")
## 
## === Standar Deviasi Profit ===
sd(df$Profit)
## [1] 29875.16
# 5. Uji korelasi Pearson
hasil_korelasi <- cor.test(df$Marketing_Spend, df$Profit, method = "pearson")
 
# Menampilkan hasil 
print(hasil_korelasi) 
## 
##  Pearson's product-moment correlation
## 
## data:  df$Marketing_Spend and df$Profit
## t = 3.9071, df = 198, p-value = 0.0001282
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.1337671 0.3917426
## sample estimates:
##       cor 
## 0.2675428
# 6. Membuat scatter plot 
plot(df$Marketing_Spend, df$Profit, 
     main = "Scatter Plot Marketing Spend vs Profit", 
     xlab = "Marketing_Spend", 
     ylab = "Profit", 
     pch = 19, 
     col = "blue") 
 
# Menambahkan garis regresi 
abline(lm(df$Profit ~ df$Marketing_Spend), col = "red", lwd = 2)

# 7. Hubungan Marketing Spend dan Profit
ggplot(data, aes(x =Marketing_Spend, y = Profit)) + 
geom_point(size = 3) + 
geom_smooth(method = "lm", se = TRUE) + 
labs(title = "Hubungan Marketing Spend dan Profit", 
x = "Marketing_Spend", 
y = "Profit") + 
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'