set.seed(456)
                    sales_analysis <- tibble(
                      transaction_id = 1:1000,
                      customer_id = sample(1:200, 1000, replace = TRUE),
                      product_category = sample(c("電子機器", "衣料品", "食品", "書籍", "スポーツ用品"), 
                                                    1000, replace = TRUE),
                      purchase_date = sample(seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by = "day"), 
                                              1000, replace = TRUE),
                      amount = round(rlnorm(1000, log(5000), 1)),
                      payment_method = sample(c("クレジット", "現金", "電子マネー"), 
                                                1000, replace = TRUE, prob = c(0.6, 0.2, 0.2)),
                      store_location = sample(c("東京", "大阪", "名古屋", "福岡"), 
                                                1000, replace = TRUE)
                    ) %>%
                      mutate(
                        month = lubridate::month(purchase_date),
                        quarter = lubridate::quarter(purchase_date),
                        day_of_week = lubridate::wday(purchase_date, label = TRUE),
                        amount_category = case_when(
                          amount < 2000 ~ "小額",
                          amount < 10000 ~ "中額",
                          TRUE ~ "高額"
                        )
                      )
                    
                    print("売上データサンプル:")
                    print(head(sales_analysis))
                    
                    
                    customer_patterns <- sales_analysis %>%
                      group_by(customer_id) %>%
                      summarise(
                        total_purchases = n(),
                        total_amount = sum(amount),
                        avg_amount = mean(amount),
                        favorite_category = names(sort(table(product_category), decreasing = TRUE))[1],
                        preferred_payment = names(sort(table(payment_method), decreasing = TRUE))[1],
                        first_purchase = min(purchase_date),
                        last_purchase = max(purchase_date),
                        purchase_span_days = as.numeric(max(purchase_date) - min(purchase_date)),
                        .groups = 'drop'
                      ) %>%
                      mutate(
                        customer_segment = case_when(
                          total_amount > 50000 && total_purchases > 10 ~ "VIP",
                          total_amount > 20000 || total_purchases > 5 ~ "優良",
                          TRUE ~ "一般"
                        )
                      )
                    
                    print("顧客セグメント分析:")
                    print(customer_patterns %>% count(customer_segment))
                    
                    
                    regional_monthly <- sales_analysis %>%
                      group_by(store_location, month) %>%
                      summarise(
                        transactions = n(),
                        total_sales = sum(amount),
                        avg_transaction = mean(amount),
                        unique_customers = n_distinct(customer_id),
                        .groups = 'drop'
                      ) %>%
                      arrange(store_location, month)
                    
                    print("地域別月次売上(上位10行):")
                    print(head(regional_monthly, 10))
                    
                    
                    category_payment_analysis <- sales_analysis %>%
                      group_by(product_category, payment_method) %>%
                      summarise(
                        count = n(),
                        avg_amount = mean(amount),
                        .groups = 'drop'
                      ) %>%
                      group_by(product_category) %>%
                      mutate(
                        percentage = round(count / sum(count) * 100, 1)
                      ) %>%
                      arrange(product_category, desc(percentage))
                    
                    print("カテゴリ別支払い方法傾向:")
                    print(category_payment_analysis)