prepocessing

suppressPackageStartupMessages(library(data.table))
suppressPackageStartupMessages(library(knitr))
suppressPackageStartupMessages(library(psych))
suppressPackageStartupMessages(library(Hmisc))
suppressPackageStartupMessages(library(reshape2))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(PerformanceAnalytics))
suppressPackageStartupMessages(library(gridExtra))
suppressPackageStartupMessages(library(car))

opar <- par(no.readonly = TRUE)
library(data.table)

# Define the path to your data (please remark the forward slash)
getwd()
## [1] "D:/kuliah/Semester 3/EVD/project/project/project"
filepath <- "D:/kuliah/Semester 3/EVD/project/survey analisis pembeli.csv"
df <- read.csv(filepath, sep = ";")
View(df)
# Convert characters to UTF-8 encoding
## Depending on your local R settings
## you could try to ignore and skip the next 4 lines
colnames(df) <- iconv(colnames(df), "ASCII", "UTF-8")
col_names <- sapply(df, is.character)
df[, col_names] <-
sapply(df[, col_names], function(col)
iconv(col, "ASCII", "UTF-8"))
# Column names of selected continuous variables
# Assuming df is your data frame
df_num <- df[, c("usia", "paham_isu", "medsos_panpol", "kepuasan_brand", "kesadaran_selektif", "opini_panbrand", "kepercayaan_setelah", "fre_beli_seblum", "fre_beli_sesudah"), drop = FALSE]
df_factor <- df[, c("prodi", "jenis_kelamin","kategori_brand", "dukung_atau_tidak","platform_marketing","beli_atau_ganti","alasan_beli","alasan_ganti"), drop=FALSE]
str(df)
## 'data.frame':    110 obs. of  17 variables:
##  $ prodi              : chr  "Teknologi Sains Data" "Teknologi Sains Data" "Teknologi Sains Data" "Teknologi Sains Data" ...
##  $ jenis_kelamin      : chr  "Perempuan" "Laki-laki" "Laki-laki" "Perempuan" ...
##  $ usia               : int  19 19 20 20 19 18 19 20 21 19 ...
##  $ paham_isu          : int  10 8 7 7 7 10 6 7 7 4 ...
##  $ medsos_panpol      : int  10 9 8 10 7 5 8 9 8 6 ...
##  $ kategori_brand     : chr  "FnB (Food and Beverage), Skincare and Cosmetics, Sport" "FnB (Food and Beverage), Sport" "FnB (Food and Beverage)" "FnB (Food and Beverage)" ...
##  $ kepuasan_brand     : int  10 10 9 10 8 10 9 9 8 8 ...
##  $ kesadaran_selektif : int  10 2 6 9 8 10 9 9 7 5 ...
##  $ opini_panbrand     : int  10 2 5 10 7 2 8 8 7 7 ...
##  $ kepercayaan_setelah: int  3 5 8 6 3 10 9 9 3 7 ...
##  $ fre_beli_seblum    : int  10 8 7 6 6 8 8 9 9 8 ...
##  $ fre_beli_sesudah   : int  1 8 7 4 2 8 8 2 3 2 ...
##  $ dukung_atau_tidak  : chr  "Sangat mendukung" "Tidak mendukung" "Tidak mendukung" "Sangat mendukung" ...
##  $ platform_marketing : chr  "Media sosial" "Media sosial" "Media sosial" "Media sosial" ...
##  $ beli_atau_ganti    : chr  "Mencari Alternatif Brand Lain" "Tetap Membeli" "Tetap Membeli" "Tetap Membeli" ...
##  $ alasan_beli        : chr  "" "Berpendapat bahwa tidak ada hubungannya membeli brand pro israel dengan isu Israel-Palestina" "Sudah cocok terhadap produk, Kualitas bagus, Reputasi Brand, Berpendapat bahwa tidak ada hubungannya membeli br"| __truncated__ "Sudah cocok terhadap produk, Mudah ditemukan di lingkungan sekitar" ...
##  $ alasan_ganti       : chr  "Rasa kemanusiaan terhadap Palestina, Rekomendasi orang lain" "-" "" "" ...
summary(df)
##     prodi           jenis_kelamin           usia         paham_isu     
##  Length:110         Length:110         Min.   :17.00   Min.   : 2.000  
##  Class :character   Class :character   1st Qu.:18.00   1st Qu.: 7.000  
##  Mode  :character   Mode  :character   Median :19.00   Median : 8.000  
##                                        Mean   :18.97   Mean   : 7.373  
##                                        3rd Qu.:20.00   3rd Qu.: 8.000  
##                                        Max.   :22.00   Max.   :10.000  
##                                                                        
##  medsos_panpol    kategori_brand     kepuasan_brand   kesadaran_selektif
##  Min.   : 2.000   Length:110         Min.   : 5.000   Min.   : 1.000    
##  1st Qu.: 8.000   Class :character   1st Qu.: 8.000   1st Qu.: 5.250    
##  Median : 9.000   Mode  :character   Median : 8.500   Median : 8.500    
##  Mean   : 8.582                      Mean   : 8.418   Mean   : 7.364    
##  3rd Qu.:10.000                      3rd Qu.: 9.000   3rd Qu.:10.000    
##  Max.   :10.000                      Max.   :10.000   Max.   :10.000    
##                                                                         
##  opini_panbrand   kepercayaan_setelah fre_beli_seblum  fre_beli_sesudah
##  Min.   : 1.000   Min.   : 1.000      Min.   : 2.000   Min.   : 1.000  
##  1st Qu.: 6.000   1st Qu.: 2.750      1st Qu.: 7.000   1st Qu.: 2.000  
##  Median : 8.000   Median : 5.000      Median : 8.000   Median : 4.000  
##  Mean   : 7.264   Mean   : 5.046      Mean   : 8.045   Mean   : 4.336  
##  3rd Qu.:10.000   3rd Qu.: 8.000      3rd Qu.: 9.000   3rd Qu.: 7.000  
##  Max.   :10.000   Max.   :10.000      Max.   :10.000   Max.   :10.000  
##                   NA's   :2                                            
##  dukung_atau_tidak  platform_marketing beli_atau_ganti    alasan_beli       
##  Length:110         Length:110         Length:110         Length:110        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##  alasan_ganti      
##  Length:110        
##  Class :character  
##  Mode  :character  
##                    
##                    
##                    
## 

