---
title: "Implementation Lab: Linear Regression & Experimental Analysis"
subtitle: "Student Worksheet"
format:
  html:
    code-fold: show
    theme:
      light: 
        - journal
        - css/custom.scss # Change the default colour
    toc: true
execute:
  error: true
---

# Setup

```{r}
#| label: setup
#| message: false

# Load required packages
library(ggplot2)
library(dplyr)
library(broom)
library(effectsize)
library(car)
```

```{r}
#| label: load-data

# Load the datasets
marketing_data <- read.csv("marketing_data.csv")
firm_growth_data <- read.csv("firm_growth_data.csv")
leadership_study_between <- read.csv("leadership_study_between.csv")
communication_study <- read.csv("communication_study.csv")
```

---

# Part 1: Linear Regression

## 1.1 Simple vs. Multiple Regression

**Research Question:** How does ad spending affect sales revenue?

### Simple Regression

```{r}
#| label: simple-regression

# Fit simple regression: sales_revenue ~ ad_spend; use marketing_data
model_simple <- lm(___ ~ ___, data = ___)
summary(model_simple)
```

**Coefficient interpretation:**

For every EUR 1 increase in ad spending, there is an associated sales revenue increases by approximately _______ EUR on average.

### Multiple Regression

```{r}
#| label: multiple-regression

# Add website_traffic to the model
model_multiple <- lm(___ ~ ___ + ___, data = marketing_data)
summary(model_multiple)
```

### Compare Coefficients

```{r}
#| label: compare-coefficients

# Extract coefficient for ad_spend from both models
coef_simple <- coef(model_simple)["ad_spend"]
coef_multiple <- coef(model_multiple)["ad_spend"]

cat("Simple model:", round(coef_simple, 3), "\n")
cat("Multiple model:", round(coef_multiple, 3), "\n")
cat("Difference:", round(coef_simple - coef_multiple, 3), "\n")
```

**Notes on omitted variable bias:**

_Space for your notes:_

---

## 1.2 Model Diagnostics

```{r}
#| label: diagnostic-plots

# Extract fitted values and residuals
fitted_values <- fitted(model_multiple)
residuals_values <- residuals(model_multiple)

# Create diagnostic plots
library(gridExtra)

# Residuals vs Fitted
p1 <- ggplot(data.frame(fitted = fitted_values, resid = residuals_values),
             aes(x = fitted, y = resid)) +
  geom_point(alpha = 0.6) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
  geom_smooth(se = FALSE, color = "blue") +
  labs(title = "Residuals vs Fitted",
       x = "Fitted Values", 
       y = "Residuals") +
  theme_minimal()

# Q-Q Plot
p2 <- ggplot(data.frame(resid = residuals_values), aes(sample = resid)) +
  stat_qq() +
  stat_qq_line(color = "red") +
  labs(title = "Normal Q-Q Plot",
       x = "Theoretical Quantiles",
       y = "Sample Quantiles") +
  theme_minimal()

grid.arrange(p1, p2, ncol = 2)
```

**What to look for:**
- Residuals vs Fitted: _______________
- Q-Q Plot: _______________

---

## 1.3 Log Transformation

**Research Question:** How has company revenue grown over time?

### Visualize Raw Data

```{r}
#| label: visualize-exponential

# Create scatter plot with linear fit
ggplot(firm_growth_data, aes(x = year, y = revenue)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  labs(title = "Revenue Growth Over Time",
       x = "Year",
       y = "Revenue (EUR)") +
  theme_minimal()
```

**Observation:**

_Does the linear fit look appropriate?_

### Apply Log Transformation

```{r}
#| label: log-transform

# Create log-transformed variable
firm_growth_data <- firm_growth_data %>%
  mutate(log_revenue = ____(revenue))

# Fit models
model_linear <- lm(revenue ~ year, data = firm_growth_data)
model_log <- lm(log_revenue ~ year, data = firm_growth_data)

# Compare R²
summary(model_linear)$r.squared
summary(model_log)$r.squared
```

### Visualize Log-Transformed Data

```{r}
#| label: visualize-log

# Plot log(revenue) vs year
ggplot(firm_growth_data, aes(x = year, y = log_revenue)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  labs(title = "Log(Revenue) vs Year",
       x = "Year",
       y = "Log(Revenue)") +
  theme_minimal()
```

### Interpret Coefficients

```{r}
#| label: interpret-log

# Get coefficient
coef_log <- coef(model_log)["year"]
percentage_change <- (exp(coef_log) - 1) * 100

cat("Annual growth rate:", round(percentage_change, 2), "%\n")
```

**Interpretation:**

When the dependent variable is log-transformed, coefficients represent _____________.

---

# Part 2: Experimental Analysis

## 2.1 t-Test

**Research Question:** Does leadership training improve team performance?

