# Lab 7-2: Feature Selection Techniques

# Load required packages
library(tidyverse)
library(sqldf)
library(corrplot)

# -----------------------------
# Step 1: Full Dataset Creation
# -----------------------------
set.seed(123)

customers_full <- data.frame(
  customer_id = 1:500,
  
  age = sample(18:75, 500, replace = TRUE),
  income = round(rnorm(500, 55000, 20000)),
  education_years = sample(12:20, 500, replace = TRUE),
  
  region = sample(c('North', 'South', 'East', 'West', 'Central'), 500, replace = TRUE),
  urban_rural = sample(c('Urban', 'Suburban', 'Rural'), 500, replace = TRUE),
  
  years_customer = sample(1:15, 500, replace = TRUE),
  monthly_purchases = sample(0:20, 500, replace = TRUE),
  avg_order_value = round(runif(500, 25, 300), 2),
  website_visits_month = sample(0:50, 500, replace = TRUE),
  email_opens_month = sample(0:30, 500, replace = TRUE),
  
  support_tickets_year = sample(0:12, 500, replace = TRUE),
  social_media_follower = sample(c(TRUE, FALSE), 500, replace = TRUE),
  newsletter_subscriber = sample(c(TRUE, FALSE), 500, replace = TRUE),
  mobile_app_user = sample(c(TRUE, FALSE), 500, replace = TRUE),
  
  credit_score = sample(300:850, 500, replace = TRUE),
  payment_method = sample(c('Credit', 'Debit', 'PayPal', 'Bank'), 500, replace = TRUE),
  
  stringsAsFactors = FALSE
) %>%
  mutate(
    total_annual_spend = monthly_purchases * avg_order_value * 12,
    customer_lifetime_value = total_annual_spend * years_customer,
    engagement_score = website_visits_month + (email_opens_month * 2) +
      ifelse(social_media_follower, 10, 0) +
      ifelse(newsletter_subscriber, 5, 0),
    
    churn_risk = case_when(
      years_customer <= 1 & monthly_purchases <= 2 ~ 'High',
      years_customer <= 3 & monthly_purchases <= 5 ~ 'Medium',
      TRUE ~ 'Low'
    )
  )

cat("FULL DATASET CREATED\n")
cat(nrow(customers_full), "rows ×", ncol(customers_full), "columns\n")

# -----------------------------
# Step 2: Correlation Analysis
# -----------------------------
numeric_vars <- customers_full %>%
  select_if(is.numeric) %>%
  select(-customer_id)

correlation_matrix <- cor(numeric_vars, use = "complete.obs")

spend_correlations <- correlation_matrix[, "total_annual_spend"]
spend_correlations <- sort(abs(spend_correlations[names(spend_correlations) != "total_annual_spend"]), 
                           decreasing = TRUE)

clv_correlations <- correlation_matrix[, "customer_lifetime_value"]
clv_correlations <- sort(abs(clv_correlations[names(clv_correlations) != "customer_lifetime_value"]), 
                         decreasing = TRUE)

engagement_correlations <- correlation_matrix[, "engagement_score"]
engagement_correlations <- sort(abs(engagement_correlations[names(engagement_correlations) != "engagement_score"]), 
                                decreasing = TRUE)

cat("\nTOP SPEND CORRELATIONS\n")
print(head(spend_correlations, 10))

cat("\nTOP CLV CORRELATIONS\n")
print(head(clv_correlations, 10))

cat("\nTOP ENGAGEMENT CORRELATIONS\n")
print(head(engagement_correlations, 10))

# -----------------------------
# Step 3: Business Feature Sets
# -----------------------------
acquisition_features <- customers_full %>%
  select(customer_id, age, income, education_years,
         region, urban_rural,
         years_customer, monthly_purchases, avg_order_value,
         website_visits_month, email_opens_month, engagement_score,
         total_annual_spend, customer_lifetime_value, churn_risk)

value_optimization_features <- customers_full %>%
  select(customer_id, monthly_purchases, avg_order_value, years_customer,
         website_visits_month, email_opens_month,
         social_media_follower, newsletter_subscriber, mobile_app_user,
         income, credit_score,
         total_annual_spend, customer_lifetime_value)