CHECKING & HANDLING MISSING VALUE

library(mice)
md.pattern(df, rotate.names = T)

##     prodi jenis_kelamin usia paham_isu medsos_panpol kategori_brand
## 108     1             1    1         1             1              1
## 2       1             1    1         1             1              1
##         0             0    0         0             0              0
##     kepuasan_brand kesadaran_selektif opini_panbrand fre_beli_seblum
## 108              1                  1              1               1
## 2                1                  1              1               1
##                  0                  0              0               0
##     fre_beli_sesudah dukung_atau_tidak platform_marketing beli_atau_ganti
## 108                1                 1                  1               1
## 2                  1                 1                  1               1
##                    0                 0                  0               0
##     alasan_beli alasan_ganti kepercayaan_setelah  
## 108           1            1                   1 0
## 2             1            1                   0 1
##               0            0                   2 2
# 1. Cek nilai yang hilang
if (any(is.na(df$kepercayaan_setelah))) {
  print("Missing values found in 'kepercayaan_setelah'. Imputing with mode.")
  
  # Hitung modus
  mode_value <- names(sort(table(df$kepercayaan_setelah), decreasing = TRUE))[1]
  
  # Imputasi nilai hilang dengan modus
  df$kepercayaan_setelah[is.na(df$kepercayaan_setelah)] <- mode_value
  
  print("Missing values in 'kepercayaan_setelah' have been imputed with mode.")
}
## [1] "Missing values found in 'kepercayaan_setelah'. Imputing with mode."
## [1] "Missing values in 'kepercayaan_setelah' have been imputed with mode."
# 2. Cek lagi apakah masih ada nilai yang hilang
if (any(is.na(df$kepercayaan_setelah))) {
  print("There are still missing values in 'kepercayaan_setelah'. Please check the data.")
} else {
  print("No missing values found in 'kepercayaan_setelah'.")
}
## [1] "No missing values found in 'kepercayaan_setelah'."
# 3. Jalankan md.pattern pada data keseluruhan
library(mice)
md.pattern(df, rotate.names = TRUE)
##  /\     /\
## {  `---'  }
## {  O   O  }
## ==>  V <==  No need for mice. This data set is completely observed.
##  \  \|/  /
##   `-----'

##     prodi jenis_kelamin usia paham_isu medsos_panpol kategori_brand
## 110     1             1    1         1             1              1
##         0             0    0         0             0              0
##     kepuasan_brand kesadaran_selektif opini_panbrand kepercayaan_setelah
## 110              1                  1              1                   1
##                  0                  0              0                   0
##     fre_beli_seblum fre_beli_sesudah dukung_atau_tidak platform_marketing
## 110               1                1                 1                  1
##                   0                0                 0                  0
##     beli_atau_ganti alasan_beli alasan_ganti  
## 110               1           1            1 0
##                   0           0            0 0
df_numeric <- df[, sapply(df, is.numeric)]
# Simpan parameter grafis awal
opar <- par()

# Continuous variables
## Descriptive graphics: Box-Plots Summary
### Order by variable name
### Set graphical parameters
par(
  mfrow = c(3, 3),
  mar = c(2, 2, 2, 1),
  oma = c(1, 1, 1, 1)
)

# Loop over variables
colors <- rainbow(length(df_numeric))
for (i in 1:ncol(df_numeric)) {
  # Identifikasi dan hapus outlier
  outliers <- boxplot(df_numeric[, i], plot = FALSE)$out
  df_outlier <- df_numeric[!df_numeric[, i] %in% outliers, , drop = FALSE]
  
  # Gambarkan boxplot beroutlier 
  boxplot(
    df_outlier[, c(i)],
    col = "#2fa42d",
    main = paste(colnames(df_numeric[i]),
    xlab = paste(colnames(df_numeric[i])),
    xaxt = "n",
    horizontal = TRUE
  ))
}

# Restore original graphical settings
par(opar)

replace_outliers <- function(var, lower_bound_var, upper_bound_var) {
  var[var < lower_bound_var] <- lower_bound_var
  var[var > upper_bound_var] <- upper_bound_var
  return(var)
}
q1Area <- quantile(df$paham_isu, 0.25)
q3Area <- quantile(df$paham_isu, 0.75)

iqrArea <- q3Area - q1Area  
lower_bound_area <- q1Area - 1.5 * iqrArea
upper_bound_area <- q3Area + 1.5 * iqrArea 
paham_isu_new <- replace_outliers(df$paham_isu, lower_bound_area, upper_bound_area)
q1Area <- quantile(df$medsos_panpol, 0.25)
q3Area <- quantile(df$medsos_panpol, 0.75)

iqrArea <- q3Area - q1Area  
lower_bound_area <- q1Area - 1.5 * iqrArea
upper_bound_area <- q3Area + 1.5 * iqrArea 
medsos_panpol_new <- replace_outliers(df$medsos_panpol, lower_bound_area, upper_bound_area)
q1Area <- quantile(df$kepuasan_brand, 0.25)
q3Area <- quantile(df$kepuasan_brand, 0.75)

