---
title: "Aactivity_Task_Week_6"
author: "Mehul Prafulbhai Tank"
output: pdf_document
---

\newpage
```{r}

library(gapminder)
library(dplyr)

# Extract life expectancy data
life_data <- gapminder$lifeExp

# Generate 1000 sample means
sample_means <- replicate(1000, mean(sample(life_data, size = 50, replace = TRUE)))

# Calculate standard error from the sampling distribution
se <- sd(sample_means)

# Create histogram and store histogram object
hist_obj <- hist(sample_means, 
                 main = "Sampling Distribution of Sample Mean", 
                 xlab = "Mean Life Expectancy", 
                 )

```

```{r}
# Try with n = 10 and n = 100
means_10 <- replicate(1000, mean(sample(life_data, 10, replace = TRUE)))
hist(means_10, main="n = 10")
```

```{r}

means_100 <- replicate(1000, mean(sample(life_data, 100, replace = TRUE)))


hist(means_100, main="n = 100")
```

```{r}
simulate_means <- function(n, reps = 1000) {
  replicate(reps, mean(sample(life_data, size = n, replace = TRUE)))
}

sample_sizes <- c(20, 40, 60, 120)
par(mfrow = c(2,2))
for (n in sample_sizes) {
  hist(simulate_means(n), main = paste("n =", n))}

# Extract life expectancy data
life_data <- gapminder$lifeExp

# Function to simulate sample means
simulate_means <- function(n, reps = 1000) {
  replicate(reps, mean(sample(life_data, size = n, replace = TRUE)))
}

# Define sample sizes
sample_sizes <- c(20, 40, 60, 120)

# Set plotting layout: 2 rows, 2 columns
par(mfrow = c(2, 2))

# Loop through each sample size and plot histograms with SE
for (n in sample_sizes) {
  sample_means <- simulate_means(n)
  se <- sd(sample_means)  # Calculate standard error
  
  # Create histogram and store object to get y-axis height
  hist_obj <- hist(sample_means, 
                   main = paste("n =", n,"\nSE =", round(se, 2)), 
                   xlab = "Sample Mean", 
                   )
}

```

```{r}
library(gapminder)
library(ggplot2)
head(gapminder) 


summary(gapminder)  
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) + geom_point() 

```

```{r}
# R Code
cor(gapminder$lifeExp, gapminder$gdpPercap, method = "pearson")
 
# Visualise
library(ggpubr) 
ggscatter(gapminder, x = "gdpPercap", y = "lifeExp", add = "reg.line") 

```

```{r}
# R Code for correlation
cor(gapminder$lifeExp, gapminder$gdpPercap, method = "spearman") 

```

```{r}
# Extract variables
life_exp <- gapminder$lifeExp
population <- gapminder$pop

# Pearson correlation
cor_pearson <- cor(life_exp, population, method = "pearson")

# Spearman correlation
cor_spearman <- cor(life_exp, population, method = "spearman")

# Display results
cat("Pearson correlation:", round(cor_pearson, 3), "\n")
cat("Spearman correlation:", round(cor_spearman, 3), "\n")

```
# Spearman correlation is better for analyzing the relationship between lifeExp (life expectancy) and pop (population) in the gapminder dataset.