churn_prevention_features <- customers_full %>%
  select(customer_id, years_customer, monthly_purchases, support_tickets_year,
         website_visits_month, email_opens_month, engagement_score,
         social_media_follower, newsletter_subscriber,
         churn_risk)

cat("\nFEATURE SETS CREATED\n")

# -----------------------------
# Step 4: Variable Importance
# -----------------------------
calculate_variable_importance <- function(data, target_var) {
  numeric_cols <- data %>% select_if(is.numeric) %>% names()
  numeric_cols <- numeric_cols[!numeric_cols %in% c(target_var, "customer_id")]
  
  scores <- sapply(numeric_cols, function(col) {
    cor(data[[col]], data[[target_var]], use = "complete.obs")^2
  })
  
  sort(scores, decreasing = TRUE)
}

spend_importance <- calculate_variable_importance(customers_full, "total_annual_spend")
clv_importance <- calculate_variable_importance(customers_full, "customer_lifetime_value")
engagement_importance <- calculate_variable_importance(customers_full, "engagement_score")

cat("\nTOP SPEND IMPORTANCE\n")
print(head(spend_importance, 10))

cat("\nTOP CLV IMPORTANCE\n")
print(head(clv_importance, 10))

cat("\nTOP ENGAGEMENT IMPORTANCE\n")
print(head(engagement_importance, 10))

# -----------------------------
# Step 5: Feature Selection Workflow
# -----------------------------
feature_selection_workflow <- function(data, target_var, corr_threshold = 0.3, imp_threshold = 0.1) {
  
  numeric_data <- data %>% select_if(is.numeric)
  correlations <- cor(numeric_data, use = "complete.obs")[, target_var]
  correlations <- correlations[names(correlations) != target_var]
  
  corr_features <- names(abs(correlations) > corr_threshold)
  
  importance <- calculate_variable_importance(data, target_var)
  imp_features <- names(importance > imp_threshold)
  
  final_features <- unique(c(corr_features, imp_features))
  
  list(final_features = final_features)
}

spend_model <- feature_selection_workflow(customers_full, "total_annual_spend")
clv_model <- feature_selection_workflow(customers_full, "customer_lifetime_value")
eng_model <- feature_selection_workflow(customers_full, "engagement_score")

cat("\nFINAL FEATURE SETS\n")
print(spend_model$final_features)
print(clv_model$final_features)
print(eng_model$final_features)

# -----------------------------
# Exercise Dataset
# -----------------------------
set.seed(456)

techcorp_customers <- data.frame(
  customer_id = 1:300,
  age = sample(25:65, 300, replace = TRUE),
  income = round(rnorm(300, 65000, 25000)),
  job_level = sample(c('Entry', 'Mid', 'Senior', 'Executive'), 300, replace = TRUE),
  company_size = sample(c('Small', 'Medium', 'Large', 'Enterprise'), 300, replace = TRUE),
  industry = sample(c('Tech', 'Finance', 'Healthcare', 'Manufacturing', 'Other'), 300, replace = TRUE),
  products_used = sample(1:5, 300, replace = TRUE),
  usage_hours_week = sample(1:40, 300, replace = TRUE),
  feature_adoption_score = sample(1:100, 300, replace = TRUE),
  training_sessions = sample(0:20, 300, replace = TRUE),
  support_tickets = sample(0:15, 300, replace = TRUE),
  community_posts = sample(0:50, 300, replace = TRUE),
  contract_value = round(runif(300, 5000, 100000), 2),
  payment_terms = sample(c('Monthly', 'Quarterly', 'Annual'), 300, replace = TRUE),
  nps_score = sample(-100:100, 300, replace = TRUE),
  satisfaction_rating = sample(1:10, 300, replace = TRUE)
) %>%
  mutate(
    annual_revenue = contract_value,
    renewal_probability = pmax(0, pmin(1, (nps_score + 100) / 200 + runif(300, -0.2, 0.2))),
    expansion_potential = case_when(
      feature_adoption_score >= 80 & usage_hours_week >= 20 ~ 'High',
      feature_adoption_score >= 60 & usage_hours_week >= 10 ~ 'Medium',
      TRUE ~ 'Low'
    )
  )

cat("\nTECHCORP DATA READY\n")