---
session: "Session 4"
title: "Machine Learning Baselines: Regression"
session_id: session-04
format:
  html: default
---


In this exercise, we cover:

| Exercise part | Time (min) |
|---|---:|
| [Baseline regression](#baseline) | 18 |
| [Regression inference and model summary](#inference) | 17 |
| [Regression diagnostics](#diagnostics) | 22 |
| [Subgroup analysis](#subgroup-analysis) | 18 |
| [Deployment](#deployment) | 10 |
| [Wrap-up](#wrap-up) | 5 |
| **Overall** | **90** |

Work directly in this Quarto (`.qmd`) exercise using R in RStudio, Positron, or VS Code.

# Case: Employee performance at MetaCorp

You are a business analyst working for MetaCorp.
The company wants to understand which factors are associated with employee performance.
A central question in this exercise is whether **remote work** is associated with employee performance once we also account for other relevant variables.

MetaCorp is interested in questions such as:

- Does remote work seem to be associated with higher or lower performance?

**Your answer:** *Write your answer here.*

- Does this relationship remain once we control for experience, training, salary, and management quality?

**Your answer:** *Write your answer here.*

- Is the relationship between remote work and performance similar across departments?

**Your answer:** *Write your answer here.*

- When should we be careful not to overinterpret regression results?

**Your answer:** *Write your answer here.*

The target variable is `performance`, a quantitative employee performance score.

Potential predictors include:

- `experience`: years of work experience
- `training_hours`: annual training hours
- `salary`: annual salary in EUR
- `remote_share`: share of working time spent remotely (between 0 and 1)
- `team_size`: size of the employee's team
- `manager_quality`: manager quality on a 1–5 scale
- `department`: department (`Sales`, `IT`, `HR`)

## Package setup

Before starting this exercise, make sure the required R packages are installed.

Run this command once in the R console. You do not need to run it every time you open the exercise. You can skip this step if these packages are already installed.

```{r}
#| eval: false
install.packages("tidyverse")
```


# Setup and dataset

## Load and inspect the dataset

```{r}
library(tidyverse)

df <- read_csv(
  "https://raw.githubusercontent.com/fs-ise/machine-learning-for-big-data/main/exercises/data/employee_performance_data.csv",
  show_col_types = FALSE
) |>
  mutate(
    department = factor(
      department,
      levels = c("HR", "IT", "Sales")
    )
  )

glimpse(df)
summary(df)
```

Setting the factor levels explicitly makes **HR the reference category**. R's formula interface will automatically represent the other departments with contrasts; we do not need to create indicator columns manually.

## Variable overview

| Variable | Meaning | Type |
|---|---|---|
| `performance` | employee performance score | target variable |
| `experience` | years of experience | numeric |
| `training_hours` | annual training hours | numeric |
| `salary` | annual salary in EUR | numeric |
| `remote_share` | share of remote work between 0 and 1 | numeric |
| `team_size` | size of employee team | numeric |
| `manager_quality` | manager quality rating from 1 to 5 | numeric |
| `department` | employee department | categorical |

# Part A — Baseline regression {#baseline}

We begin with one regression model for the full company. This gives us a first answer to the question:

> How is `remote_share` associated with performance when we control for other variables at the same time?

This is useful because simple bivariate comparisons can be misleading. For example, employees with higher remote work shares may also differ in their department, training patterns, or salaries.

## Task A1: Fit a regression model

Use the full dataset to estimate a linear regression model for `performance`.

Your tasks:

1. Specify the regression formula with `performance` as the dependent variable.
2. Include `experience`, `training_hours`, `salary`, `remote_share`, `team_size`, `manager_quality`, and `department` as predictors.
3. Fit the model with `lm()` and store it as `model`.
4. Inspect its coefficients with `coef(model)`.
5. Identify the intercept and pay special attention to `remote_share`.
6. Identify how R represented the `department` factor.

```{r}
# YOUR CODE HERE
```

The identifying fields `name`, `employee_id`, and `role` simply do not appear in the formula. Because `department` is a factor, the formula interface handles its contrasts automatically.


## Task A2: Interpret the coefficients

Answer the following questions.

1. Which variables have a positive association with performance?


2. Which variable has a negative association with performance?


3. How would you interpret the coefficient of `remote_share`?


4. Is the `remote_share` association large or small in practical terms?


5. How should `departmentIT` and `departmentSales` be interpreted?



::: {.callout-note title="Interpretation reminder"}
A coefficient expresses the expected change in the dependent variable when the predictor increases by one unit, **holding the other variables constant**.
:::

::: {.callout-important title="Important note on remote_share"}
Because `remote_share` ranges from 0 to 1, a one-unit increase means moving from **fully on-site** to **fully remote**. In practice, smaller changes such as 0.10, or 10 percentage points, are often easier to interpret.
:::


# Part B — Regression inference and model summary {#inference}

Base R's `lm()` provides estimation, prediction, and statistical inference in the same fitted model. We can examine coefficient uncertainty and overall model fit without refitting the model through another library.

## Task B1: Examine the fitted model

Use the same `model` from Part A.

1. Display its regression summary with `summary(model)`.
2. Display coefficient confidence intervals with `confint(model)`.
3. Retrieve only the confidence interval for `remote_share`.

```{r}
# YOUR CODE HERE
```

In the summary, locate the coefficient estimates, standard errors, t-statistics, p-values, residual standard error, $R^2$, adjusted $R^2$, and overall F-test.


## Task B2: Interpret inference and model fit

Use the model output to answer the following questions.

1. Which predictors are statistically significant at the 5% level?


2. What do the p-value and confidence interval say about `remote_share`?


3. What do $R^2$ and adjusted $R^2$ tell you?


4. How do statistical significance and practical relevance differ here?


5. Why shuld we avoid a causal interpretation?


6. What does the formula interface (`lm(...)`) allow us to express directly, and how does R handle `department`?


# Part C — Regression diagnostics {#diagnostics}

Regression coefficients are only part of the story. Before relying on model predictions, assess whether the model reasonably represents the data.

A key linear-regression assumption is **linearity**. Residuals capture what the fitted model does not explain. If the model is appropriate, they should look like random noise around zero. Curvature or other systematic patterns can indicate missing structure, while a funnel shape can indicate non-constant variance.

Even if `remote_share` has a positive or negative coefficient, that does **not** mean that the model is perfectly specified or that the relationship is causal.

## Task C1: Residual plot

Create a data frame containing fitted values and residuals from `model`, then create a residual plot with ggplot2.

```{r}
# YOUR CODE HERE
```


Questions:

1. Do the residuals look roughly random?


2. Do you see systematic patterns that may indicate nonlinearity or other model problems?


3. What would a clear funnel shape suggest?


# Part D — Subgroup analysis: does one model fit all departments? {#subgroup-analysis}

Sometimes a single regression for the whole company may hide important differences. Internal feedback at MetaCorp suggests that complaints about remote work are more frequent in some departments than in others. Coordination-intensive work may face communication challenges, while other work may benefit from concentration and flexibility.

Separate department models provide an intuitive first exploration of whether relationships look similar across departments, whether some variables matter more in certain contexts, and whether the association with `remote_share` changes with the work environment.

## Task D1: Department-specific regressions

Estimate separate regressions for Sales, IT, and HR. Omit `department` from each formula because each subset contains only one department.


```{r}
departments <- unique(df$department)
results <- list()

# YOUR CODE HERE: add a loop. For each department:
# 1. filter df;
# 2. fit the subgroup model with lm();
# 3. store it in results;
# 4. print summary(model_sub).
```


## Task D2: Compare the subgroup models

1. Do the coefficients differ across departments?


2. Is `remote_share` associated with performance in the same way in every department?


3. Does `manager_quality` seem equally important in all departments?


4. What does this tell us about heterogeneity in company data?


5. Why might a single pooled model be too simplistic?


::: {.callout-note title="From subgroups to interactions"}
Separate regressions are intuitive for exploration. Differences can also be represented in one model with interaction terms, for example:

```{r}
#| eval: false
performance ~
  experience +
  training_hours +
  salary +
  remote_share * department +
  team_size +
  manager_quality
```

The interaction `remote_share * department` allows the `remote_share` slope to differ by `department` while estimating one combined model.
:::

# Part E — Deployment {#deployment}

Saving and loading a model is useful in practice, but it is not the main conceptual focus of this exercise. It is included as an optional extension after estimation, interpretation, diagnostics, and subgroup analysis.

## Task E1 (optional): Save a fitted model


```{r}
# YOUR CODE HERE: save the model to employee_performance_model.rds.
```


## Task E2 (optional): Load the model and predict a new case

Load the fitted model, create a tibble for an IT employee with the original predictors, and call `predict()`.

```{r}
# YOUR CODE HERE
```

> **Important:** The fitted `lm()` object retains its formula and factor contrasts. You do not need to construct department indicator variables manually. Production use still requires validation that incoming values, units, factor levels, and other preparation rules match the training data.


## Task E3: Reflect on practical and organizational considerations

In practice, deploying a model like this involves more than saving and loading it. Think about how such a model would be used in a real company like MetaCorp.

**Questions:**

1. What practical challenges might arise when using this model to support decisions about employees?


2. What risks could emerge if the model is used without careful interpretation or oversight?


3. What safeguards are important for fair, transparent, and responsible use, including legal and organizational requirements?



## Wrap-up {#wrap-up}

🎉🎈 You have completed the exercise—good work! 🎈🎉

In this exercise, we have learned to

- Fit and interpret linear regression models in R
- Use regression summaries for statistical inference
- Interpret coefficients, confidence intervals, and statistical significance
- Diagnose regression models using residual plots
- Compare relationships across subgroups
- Save, load, and apply fitted models for prediction
- Reflect on responsible organizational use of predictive models