iqrArea <- q3Area - q1Area  
lower_bound_area <- q1Area - 1.5 * iqrArea
upper_bound_area <- q3Area + 1.5 * iqrArea 
kepuasan_brand_new <- replace_outliers(df$kepuasan_brand, lower_bound_area, upper_bound_area)
q1Area <- quantile(df$fre_beli_seblum, 0.25)
q3Area <- quantile(df$fre_beli_seblum, 0.75)

iqrArea <- q3Area - q1Area  
lower_bound_area <- q1Area - 1.5 * iqrArea
upper_bound_area <- q3Area + 1.5 * iqrArea 
fre_beli_seblum_new <- replace_outliers(df$fre_beli_seblum, lower_bound_area, upper_bound_area)
opar <- par(mfrow = c(2,2))

par(
  mfrow = c(2, 2),
  mar = c(2, 2, 2, 1),
  oma = c(1, 1, 1, 1)
)


boxplot_paham_isu <- boxplot(paham_isu_new, main = "Adjusted paham isu", col = "orangered", at =1)

boxplot_medsos_panpol <- boxplot(medsos_panpol_new, main = "Adjusted medsos pandangan politik", col = "blue", at=2)

boxplot_kepuasan_brand <- boxplot(kepuasan_brand_new, main = "Adjusted kepuasan brand", col = "pink", at=3)

boxplot_fre_beli_seblum <- boxplot(fre_beli_seblum_new, main = "Adjusted frekuensi beli sebelum", col = "green", at =4)

par(opar)
# Transformasi menjadi data interval dengan normalisasi ke rentang [0, 1]
data_norm_paham_isu <- (df$paham_isu - min(df$paham_isu)) / (max(df$paham_isu) - min(df$paham_isu))

print(data_norm_paham_isu)
##   [1] 1.000 0.750 0.625 0.625 0.625 1.000 0.500 0.625 0.625 0.250 0.750 0.500
##  [13] 0.875 0.500 0.500 0.750 1.000 0.750 1.000 0.750 0.875 0.750 1.000 1.000
##  [25] 0.125 0.875 0.750 0.750 0.875 0.750 0.625 0.625 0.750 1.000 0.750 0.375
##  [37] 0.250 0.625 0.625 0.750 0.625 1.000 0.875 0.875 0.375 0.750 1.000 0.625
##  [49] 0.625 0.125 0.375 0.875 0.875 0.625 1.000 0.750 0.625 0.750 0.750 1.000
##  [61] 0.500 0.750 0.625 0.875 1.000 0.250 0.250 0.750 0.750 0.625 0.750 0.750
##  [73] 0.125 0.750 1.000 0.750 0.750 0.250 0.750 1.000 0.500 0.750 0.500 0.750
##  [85] 0.500 0.250 1.000 0.750 0.750 0.375 0.125 0.750 0.625 0.625 0.625 0.750
##  [97] 0.750 0.750 0.250 0.750 0.250 0.875 0.750 0.500 0.875 0.000 0.875 0.625
## [109] 0.625 0.500
data_norm_medsos_panpol <- (df$medsos_panpol - min(df$medsos_panpol)) / (max(df$medsos_panpol) - min(df$medsos_panpol))

print(data_norm_medsos_panpol)
##   [1] 1.000 0.875 0.750 1.000 0.625 0.375 0.750 0.875 0.750 0.500 0.625 0.750
##  [13] 1.000 0.875 0.750 0.625 0.500 1.000 1.000 1.000 0.750 0.875 1.000 1.000
##  [25] 1.000 0.875 0.875 1.000 1.000 0.875 0.625 0.500 0.875 0.875 1.000 1.000
##  [37] 0.500 1.000 0.875 1.000 0.875 1.000 1.000 1.000 0.875 1.000 1.000 0.875
##  [49] 0.875 0.750 0.875 1.000 1.000 0.875 0.250 1.000 0.875 0.750 0.750 1.000
##  [61] 0.750 0.875 0.625 0.000 1.000 0.750 1.000 0.875 1.000 1.000 1.000 1.000
##  [73] 0.500 0.750 0.625 0.750 0.875 0.500 1.000 1.000 0.625 1.000 0.750 1.000
##  [85] 0.750 0.750 0.875 1.000 1.000 0.625 0.125 1.000 0.750 0.625 0.875 0.375
##  [97] 0.500 0.750 1.000 0.750 0.750 0.875 0.875 0.875 0.875 0.625 0.875 1.000
## [109] 1.000 1.000
data_norm_kepuasan_brand <- (df$kepuasan_brand - min(df$kepuasan_brand)) / (max(df$kepuasan_brand) - min(df$kepuasan_brand))

print(data_norm_kepuasan_brand)
##   [1] 1.0 1.0 0.8 1.0 0.6 1.0 0.8 0.8 0.6 0.6 0.8 0.4 0.8 0.8 0.8 0.8 0.6 0.6
##  [19] 1.0 0.8 0.8 0.6 0.4 0.6 0.0 0.6 0.6 0.8 0.6 0.6 0.2 0.6 0.8 1.0 0.4 0.6
##  [37] 0.6 0.8 0.6 1.0 0.8 1.0 0.8 0.6 1.0 0.6 0.6 0.4 0.6 0.8 0.8 0.0 0.6 0.4
##  [55] 0.6 0.4 0.6 0.8 1.0 0.8 1.0 0.8 0.6 1.0 0.8 1.0 0.0 0.4 0.8 0.4 0.6 1.0
##  [73] 0.6 0.6 0.8 1.0 0.2 0.6 0.8 0.2 0.6 0.8 0.6 1.0 0.6 0.8 0.8 0.8 0.8 1.0
##  [91] 0.8 0.8 0.6 0.6 0.6 1.0 0.6 0.6 1.0 0.8 0.2 0.8 0.6 1.0 0.4 0.8 0.6 1.0
## [109] 0.0 0.4
data_norm_kesadaran_selektif <- (df$kesadaran_selektif - min(df$kesadaran_selektif)) / (max(df$kesadaran_selektif) - min(df$kesadaran_selektif))

