Project: s41467-025-60538-2 | Function: cross_cultural_difference (r)

system: You are an expert R programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow R best practices and maintain consistent code style.
Return **only** the complete R function code.

user: Complete this R function:

**DESCRIPTION:** Statistical analysis of cross-cultural differences in diversity measures. Implements comparative analysis methodology for cultural variation assessment.

**KEY TERMS:** {
  "cross_cultural": "between-culture comparisons",
  "difference": "cultural variation measures"
}

**CONTEXT CODE:**   mutate(value = value * 100) %>%
  group_by(name) %>%
  summarise(m = mean(value),
            lower_ci = quantile(value, 0.025),
            upper_ci = quantile(value, 0.975))

print(census_diff_agg)

census_diff_cohen <- census_join %>%
  filter(!is.na(region_category)) %>%
  select(boot, region_category, bid = song_div_q1, wid = ind_div) %>%
  pivot_wider(names_from = region_category, values_from = c(bid, wid))
census_diff_cohen <- census_diff_cohen %>% 
  group_by(boot) %>%
  summarise(bid = get_t_stat(bid_rural %>% unlist(), bid_urban %>% unlist()),
            wid = get_t_stat(wid_rural %>% unlist(), wid_urban %>% unlist())
  ) 

census_diff_cohen$bid$cohensD %>% mean()
census_diff_cohen$bid$cohensD %>% quantile(c(0.025, 0.975))

census_diff_cohen$wid$cohensD %>% mean()
census_diff_cohen$wid$cohensD %>% quantile(c(0.025, 0.975))

# STATS: report correlations for different orders of q in SI
agg_q_mean <- joined_div %>%
  group_by(country) %>%
  summarise(get_boot_mean_ci(song_div_q0, 'song_div_q0'),
            get_boot_mean_ci(song_div_q1, 'song_div_q1'),
            get_boot_mean_ci(song_div_q2, 'song_div_q2'),
            get_boot_mean_ci(song_gini, 'song_gini'))

agg_q_order <- joined_div %>%
  group_by(country, regional_group) %>%
  summarise(q0 = mean(song_div_q0),
            q1 = mean(song_div_q1),
            q2 = mean(song_div_q2),
            gini = mean(song_gini)) %>%
  ungroup() %>%
  left_join(joined_pop) %>%
  select(country, pop_size = user_pop_size_log10, q0, q1, q2, gini)

agg_q_order %>% filter(country == 'France') %>% correlation::correlation()
agg_q_order %>% filter(country == 'Brazil') %>% correlation::correlation()
agg_q_order %>% filter(country == 'Germany') %>% correlation::correlation()


################################################################################
## CROSS-CULTURAL DIFFERENCES
################################################################################
cross_cultural_difference <- function(plot_type = c('WID', 'BID')) {
  stop("Function implementation has been masked")
}

# Run the function for demonstration
cross_cultural_difference('WID')
cross_cultural_difference('BID')

cross_cultural_difference('WID')
cross_cultural_difference('BID')


## ANOVA and post hoc t-tests for the cross-cultural differences
plot_type <- 'BID'
agg_sel <- agg_stat %>%
  filter(metric == plot_type) %>%
  select(country, boot_m)

agg_sel %>%
  group_by(country) %>%
  reframe(get_t_independent(boot_m))

# perform anova to see group level differences
aov_model <- aov(boot_m ~ country, data = agg_sel)
aov_model %>% summary()

# post hoc pair-wise and effect size
TukeyHSD(aov_model)

agg_sel %>%
  pivot_wider(names_from = country, values_from = boot_m) %>%
  reframe(get_t_stat(Germany %>% unlist(), France %>% unlist()))

################################################################################
## DIFFERENT TYPES OF BID: replicating for genres and artists
################################################################################
# split the population group by small vs. large
joined_pop_split <- joined_pop %>%
  group_by(country) %>%
  mutate(region_size = ntile(n_users, 3),
         region_size = case_match(region_size,
                    3 ~ 'L',
                    2 ~ 'M',
                    1 ~ 'S') %>% factor(levels = c('S', 'M', 'L'))) 

pop_split_agg <- joined_div %>%
  left_join(joined_pop_split) %>%
  group_by(country) %>%
  mutate(across(c(song_div_q1, artist_div_q1, genre_div_q1), scale)) %>%
  pivot_longer(c(song_div_q1, artist_div_q1, genre_div_q1)) %>%
  group_by(country, region_size, name) %>%
  reframe(get_boot_mean_ci(value, 'boot')) %>%
  ungroup() %>%
  mutate(name = case_match(name,
    'song_div_q1' ~ 'Song',
    'artist_div_q1' ~ 'Artist',
    'genre_div_q1' ~ 'Genre',
    'song_age' ~ 'Song Age'
  )) %>%
  mutate(name = factor(name, levels = c('Song', 'Artist', 'Genre', 'Song Age')),
         country = factor(country, levels = c('France', 'Brazil', 'Germany')))

(BID_alternatives <- pop_split_agg %>%
  ggplot(aes(region_size, boot_m, colour = country, group = country)) +
  geom_point(position = position_dodge(0.3)) +
  geom_line(position = position_dodge(0.3), alpha = 0.5) +
  scale_color_manual(values = c(FR_COL1, BR_COL1, DE_COL1), guide = F) +
  labs(x = 'Population size', y = 'Standardised BID') +
  facet_wrap(~ name))

plot_save(BID_alternatives, plot_name = 'main/BID_aritst_genre', size = c(70, 50))

################################################################################
## WID VS. BID
################################################################################
# relations between WID and BID for SI figure
WID_BID <- agg_stat %>%
  select(country, regional_group, metric, boot_m) %>%
  pivot_wider(names_from = metric, values_from = boot_m) %>%
  left_join(joined_pop) %>%
  group_by(country) %>%
  mutate(WID = scale(WID),
         BID = scale(BID),
         pop_size = normalize_to_one(user_pop_size_log10)) %>% 
  ungroup() %>%
  mutate(country = factor(country, levels = c('France', 'Brazil', 'Germany')))

(WID_BID_p <- WID_BID %>%
  ggplot(aes(WID, BID, colour = country)) +
  geom_point(aes(size = pop_size), alpha = 0.8) +
  ggpubr::stat_cor(r.accuracy = 0.01, p.accuracy = 0.001, cor.coef.name = 'r') +
  geom_vline(xintercept = 0, linetype = 'dashed') +
  geom_hline(yintercept = 0, linetype = 'dashed') +
  scale_size(guide = 'none') +
  scale_colour_manual(values = c(FR_COL1, BR_COL1, DE_COL1)) +
  labs(x = 'WID', y = 'BID', colour = 'Country') +
  theme(legend.position = 'top'))

plot_save(WID_BID_p, plot_name = 'SI/WID_w_BID',  size = c(150, 150))



Complete the function implementation replacing "stop("Not implemented")".