### Visualize the Data

```{r}
#| label: visualize-ttest

ggplot(leadership_study_between, aes(x = group, y = team_performance, fill = group)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.2, alpha = 0.5) +
  stat_summary(fun = mean, geom = "point", shape = 23, size = 3, fill = "red") +
  labs(title = "Team Performance by Group",
       x = "Group",
       y = "Team Performance Score") +
  theme_minimal() +
  theme(legend.position = "none")
```

### Check Assumptions

```{r}
#| label: check-assumptions-ttest

# Normality test for each group: the Shapiro test
____(filter(leadership_study_between, group == "control")$team_performance)
____(filter(leadership_study_between, group == "training")$team_performance)

# Equal variances test: Levene test
____(team_performance ~ group, data = leadership_study_between)
```

**Interpretation:**
- p-values > 0.05? If yes, assumptions are met: _______

### Run t-test

```{r}
#| label: run-ttest

t_result <- t.test(
  team_performance ~ group,
  data = leadership_study_between,
  var.equal = TRUE
)

print(t_result)
```

**Results:**
- Mean difference: _______
- p-value: _______
- Conclusion: _______

### Calculate Effect Size

```{r}
#| label: cohens-d-ttest

cohens_d_result <- ____(team_performance ~ group, 
                            data = leadership_study_between)
print(cohens_d_result)
```

**Effect size interpretation:**
- Cohen's d = _______ 
- This is a _______ effect (small/medium/large)

---

## 2.2 One-Way ANOVA

**Research Question:** Which communication method leads to highest satisfaction?

### Visualize the Data

```{r}
#| label: visualize-anova

ggplot(communication_study, aes(x = communication_method, 
                                 y = satisfaction_score, 
                                 fill = communication_method)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.2, alpha = 0.3) +
  stat_summary(fun = mean, geom = "point", shape = 23, size = 4, fill = "red") +
  labs(title = "Satisfaction Score by Communication Method",
       x = "Communication Method",
       y = "Satisfaction Score") +
  theme_minimal() +
  theme(legend.position = "none")
```

### Fit ANOVA

```{r}
#| label: fit-anova

# Using aov()
anova_model <- ____(satisfaction_score ~ communication_method, 
                   data = communication_study)
summary(anova_model)
```

**Results:**
- F-statistic: _______
- p-value: _______
- Conclusion: _______

### ANOVA as Regression

```{r}
#| label: anova-regression

# Show that lm() gives same results
lm_model <- lm(satisfaction_score ~ communication_method, 
               data = communication_study)
anova(lm_model)
```

```{r}
#| label: coefficients

# Look at coefficients
summary(lm_model)$coefficients
```

**Notes:**
- Reference group: _______
- Coefficients show: _______

### Effect Size

```{r}
#| label: eta-squared

eta_sq <- eta_squared(anova_model)
print(eta_sq)
```

**Interpretation:**

Communication method explains _______ % of variance in satisfaction.

### Post-Hoc Tests

```{r}
#| label: tukey

# Which groups differ?
tukey_result <- TukeyHSD(anova_model)
print(tukey_result)
```

**Significant differences:**
- _______
- _______

**No significant difference:**
- _______

---

# Part 3: Guided Exercise

## Dataset: Website A/B Test

**Variables:**
- `design`: Simple vs. Complex
- `time_on_site`: Time spent (seconds)
- `previous_visits`: Number of previous visits
- `converted`: Purchase made (1 = yes, 0 = no)

```{r}
#| label: load-exercise-data

exercise_data <- read.csv("exercise_data.csv")
head(exercise_data)
```

## Task 1: Regression Analysis

### a. Scatter plot

```{r}
#| label: task1a

# Create scatter plot of time_on_site vs converted


```

### b. Fit simple regression

```{r}
#| label: task1b

# Fit: converted ~ time_on_site


```

### c. Interpret coefficient

**Interpretation:**


### d. Report R²

```{r}
#| label: task1d

# Calculate R²


```

---

## Task 2: Compare Designs

### a. t-test

```{r}
#| label: task2a

# Compare time_on_site between designs


```

### b. Effect size

```{r}
#| label: task2b

# Calculate Cohen's d


```

### c. Conclusion

**Which design keeps users longer?**


---

## Task 3: Omitted Variable Bias (Challenge)

### a. Add previous_visits

```{r}
#| label: task3a

# Fit: converted ~ time_on_site + previous_visits


```

### b. Compare coefficients

```{r}
#| label: task3b

# Extract and compare coefficients for time_on_site


```

### c. Interpret

**Is there omitted variable bias? Why or why not?**


---

# Key Takeaways

Write down 3 key things you learned today:

1. 

2. 

3. 

---

# Questions for Office Hours

Write down any questions or concepts you'd like to discuss further:

