# ===============================
# 1. INPUT DATA BISNIS
# ===============================

data_bisnis <- data.frame(
  Sales = c(520, 610, 580, 450, 700, 640, 500, 720, 680, 560, 590, 630),
  Promotion = c(50, 65, 60, 40, 80, 70, 45, 85, 75, 55, 58, 68),
  Staff = c(20, 25, 23, 18, 30, 27, 19, 32, 29, 22, 24, 26),
  StoreSize = c(200, 250, 230, 180, 300, 270, 190, 320, 290, 210, 240, 260),
  CustomerSat = c(78, 85, 82, 70, 90, 88, 75, 92, 89, 80, 83, 87),
  Loyalty = c(72, 80, 78, 65, 88, 84, 70, 90, 86, 75, 79, 83)
)

data_bisnis
##    Sales Promotion Staff StoreSize CustomerSat Loyalty
## 1    520        50    20       200          78      72
## 2    610        65    25       250          85      80
## 3    580        60    23       230          82      78
## 4    450        40    18       180          70      65
## 5    700        80    30       300          90      88
## 6    640        70    27       270          88      84
## 7    500        45    19       190          75      70
## 8    720        85    32       320          92      90
## 9    680        75    29       290          89      86
## 10   560        55    22       210          80      75
## 11   590        58    24       240          83      79
## 12   630        68    26       260          87      83
# Korelasi Bivariat #

cor(data_bisnis$Promotion, data_bisnis$Sales)
## [1] 0.994136
cor.test(data_bisnis$Promotion, data_bisnis$Sales)
## 
##  Pearson's product-moment correlation
## 
## data:  data_bisnis$Promotion and data_bisnis$Sales
## t = 29.072, df = 10, p-value = 5.407e-11
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.9785095 0.9984090
## sample estimates:
##      cor 
## 0.994136
# ======Interpretasi========= 
  
# -) Jika r mendekati 1 → promosi dan penjualan bergerak searah
# -) Lihat p-value untuk signifikansi

# Korelasi Berganda #
model <- lm(Sales ~ Promotion + Staff + StoreSize, data = data_bisnis)
summary(model)
## 
## Call:
## lm(formula = Sales ~ Promotion + Staff + StoreSize, data = data_bisnis)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -17.1250  -0.6046   0.0464   3.0067  14.7174 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 201.2062    31.5305   6.381 0.000213 ***
## Promotion     4.1824     1.9244   2.173 0.061495 .  
## Staff        13.7017    12.4941   1.097 0.304703    
## StoreSize    -0.8223     1.0305  -0.798 0.447930    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.659 on 8 degrees of freedom
## Multiple R-squared:   0.99,  Adjusted R-squared:  0.9862 
## F-statistic:   263 on 3 and 8 DF,  p-value: 2.487e-08
# Ambil R^2
R2 <- summary(model)$r.squared
R <- sqrt(R2)
R2
## [1] 0.9899639
R
## [1] 0.9949693
# ======Interpretasi========= 
  
# R² menunjukkan berapa persen variasi Sales dijelaskan oleh ketiga variabel


# Korelasi Parsial #

# Case: Apakah Promotion tetap berpengaruh terhadap Sales setelah dikontrol Staff dan StoreSize?

library(ppcor)
## Loading required package: MASS
pcor.test(
  data_bisnis$Promotion,
  data_bisnis$Sales,
  data_bisnis[, c("Staff", "StoreSize")]
)
##    estimate    p.value statistic  n gp  Method
## 1 0.6092977 0.06149483  2.173369 12  2 pearson
# ======Interpretasi========= 

# R² menunjukkan berapa persen variasi Sales dijelaskan oleh ketiga variabel

# Korelasi Kanonik #

# Kelompok 1 (Operasional):Promotion,Staff,StoreSize
# Kelompok 2 (Outcome): Sales,CustomerSat,Loyalty

# Standarisasi dulu
X <- scale(data_bisnis[, c("Promotion", "Staff", "StoreSize")])
Y <- scale(data_bisnis[, c("Sales", "CustomerSat", "Loyalty")])

cc <- cancor(X, Y)

# Nilai canonical correlation
cc$cor
## [1] 0.9959211 0.5134494 0.3985442
# Koefisien pembentuk kombinasi
cc$xcoef
##                 [,1]       [,2]      [,3]
## Promotion -0.1570032  2.0713305 -1.819253
## Staff     -0.3103713  0.7337338  5.688635
## StoreSize  0.1659745 -2.8189990 -3.885821
cc$ycoef
##                     [,1]      [,2]       [,3]
## Sales       -0.400158282  1.998564  3.5166710
## CustomerSat  0.094027878  3.175984 -0.3855181
## Loyalty      0.006076517 -5.147160 -3.1583887
# ======Interpretasi========= 

# -)Canonical correlation pertama menunjukkan seberapa kuat hubungan sistem operasional
# dengan sistem outcome bisnis.

# -)Jika misalnya hasilnya 0.85 → hubungan antar sistem sangat kuat.