print(data_norm_kesadaran_selektif)
##   [1] 1.0000000 0.1111111 0.5555556 0.8888889 0.7777778 1.0000000 0.8888889
##   [8] 0.8888889 0.6666667 0.4444444 1.0000000 0.4444444 0.8888889 0.8888889
##  [15] 0.7777778 0.8888889 1.0000000 1.0000000 0.0000000 1.0000000 1.0000000
##  [22] 0.8888889 0.7777778 1.0000000 1.0000000 0.8888889 1.0000000 0.8888889
##  [29] 1.0000000 0.7777778 0.4444444 0.6666667 0.5555556 0.0000000 1.0000000
##  [36] 0.2222222 0.1111111 0.4444444 0.6666667 1.0000000 0.8888889 1.0000000
##  [43] 1.0000000 1.0000000 0.6666667 1.0000000 1.0000000 0.0000000 0.8888889
##  [50] 0.1111111 0.8888889 1.0000000 0.8888889 0.7777778 0.6666667 1.0000000
##  [57] 0.7777778 1.0000000 1.0000000 1.0000000 0.0000000 1.0000000 0.7777778
##  [64] 0.7777778 0.4444444 0.0000000 0.6666667 0.7777778 1.0000000 0.6666667
##  [71] 0.0000000 1.0000000 0.8888889 0.4444444 1.0000000 0.0000000 1.0000000
##  [78] 0.3333333 0.4444444 1.0000000 0.2222222 1.0000000 0.5555556 0.7777778
##  [85] 0.5555556 0.0000000 0.8888889 0.8888889 0.1111111 0.3333333 0.2222222
##  [92] 1.0000000 0.8888889 0.5555556 0.4444444 0.1111111 0.7777778 0.7777778
##  [99] 0.7777778 0.4444444 0.4444444 0.8888889 0.8888889 0.6666667 1.0000000
## [106] 0.6666667 0.8888889 0.8888889 0.8888889 1.0000000
data_norm_opini_panbrand <- (df$opini_panbrand - min(df$opini_panbrand)) / (max(df$opini_panbrand) - min(df$opini_panbrand))

print(data_norm_opini_panbrand)
##   [1] 1.0000000 0.1111111 0.4444444 1.0000000 0.6666667 0.1111111 0.7777778
##   [8] 0.7777778 0.6666667 0.6666667 0.8888889 0.8888889 0.8888889 1.0000000
##  [15] 0.3333333 0.7777778 0.0000000 0.8888889 1.0000000 0.4444444 0.7777778
##  [22] 0.8888889 0.4444444 0.8888889 0.2222222 1.0000000 0.6666667 0.8888889
##  [29] 1.0000000 0.8888889 0.4444444 0.6666667 0.6666667 0.0000000 0.7777778
##  [36] 1.0000000 0.1111111 0.7777778 0.6666667 1.0000000 1.0000000 1.0000000
##  [43] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 0.1111111 0.8888889
##  [50] 0.5555556 0.8888889 1.0000000 1.0000000 0.6666667 0.6666667 1.0000000
##  [57] 0.7777778 0.0000000 0.0000000 1.0000000 0.4444444 0.7777778 0.6666667
##  [64] 0.6666667 0.6666667 0.0000000 1.0000000 0.7777778 0.8888889 1.0000000
##  [71] 0.0000000 1.0000000 1.0000000 0.5555556 1.0000000 0.6666667 1.0000000
##  [78] 0.3333333 0.6666667 1.0000000 0.3333333 1.0000000 0.7777778 0.6666667
##  [85] 0.6666667 0.0000000 0.8888889 1.0000000 0.2222222 0.6666667 0.0000000
##  [92] 1.0000000 0.8888889 0.6666667 0.5555556 0.0000000 0.5555556 0.7777778
##  [99] 0.8888889 0.6666667 0.3333333 0.8888889 0.7777778 0.5555556 0.8888889
## [106] 0.3333333 0.8888889 1.0000000 0.8888889 1.0000000
# Konversi ke numerik
df$kepercayaan_setelah <- as.numeric(df$kepercayaan_setelah)

data_norm_kepercayaan_setelah <- (df$kepercayaan_setelah - min(df$kepercayaan_setelah)) / (max(df$kepercayaan_setelah) - min(df$kepercayaan_setelah))

