---
title: "Activity"
author: "Nisarg Jani"
date: "2025-10-21"
output: html_document
---



```{r}
# 1. Load libraries
library(gapminder)
library(dplyr)
library(ggplot2)
library(car)


# 2. Load the Gapminder dataset
data("gapminder")

# Inspect data
head(gapminder)

# 3. Conduct ANOVA on GDP per capita by continent
anova_model <- aov(gdpPercap ~ continent, data = gapminder)
summary(anova_model)

# 4. Check assumptions
## Normality of residuals
plot(anova_model, which = 2)  # Q-Q plot
shapiro.test(residuals(anova_model))  # Shapiro-Wilk test

## Homogeneity of variances
leveneTest(gdpPercap ~ continent, data = gapminder)

# 5. Tukey post-hoc test
tukey_result <- TukeyHSD(anova_model)
tukey_result

# 6. Visualise the outcome
## Boxplot of GDP per capita by continent
ggplot(gapminder, aes(x = continent, y = gdpPercap, fill = continent)) +
  geom_boxplot() +
  scale_y_log10() +  # log scale improves readability
  theme_minimal() +
  labs(
    title = "GDP per Capita by Continent (Gapminder)",
    y = "GDP per Capita (log scale)",
    x = "Continent"
  )

## Plot Tukey results
plot(tukey_result, las = 1)


```

