# Using ChickWeight Dataset # Imagine you are monitoring the growth of young birds #(chicks) fed four different diets over several weeks. # Each chick is weighed repeatedly to see how diet # affects growth rate over time. # The dataset we will use, ChickWeight, # mimics this kind of ecological experiment. # Step 1: Load and Explore the Dataset # Load the built-in dataset ChickWeight # To “Show me the help file (documentation) for the # dataset called ChickWeight. ?ChickWeight # It contains data from an experiment on chicks fed # with different diets and their weight measured # over time. head(ChickWeight) str(ChickWeight) # Step 2: Visualise Growth Trends # We’ll plot each chick’s weight across time, # coloured by diet. library(ggplot2) ggplot(ChickWeight, aes(x = Time, y = weight, group = Chick, color = Diet)) + geom_line(alpha = 0.5) + geom_point(size = 2) + labs(x = "Time (days)", y = "Weight (g)") + theme_classic() # What patterns do you observe in the growth curves # across diets? # Are some diets producing faster growth than others? # NOTE: Let's assume NORMAL DISTRIBUTION here. # Step 3: Repeated Measures ANOVA # We will now test: # (i) Whether chick weight changes over time, and # (ii) Whether diet affects growth patterns. # Run repeated measures ANOVA aov_result <- aov(weight ~ Time * Diet, data = ChickWeight) # View the results summary(aov_result) # Is the effect of Time significant? # Is the effect of Diet significant? # Is there a significant Time × Diet interaction? # Use the p-values to support your answer. # Step 4: Post Hoc Test (Pairwise Comparisons) # If the ANOVA shows significant differences # between diets, we can test which diets differ # from each other. # For ANOVA # If ANOVA test is significant, perform pairwise comparisons: TukeyHSD(aov_result, "Diet") # For Kruskal–Wallis # If Kruskal–Wallis is significant, perform pairwise comparisons: pairwise.wilcox.test(ChickWeight$weight, ChickWeight$Diet) # Interpretation: # Compare p-values: # If p < 0.05, the two diets differ significantly # in chick weight. # If p > 0.05, there’s no significant difference. # Example: # Diet 1 vs Diet 3 (p = 0.00048) → significant difference. # Diet 2 vs Diet 3 (p = 0.29) → no difference. # Step 5: Plot Mean Growth Curves # To visualise average growth by diet: library(dplyr) # Summarise mean and standard error for each diet and time summary_data <- ChickWeight %>% group_by(Diet, Time) %>% summarise(mean_weight = mean(weight), se = sd(weight)/sqrt(n()), .groups = "drop") # Plot mean ± SE ggplot(summary_data, aes(x = Time, y = mean_weight, color = Diet, group = Diet)) + geom_line(size = 1.2) + geom_point(size = 3) + geom_errorbar(aes(ymin = mean_weight - se, ymax = mean_weight + se), width = 0.5) + labs(x = "Time (days)", y = "Mean Weight (g)") + theme_classic() # How does this mean plot confirm or # contradict the earlier ANOVA results? # Describe the growth pattern of chicks under # each diet.