print(data_norm_kepercayaan_setelah)
##   [1] 0.2222222 0.4444444 0.7777778 0.5555556 0.2222222 1.0000000 0.8888889
##   [8] 0.8888889 0.2222222 0.6666667 0.7777778 0.4444444 0.2222222 0.1111111
##  [15] 0.6666667 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
##  [22] 0.2222222 0.2222222 0.0000000 0.4444444 1.0000000 0.4444444 0.1111111
##  [29] 0.0000000 0.4444444 0.5555556 0.4444444 0.3333333 1.0000000 0.0000000
##  [36] 0.2222222 0.7777778 0.4444444 0.6666667 0.0000000 0.3333333 1.0000000
##  [43] 0.1111111 0.0000000 0.2222222 0.0000000 0.0000000 0.6666667 0.1111111
##  [50] 0.7777778 1.0000000 0.0000000 0.0000000 0.0000000 0.5555556 1.0000000
##  [57] 0.3333333 0.0000000 1.0000000 0.8888889 0.0000000 0.4444444 0.0000000
##  [64] 0.5555556 0.5555556 1.0000000 0.0000000 0.3333333 0.0000000 0.5555556
##  [71] 0.7777778 1.0000000 0.2222222 0.4444444 1.0000000 1.0000000 0.1111111
##  [78] 0.4444444 0.4444444 0.8888889 0.3333333 1.0000000 0.2222222 0.2222222
##  [85] 0.5555556 0.4444444 0.8888889 0.0000000 0.3333333 0.6666667 0.7777778
##  [92] 0.7777778 0.6666667 0.3333333 0.4444444 0.8888889 0.3333333 0.4444444
##  [99] 0.5555556 0.6666667 0.4444444 0.8888889 0.4444444 0.0000000 0.0000000
## [106] 0.4444444 0.3333333 0.7777778 0.3333333 0.1111111
data_norm_fre_beli_seblum <- (df$fre_beli_seblum - min(df$fre_beli_seblum)) / (max(df$fre_beli_seblum) - min(df$fre_beli_seblum))

print(data_norm_fre_beli_seblum)
##   [1] 1.000 0.750 0.625 0.500 0.500 0.750 0.750 0.875 0.875 0.750 0.750 0.750
##  [13] 1.000 0.875 0.875 0.875 0.250 0.875 1.000 1.000 0.875 0.500 0.500 1.000
##  [25] 0.375 0.750 0.875 0.875 0.750 0.875 0.500 0.625 0.875 1.000 0.750 0.750
##  [37] 0.625 0.750 0.875 1.000 0.875 1.000 1.000 0.625 1.000 0.500 0.625 0.625
##  [49] 0.875 0.750 0.875 0.750 0.750 0.750 0.750 0.750 0.500 0.625 0.500 0.625
##  [61] 0.750 0.750 0.625 0.750 0.625 1.000 0.000 0.625 0.500 0.750 0.750 0.750
##  [73] 0.625 0.750 0.875 0.750 0.625 0.875 0.875 0.250 0.625 1.000 0.750 0.875
##  [85] 0.875 0.625 0.750 1.000 0.500 1.000 0.625 0.875 1.000 0.750 0.875 1.000
##  [97] 0.625 0.750 0.750 0.875 0.750 0.875 0.625 0.875 0.625 1.000 0.750 1.000
## [109] 0.500 1.000
data_norm_fre_beli_sesudah <- (df$fre_beli_sesudah - min(df$fre_beli_sesudah)) / (max(df$fre_beli_sesudah) - min(df$fre_beli_sesudah))

print(data_norm_fre_beli_sesudah)
##   [1] 0.0000000 0.7777778 0.6666667 0.3333333 0.1111111 0.7777778 0.7777778
##   [8] 0.1111111 0.2222222 0.1111111 0.1111111 0.4444444 0.1111111 0.3333333
##  [15] 0.5555556 0.3333333 0.3333333 0.2222222 1.0000000 0.0000000 0.0000000
##  [22] 0.3333333 0.2222222 0.0000000 0.2222222 0.1111111 0.3333333 0.1111111
##  [29] 0.1111111 0.5555556 0.6666667 0.4444444 0.5555556 1.0000000 0.0000000
##  [36] 0.2222222 0.6666667 0.7777778 0.4444444 0.3333333 0.0000000 0.0000000
##  [43] 0.0000000 0.0000000 0.4444444 0.0000000 0.7777778 0.6666667 0.0000000
##  [50] 0.7777778 0.0000000 0.0000000 0.0000000 0.0000000 0.3333333 0.0000000
##  [57] 0.1111111 0.0000000 1.0000000 0.1111111 0.7777778 0.0000000 0.1111111
##  [64] 0.4444444 0.4444444 1.0000000 0.0000000 0.3333333 0.0000000 0.5555556
##  [71] 0.6666667 0.1111111 0.5555556 0.5555556 0.0000000 0.7777778 0.0000000
##  [78] 0.6666667 0.6666667 0.0000000 0.6666667 0.0000000 0.3333333 0.6666667
##  [85] 0.6666667 0.6666667 0.7777778 0.4444444 0.5555556 1.0000000 0.6666667
##  [92] 0.4444444 0.2222222 0.5555556 0.6666667 0.8888889 0.2222222 0.5555556
##  [99] 0.6666667 0.6666667 0.4444444 0.5555556 0.0000000 0.5555556 0.0000000
## [106] 0.5555556 0.1111111 0.5555556 0.3333333 0.0000000
df_norm <- data.frame(
  pahamIsu = data_norm_paham_isu,
  kepuasanBrand = data_norm_kepuasan_brand,
  medPanSos = data_norm_medsos_panpol,
  kesadaranSelektif = data_norm_kesadaran_selektif,
  opiniPandBrand = data_norm_opini_panbrand,
  kepercayaanSetelah = data_norm_kepercayaan_setelah,
  freBeliSbelum = data_norm_fre_beli_seblum,
  freBeliSesudah = data_norm_fre_beli_sesudah
)
df$norm_fre_beli_sesudah <- (df$fre_beli_sesudah - min(df$fre_beli_sesudah)) / (max(df$fre_beli_sesudah) - min(df$fre_beli_sesudah))

df$norm_fre_beli_seblum <- (df$fre_beli_seblum - min(df$fre_beli_seblum)) / (max(df$fre_beli_seblum) - min(df$fre_beli_seblum))

