# Chi-square test is a statistical test used to # examine the relationship between two # categorical variables. # Checks if two categorical variables are related. #--------------------------------------------- # Chi-square test using Village Weaver data #--------------------------------------------- library(ggplot2) library(dplyr) library(lubridate) library(tidyr) # Example: Is bird sex related to sampling month? weaver<-read.csv('villageweaver.csv') str(weaver) # Identify associations between categorical variables (e.g., sex and habitat type). # If your Chi-square test shows a significant # result between sex and month, it suggests: # “The proportion of male and female Village # Weavers captured differs across months # perhaps due to breeding behaviour or # migration patterns.” # If not significant: # “The sex ratio remains consistent across # months, suggesting no strong seasonal effect.” #--------------------------------------------- # 1. Sex vs Month #--------------------------------------------- # Create a contingency table table_month <- table(weaver$sex, weaver$month) table_month # Run Chi-square test chisq_month <- chisq.test(table_month) chisq_month # Interpretation # The p-value tells us if sex distribution differs across months. # If p < 0.05, there is a significant relationship between sex and month. #--------------------------------------------- # 2. Sex vs Year #--------------------------------------------- # Create contingency table table_year <- table(weaver$sex, weaver$year) table_year # Run Chi-square test chisq_year <- chisq.test(table_year) chisq_year #--------------------------------------------- # 3. Visualizations using ggplot2 #--------------------------------------------- # (a) Bar chart showing sex distribution across months ggplot(weaver, aes(x = month, fill = sex)) + geom_bar(position = "dodge") + labs(x = "Month", y = "Number of village weaver ringed") + theme_classic() # To make month in a calender order # Convert to an ordered factor using lubridate::month() weaver <- weaver %>% mutate(month = month(parse_date_time(month, orders = "b"), label = TRUE, abbr = FALSE)) # Plot the bar chart ggplot(weaver, aes(x = month, fill = sex)) + geom_bar(position = "dodge") + labs(x = "Month", y = "Number of village weavers ringed") + theme_classic() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) # (b) Bar chart showing sex distribution across years ggplot(weaver, aes(x = factor(year), fill = sex)) + geom_bar(position = "dodge") + labs(x = "Year", y = "Number of village weaver ringed") + theme_classic() # To show space for make in year 2004 # Create a summary of counts weaver_summary <- weaver %>% group_by(year, sex) %>% summarise(count = n(), .groups = "drop") # Ensure all year-sex combinations exist weaver_complete <- weaver_summary %>% complete(year, sex, fill = list(count = 0)) # Plot the bar chart ggplot(weaver_complete, aes(x = factor(year), y = count, fill = sex)) + geom_bar(stat = "identity", position = "dodge") + labs(x = "Year", y = "Number of village weavers ringed") + theme_classic()