Topik ini dilakukan terhadap sebuah dataset yang diperoleh dari www.kaggle.com yang bernama ‘Online Shoppers Purchasing Intention’. Dari dataset tersebut ingin diketahui apakah pengunjung sebuah toko online diprediksi melakukan pembelian atau tidak.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
set.seed(123) # Untuk reproduksibilitas
data <- data.frame(
Gender = c(rep("Male", 50), rep("Female", 50)),
VisitorType = c(rep("Returning", 30), rep("New", 70)),
Age = c(runif(50, 18, 45), runif(50, 18, 45)),
PageValues = c(runif(50, 0, 50), runif(50, 10, 100))
)
gender_counts <- table(data$Gender)
gender_proportions <- prop.table(gender_counts) * 100
pie(
gender_counts,
labels = paste(names(gender_counts), "(", round(gender_proportions, 1), "%)", sep = ""),
main = "Pie Chart of Gender"
)
library(ggplot2)
ggplot(data, aes(x = VisitorType)) +
geom_bar(fill = "steelblue") +
labs(title = "Bar Chart of VisitorType", x = "Visitor Type", y = "Count") +
theme_minimal()
cat("Statistik Deskriptif untuk Age:\n")
## Statistik Deskriptif untuk Age:
summary_age <- summary(data$Age)
print(summary_age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 18.02 24.63 30.59 31.46 38.40 44.85
ggplot(data, aes(x = Age)) +
geom_histogram(binwidth = 5, fill = "green", color = "black") +
labs(title = "Histogram of Age", x = "Age", y = "Count") +
theme_minimal()
cat("Statistik Deskriptif untuk PageValues:\n")
## Statistik Deskriptif untuk PageValues:
summary_pagevalues <- summary(data$PageValues)
print(summary_pagevalues)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.5234 24.0366 38.5269 40.9724 54.8848 98.7077
ggplot(data, aes(x = PageValues)) +
geom_density(fill = "purple", alpha = 0.5) +
labs(title = "Density Plot of PageValues", x = "Page Values", y = "Density") +
theme_minimal()
ggplot(data, aes(x = Gender, y = PageValues, fill = Gender)) +
geom_boxplot() +
labs(title = "Boxplot of PageValues by Gender", x = "Gender", y = "Page Values") +
theme_minimal()
t_test <- t.test(PageValues ~ Gender, data = data)
cat("Hasil t-test untuk PageValues berdasarkan Gender:\n")
## Hasil t-test untuk PageValues berdasarkan Gender:
print(t_test)
##
## Welch Two Sample t-test
##
## data: PageValues by Gender
## t = 8.4039, df = 87.865, p-value = 6.976e-13
## alternative hypothesis: true difference in means between group Female and group Male is not equal to 0
## 95 percent confidence interval:
## 23.21555 37.59624
## sample estimates:
## mean in group Female mean in group Male
## 56.17539 25.76949
if (t_test$p.value < 0.05) {
cat("Kesimpulan: Ada perbedaan signifikan dalam rata-rata PageValues antara Male dan Female (p < 0.05).\n")
} else {
cat("Kesimpulan: Tidak ada perbedaan signifikan dalam rata-rata PageValues antara Male dan Female (p >= 0.05).\n")
}
## Kesimpulan: Ada perbedaan signifikan dalam rata-rata PageValues antara Male dan Female (p < 0.05).
chi_test <- chisq.test(table(data$Gender, data$VisitorType))
cat("Hasil Chi-Square Test untuk Gender vs VisitorType:\n")
## Hasil Chi-Square Test untuk Gender vs VisitorType:
print(chi_test)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table(data$Gender, data$VisitorType)
## X-squared = 40.048, df = 1, p-value = 2.478e-10
if (chi_test$p.value < 0.05) {
cat("Kesimpulan: Ada hubungan signifikan antara Gender dan VisitorType (p < 0.05).\n")
} else {
cat("Kesimpulan: Tidak ada hubungan signifikan antara Gender dan VisitorType (p >= 0.05).\n")
}
## Kesimpulan: Ada hubungan signifikan antara Gender dan VisitorType (p < 0.05).
get_mode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
stat_summary <- data.frame(
Metric = c("Mean", "Median", "Mode", "Q1", "Q3", "Range", "Variance", "Standard Deviation"),
Age = c(
mean(data$Age), median(data$Age), get_mode(data$Age),
quantile(data$Age, 0.25), quantile(data$Age, 0.75),
diff(range(data$Age)), var(data$Age), sd(data$Age)
),
PageValues = c(
mean(data$PageValues), median(data$PageValues), get_mode(data$PageValues),
quantile(data$PageValues, 0.25), quantile(data$PageValues, 0.75),
diff(range(data$PageValues)), var(data$PageValues), sd(data$PageValues)
)
)
print(stat_summary)
## Metric Age PageValues
## 1 Mean 31.461093 40.97244
## 2 Median 30.592010 38.52689
## 3 Mode 25.764593 29.99945
## 4 Q1 24.627708 24.03664
## 5 Q3 38.397724 54.88483
## 6 Range 26.828415 98.18432
## 7 Variance 59.211059 557.42267
## 8 Standard Deviation 7.694872 23.60980