df$kepercayaan_setelah <- as.numeric(df$kepercayaan_setelah)
df$norm_kepercayaan_setelah <- (df$kepercayaan_setelah - min(df$kepercayaan_setelah)) / (max(df$kepercayaan_setelah) - min(df$kepercayaan_setelah))

df$norm_opini_panbrand <- (df$opini_panbrand - min(df$opini_panbrand)) / (max(df$opini_panbrand) - min(df$opini_panbrand))

df$norm_kesadaran_selektif <- (df$kesadaran_selektif - min(df$kesadaran_selektif)) / (max(df$kesadaran_selektif) - min(df$kesadaran_selektif))

df$norm_kepuasan_brand <- (df$kepuasan_brand - min(df$kepuasan_brand)) / (max(df$kepuasan_brand) - min(df$kepuasan_brand))

df$norm_medsos_panpol <- (df$medsos_panpol - min(df$medsos_panpol)) / (max(df$medsos_panpol) - min(df$medsos_panpol))

df$norm_paham_isu <- (df$paham_isu - min(df$paham_isu)) / (max(df$paham_isu) - min(df$paham_isu))

# Print data frame df dengan kolom baru
View(df)
# Misalkan df_norm adalah data yang sudah dinormalisasi
cov_matrix <- cov(df[, c("norm_fre_beli_sesudah", "norm_fre_beli_seblum", "norm_kepercayaan_setelah", "norm_kepuasan_brand", "norm_opini_panbrand", "norm_kesadaran_selektif", "norm_medsos_panpol")])

print(cov_matrix)
##                          norm_fre_beli_sesudah norm_fre_beli_seblum
## norm_fre_beli_sesudah              0.094524243          0.007176119
## norm_fre_beli_seblum               0.007176119          0.034514439
## norm_kepercayaan_setelah           0.041568591          0.008809425
## norm_kepuasan_brand                0.027222686          0.022914929
## norm_opini_panbrand               -0.050940599          0.008496664
## norm_kesadaran_selektif           -0.067216508         -0.002397832
## norm_medsos_panpol                -0.018415809          0.002449958
##                          norm_kepercayaan_setelah norm_kepuasan_brand
## norm_fre_beli_sesudah                 0.041568591         0.027222686
## norm_fre_beli_seblum                  0.008809425         0.022914929
## norm_kepercayaan_setelah              0.115632369         0.024312853
## norm_kepuasan_brand                   0.024312853         0.058995830
## norm_opini_panbrand                  -0.020411042        -0.009200259
## norm_kesadaran_selektif              -0.029777901        -0.012584561
## norm_medsos_panpol                   -0.013539060        -0.005909091
##                          norm_opini_panbrand norm_kesadaran_selektif
## norm_fre_beli_sesudah           -0.050940599            -0.067216508
## norm_fre_beli_seblum             0.008496664            -0.002397832
## norm_kepercayaan_setelah        -0.020411042            -0.029777901
## norm_kepuasan_brand             -0.009200259            -0.012584561
## norm_opini_panbrand              0.099824957             0.058381985
## norm_kesadaran_selektif          0.058381985             0.102781124
## norm_medsos_panpol               0.029323047             0.017167084
##                          norm_medsos_panpol
## norm_fre_beli_sesudah          -0.018415809
## norm_fre_beli_seblum            0.002449958
## norm_kepercayaan_setelah       -0.013539060
## norm_kepuasan_brand            -0.005909091
## norm_opini_panbrand             0.029323047
## norm_kesadaran_selektif         0.017167084
## norm_medsos_panpol              0.041107173
library(tidyverse)
results <- prcomp(df[, c("norm_fre_beli_sesudah", "norm_fre_beli_seblum", "norm_kepercayaan_setelah", "norm_kepuasan_brand", "norm_opini_panbrand", "norm_kesadaran_selektif", "norm_medsos_panpol")], scale = TRUE)

results$rotation <- -1*results$rotation

results$rotation
##                                 PC1        PC2         PC3          PC4
## norm_fre_beli_sesudah    -0.5150804 0.01072922  0.29581461 -0.000110091
## norm_fre_beli_seblum     -0.1097399 0.67200796 -0.02273048  0.303314134
## norm_kepercayaan_setelah -0.3324195 0.19286891 -0.12364675 -0.885663758
## norm_kepuasan_brand      -0.2906987 0.55636272 -0.17592721  0.182496079
## norm_opini_panbrand       0.4418254 0.34366328  0.01838677 -0.231674971
## norm_kesadaran_selektif   0.4752822 0.15517052 -0.45360804 -0.120381473
## norm_medsos_panpol        0.3264016 0.24365950  0.81218213 -0.148762970
##                                  PC5        PC6         PC7
## norm_fre_beli_sesudah     0.05459907 -0.4752281 -0.64672093
## norm_fre_beli_seblum     -0.57594761  0.2874571 -0.17175300
## norm_kepercayaan_setelah -0.08626319  0.2058060  0.05303433
## norm_kepuasan_brand       0.66474655 -0.1090530  0.29651150
## norm_opini_panbrand      -0.22986366 -0.7385206  0.18553848
## norm_kesadaran_selektif   0.30168931  0.1257314 -0.65034770
## norm_medsos_panpol        0.26855211  0.2757843 -0.06437838
results$x <- -1*results$x

