# 验证环境是否为 R
print("Hello R from Google Colab!")
## [1] "Hello R from Google Colab!"
sessionInfo()
## R version 4.6.1 (2026-06-24 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 11 x64 (build 26100)
##
## Matrix products: default
## LAPACK version 3.12.1
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.utf8 LC_CTYPE=Chinese (Simplified)_China.utf8
## [3] LC_MONETARY=Chinese (Simplified)_China.utf8 LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.utf8
##
## time zone: Asia/Shanghai
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] rsconnect_1.11.2
##
## loaded via a namespace (and not attached):
## [1] digest_0.6.39 R6_2.6.1 fastmap_1.2.0 xfun_0.61 magrittr_2.0.5 glue_1.8.1
## [7] cachem_1.1.0 knitr_1.52 htmltools_0.5.9 rmarkdown_2.32 lifecycle_1.0.5 cli_3.6.6
## [13] sass_0.4.10 jquerylib_0.1.4 compiler_4.6.1 tools_4.6.1 curl_8.1.0 evaluate_1.0.5
## [19] bslib_0.12.0 httr2_1.3.0 yaml_2.3.12 rlang_1.3.0 jsonlite_2.0.0
# ==========================================
# WQD7004 Group Project - Quick Test Pipeline
# ==========================================
# 1. 自动安装并加载所需依赖包
suppressMessages({
if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")
if (!requireNamespace("dplyr", quietly = TRUE)) install.packages("dplyr")
library(ggplot2)
library(dplyr)
})
## package 'generics' successfully unpacked and MD5 sums checked
## package 'tidyselect' successfully unpacked and MD5 sums checked
## package 'dplyr' successfully unpacked and MD5 sums checked
##
## 下载的二进制程序包在
## C:\Users\Admin(无密码)\AppData\Local\Temp\RtmpwliEYD\downloaded_packages里
# 2. 生成模拟数据集 (尺寸: 10,000 行 x 12 列 = 120,000 > 100,000 满足作业要求)
set.seed(42)
n_rows <- 10000
df <- data.frame(
CustomerID = 1:n_rows,
Age = sample(18:70, n_rows, replace = TRUE),
Income = round(rnorm(n_rows, mean = 50000, sd = 15000)),
CreditScore = sample(300:850, n_rows, replace = TRUE),
YearsWithCompany = sample(1:15, n_rows, replace = TRUE),
NumProducts = sample(1:4, n_rows, replace = TRUE),
IsActiveMember = sample(c(0, 1), n_rows, replace = TRUE),
Country = sample(c("Malaysia", "Singapore", "Thailand"), n_rows, replace = TRUE),
DeviceType = sample(c("Mobile", "Desktop"), n_rows, replace = TRUE),
SatisfactionScore = sample(1:5, n_rows, replace = TRUE),
# 连续型目标变量 (用于回归模型): TotalSpend
TotalSpend = round(200 + 0.05 * rnorm(n_rows, mean=50000, sd=15000) + rnorm(n_rows, mean=500, sd=200)),
# 二分类目标变量 (用于分类模型): Churn (流失: 0/1)
Churn = sample(c(0, 1), n_rows, replace = TRUE, prob = c(0.8, 0.2))
)
# 打印数据集维度 (检查是否突破 100,000)
dim_val <- nrow(df) * ncol(df)
cat("数据集维度 (Rows x Cols):", nrow(df), "x", ncol(df), "=", dim_val, "\n")
## 数据集维度 (Rows x Cols): 10000 x 12 = 120000
cat("维度要求验证:", ifelse(dim_val > 100000, "PASS (通过)", "FAIL (未通过)"), "\n\n")
## 维度要求验证: PASS (通过)
# 3. 数据探索与可视化 (Visualization)
cat("--- 1. 生成可视化图表 ---\n")
## --- 1. 生成可视化图表 ---
# 图 1: 收入与消费额散点图
p1 <- ggplot(df, aes(x = Income, y = TotalSpend, color = Country)) +
geom_point(alpha = 0.3) +
theme_minimal() +
labs(title = "Income vs Total Spend by Country", x = "Annual Income ($)", y = "Total Spend ($)")
print(p1)

# 图 2: 不同国家客户流失分布直方图
p2 <- ggplot(df, aes(x = factor(Churn), fill = Country)) +
geom_bar(position = "dodge") +
theme_light() +
labs(title = "Customer Churn Distribution by Country", x = "Churn Status (0 = Retained, 1 = Churned)", y = "Count")
print(p2)

# 4. 问题 1: 回归模型 (Regression - 预测消费金额)
cat("\n--- 2. 构建回归模型 (Linear Regression) ---\n")
##
## --- 2. 构建回归模型 (Linear Regression) ---
reg_model <- lm(TotalSpend ~ Age + Income + CreditScore + YearsWithCompany, data = df)
print(summary(reg_model)$coefficients)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.135493e+03 4.704114e+01 66.6542737 0.0000000
## Age 4.664339e-01 5.066421e-01 0.9206378 0.3572618
## Income 3.582110e-04 5.109405e-04 0.7010816 0.4832684
## CreditScore 3.637634e-02 4.851797e-02 0.7497499 0.4534230
## YearsWithCompany 7.080808e-01 1.789661e+00 0.3956509 0.6923711
# 5. 问题 2: 分类模型 (Classification - 预测客户流失)
cat("\n--- 3. 构建分类模型 (Logistic Regression) ---\n")
##
## --- 3. 构建分类模型 (Logistic Regression) ---
clf_model <- glm(Churn ~ Age + Income + IsActiveMember + SatisfactionScore, data = df, family = binomial)
print(summary(clf_model)$coefficients)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.355693e+00 1.269412e-01 -10.6796902 1.266937e-26
## Age 3.995454e-04 1.631305e-03 0.2449238 8.065155e-01
## Income -1.934082e-06 1.645655e-06 -1.1752660 2.398883e-01
## IsActiveMember 9.043480e-03 4.983618e-02 0.1814641 8.560033e-01
## SatisfactionScore 1.878583e-02 1.761712e-02 1.0663397 2.862701e-01
cat("\n所有代码成功运行完成!\n")
##
## 所有代码成功运行完成!