head(results$x)
##             PC1          PC2          PC3        PC4       PC5         PC6
## [1,]  1.4572102  2.156682451 -0.222657799  0.7446049 0.3874414  0.32167064
## [2,] -2.6774313 -0.141743529  1.180698238  0.8346582 0.8888825  0.42621962
## [3,] -1.5801478 -0.439367235  0.002566404 -0.7074202 0.6349024 -0.07878375
## [4,]  0.7035329  0.494990856  0.195243476 -0.8982925 1.8075932 -0.80904887
## [5,]  0.6458586 -1.485076989 -0.972023679  0.2307139 0.3992063 -0.26221483
## [6,] -2.7076860  0.002697531 -2.281919410 -0.5792099 0.9221428  0.43095006
##             PC7
## [1,]  0.4339650
## [2,]  0.3845506
## [3,] -0.1244766
## [4,]  0.4724239
## [5,]  0.5483023
## [6,] -1.1732093
# Contoh menggunakan prcomp
pca_result <- prcomp(iris[, 1:4])

# Membuat biplot dengan pengaturan panjang dan lebar
biplot(pca_result, scale = 0, xlim = c(0, 1), ylim = c(0, 1))

results$sdev^2 / sum(results$sdev^2)
## [1] 0.39362955 0.22153843 0.11561734 0.11026147 0.06734221 0.05319723 0.03841377
library(ggplot2)
#calculate total variance explained by each principal component
var_explained = results$sdev^2 / sum(results$sdev^2)

#create scree plot
qplot(c(1:7), var_explained) + 
  geom_line() + 
  xlab("Principal Component") + 
  ylab("Variance Explained") +
  ggtitle("Scree Plot") +
  ylim(0, 1)

# Visualisasi

Kategorik

Prodi x Jenis Kelamin

library(ggplot2)
ggplot(df, aes(x = prodi,
               fill = jenis_kelamin))+ 
  geom_bar(position = "stack")+
  scale_fill_manual(values = c("Laki-laki" = "deepskyblue", "Perempuan" = "deeppink")) +
   theme(axis.text.x = element_text(angle = 10, hjust = 1)) 

Brand

brand = c("FnB (Food and Beverage)", "Skincare and Cosmetics", "Sport", "FnB (Food and Beverage)", "Sport",
          "FnB (Food and Beverage)", "FnB (Food and Beverage)", "FnB (Food and Beverage)", 
          "FnB (Food and Beverage)", "Electronics", "FnB (Food and Beverage)", "FnB (Food and Beverage)", 
          "FnB (Food and Beverage)", "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "FnB (Food and Beverage)", "Electronics", 
          "Home Care Product", "FnB (Food and Beverage)", "Skincare and Cosmetics", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "FnB (Food and Beverage)", "Fashion",
          "FnB (Food and Beverage)", "Home Care Product", "FnB (Food and Beverage)", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion, Electronics", "Home Care Product",
          "Sport","FnB (Food and Beverage)", "Skincare and Cosmetics","FnB (Food and Beverage)", 
          "Home Care Product", "FnB (Food and Beverage)", "Skincare and Cosmetics", "Sport",
          "FnB (Food and Beverage)","Electronics", "Sport", "Skincare and Cosmetics", "Home Care Product",
          "FnB (Food and Beverage)","FnB (Food and Beverage)", "Skincare and Cosmetics", "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Home Care Product", "FnB (Food and Beverage)",
          "Electronics", "Home Care Product", "FnB (Food and Beverage)", "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "FnB (Food and Beverage)"," Skincare and Cosmetics", "Electronics", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Electronics", "Sport",
          "Home Care Product", "FnB (Food and Beverage)", "Skincare and Cosmetics", "FnB (Food and Beverage)",
          "Home Care Product", "Sport", "FnB (Food and Beverage)", "FnB (Food and Beverage)", "Fashion",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Home Care Product", "FnB (Food and Beverage)",
          "Skincare and Cosmetics", "Fashion", "FnB (Food and Beverage)", "Skincare and Cosmetics", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Home Care Product", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Electronics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion",
          "FnB (Food and Beverage)", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion","Home Care Product", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Home Care Product",
          "FnB (Food and Beverage)", "Fashion", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "Home Care Product", "FnB (Food and Beverage)", "Home Care Product", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Electronics", "Home Care Product",
          "FnB (Food and Beverage)", "Electronics", "Home Care Product", "Sport",
          "FnB (Food and Beverage)", "Electronics", "Home Care Product", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "FnB (Food and Beverage)", "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Sport",
          "FnB (Food and Beverage)", "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "Home Care Product", "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Sport", "FnB (Food and Beverage)",
          "Fashion", "FnB (Food and Beverage)", "FnB (Food and Beverage)", "Skincare and Cosmetics", 
          "Home Care Product", "FnB (Food and Beverage)", "Fashion", "FnB (Food and Beverage)", "Electronics",
          "Home Care Product", "Sport", "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "Fashion", "Electronics", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion",
          "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "Fashion", "Home Care Product",
          "FnB (Food and Beverage)", "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Electronics", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "Fashion", "Electronics", "Home Care Product", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Fashion", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Home Care Product",
          "FnB (Food and Beverage)", "Sport",
          "FnB (Food and Beverage)", "Home Care Product",
          "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "Electronics", "Home Care Product", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion", "Home Care Product",
          "FnB (Food and Beverage)",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Home Care Product", "Sport", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Fashion",
          "FnB (Food and Beverage)", "Home Care Product",
          "FnB (Food and Beverage)", "Skincare and Cosmetics", "Home Care Product", "Sport",
          "FnB (Food and Beverage)", "Skincare and Cosmetics")

brand <- data.frame(brand)

# Menghitung jumlah setiap brand
count_data <- table(brand$brand)

# Membuat data frame dari hasil perhitungan
df_count <- data.frame(brand = names(count_data), jumlah = as.numeric(count_data))

# Membuat bar chart
ggplot(df_count, aes(x = reorder(brand, jumlah), y = jumlah)) +
  geom_bar(stat = "identity", fill = "brown1") +
  labs(title = "Distribusi Brand",
       x = "Brand",
       y = "Jumlah") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Dukung or No

plotdata2 <- df %>%
  count(dukung_atau_tidak) %>%
  arrange(desc(dukung_atau_tidak)) %>%
  mutate(prop = round(n * 100 / sum(n), 1), lab.ypops = cumsum(prop) - 0.5 * prop)

library(ggplot2)

ggplot(plotdata2, aes(x = "", y = prop, fill = dukung_atau_tidak)) +
  geom_bar(width = 1, stat = "identity", color = "black") +
  geom_text(aes(label = paste0(prop, "%"), y = lab.ypops), color = "black") +
  coord_polar("y", start = 0, direction = -1) +
  scale_fill_manual(values = c("Sangat mendukung" = "chartreuse", "Tidak mendukung" = "red2", "Netral" = "lightyellow")) +
  labs(fill = "Keputusan terhadap Boikot Produk Pro-Israel?")+
  theme_void()

Platform marketing

ggplot(df, aes(x= platform_marketing))+
  geom_bar(fill = "coral",color = "black")

### Beli atau ganti

plotdata2 <- df %>%
  count(beli_atau_ganti)%>%
  arrange(desc(beli_atau_ganti))%>%
  mutate(prop=round(n*100/sum(n),1),lab.ypops = cumsum(prop)-0.5*prop)

ggplot(plotdata2,aes(x = ",",y = prop, fill = beli_atau_ganti))+
  geom_bar(width = 1,stat = "identity",color = "black")+
  geom_text(aes(label = paste0(prop, "%"), y = lab.ypops), color = "black") +
  coord_polar("y",start = 0,direction = -1)+
  labs(fill = "Keputusan terhadap Produk Israel?")+
  theme_void()

Alasan beli

library(wordcloud)
library(tm)
library(SnowballC)

# Example text data
text_data <- df$alasan_beli

# Convert the text data to a Corpus
corpus <- Corpus(VectorSource(text_data))

# Create a word cloud
dtm <- DocumentTermMatrix(corpus)


# Generate a word cloud
wordcloud(words = text_data, min.freq = 1, scale = c(3, 0.5), colors = brewer.pal(8, "Dark2"))

Alasan ganti

# Example text data
text_data <- df$alasan_ganti

# Convert the text data to a Corpus
corpus <- Corpus(VectorSource(text_data))

# Create a word cloud
dtm <- DocumentTermMatrix(corpus)


# Generate a word cloud
wordcloud(words = text_data, min.freq = 1, scale = c(3, 0.5), colors = brewer.pal(8, "Dark2"))

Numerik

usia

ggplot(df, aes(x=usia))+
  geom_bar(fill = "darkturquoise",
                 color = "white")+
  scale_x_continuous(breaks = c(17,18,19,20,21,22))

### norm paham isu

ggplot(df, aes(x=norm_paham_isu))+
  geom_density(fill = "darkgoldenrod1")+
  labs(title = "Tingkat Pemahaman Isu Palestina-Israel",
       x = "Tingkat Pemahaman",
       y = "Density")

norm medsos

ggplot(df, aes(x=norm_medsos_panpol))+
  geom_density(fill = "green")+
  labs(title = "Seberapa Media Sosial mempengaruhi Pandangan Isu Palestina-Israel",
       x = "Tingkat Pengaruh",
       y = "Density")

norm kepuasan brand

ggplot(df, aes(x=norm_kepuasan_brand))+
  geom_density(fill = "darkgoldenrod1")+
  labs(title = "Tingkat Kepuasan Brand Pro-Israel",
       x = "Tingkat Kepuasan",
       y = "Density")

norm kesadaran selektif

ggplot(df, aes(x=norm_kesadaran_selektif))+
  geom_density(fill = "green")+
  labs(title = "Tingkat Kesadaran Selektif Brand",
       x = "Tingkat Kesadaran",
       y = "Density")

norm opini brand

ggplot(df, aes(x=norm_opini_panbrand))+
  geom_density(fill = "green")+
  labs(title = "Seberapa baik Opini/Penilaian terhadap Brand Israel",
       x = "Penilaian",
       y = "Density")

norm kepercayaan setelah

ggplot(df, aes(x=norm_kepercayaan_setelah))+
  geom_density(fill = "orangered")+
  labs(title = "Tingkat Kepercayaan Brand Pro-Israel setelah Isu Palestina-Israel",
       x = "Tingkat kepercayaan",
       y = "Density")

norm frek beli sblm

ggplot(df, aes(x=norm_fre_beli_seblum))+
  geom_density(fill = "darkgoldenrod1")+
  labs(title = "Frekuensi Membeli Produk sebelum ada Isu Palestina-Israel",
       x = "frekuensi membeli",
       y = "Density")

norm frek beli stlh

ggplot(df, aes(x=norm_fre_beli_sesudah))+
  geom_density(fill = "orangered")+
  labs(title = "Frekuensi Membeli Produk setelah ada Isu Palestina-Israel",
       x = "Frekuensi Membeli",
       y = "Density")

### Korelasi ##### Paham Isu vs Kesadaran Selektif

ggplot(df,aes(x = norm_paham_isu,y = norm_kesadaran_selektif))+
  geom_point(color= "cornflowerblue")

dukung vs beli

library(ggplot2)
ggplot(df, aes(x = dukung_atau_tidak,
               fill = beli_atau_ganti))+ 
  geom_bar(position = "stack") +
  labs(x = "Keputusan terhadap Boikot Produk Pro-Israel",
       fill = "Keputusan terhadap Produk Pro-Israel") +
   theme(axis.text.x = element_text(angle = 10, hjust = 1))