---
title: "Lab 6"
subtitle: "36-315: Statistical Graphics and Visualization, Summer 2026"
format:
  pdf:
    colorlinks: true
    toc: true
    fontsize: 10pt
    geometry: margin=0.9in
execute:
  warning: false
  message: false
---

\newpage

# Problem 1: Scatterplots (50 points)

We will again use the food dataset from the previous lab:

```{r}
library(tidyverse)
theme_set(theme_light())
food <- read_csv("https://raw.githubusercontent.com/qntkhvn/36-315-summer26/refs/heads/master/data/food.csv")
```

## A (10pts) 

Create a scatterplot with `fat_100g` on the x-axis and `energy_100g` on the y-axis. Make sure to change `alpha` to add transparency to individual points.

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

Describe the joint distribution of these two variables in 1--3 sentences. What are the interesting features? Do the two variables appear to be associated or correlated with each other? Does there appear to be any group structure in the data?

**YOUR ANSWER HERE**

## B (5pts)  

Repeat Part A, but color the points by the food group (`pnns_groups_1`).

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

Are there particular areas of the graph where certain food groups are common or uncommon? Interpret in the context of the problem.

**YOUR ANSWER HERE**

## C (5pts) 

Repeat Part B, but facet on the food group instead of coloring the points. Also, move the legend the bottom of the graph by adding `theme(legend.position = "bottom")`.

```{r}
#| fig-height: 5
#| fig-width: 8
# YOUR CODE HERE
```

Is it easier to see food group-specific trends between fat and energy with faceting or with the colors? Is this true in general, or does it depend on the problem? 

**YOUR ANSWER HERE**

## D (10pts) 

Repeat Part A, but add a linear regression line to the plot, with no confidence band.

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

Does the line appear to provide a good fit to the variables being plotted here? Why or why not?

**YOUR ANSWER HERE**

## E (10pts) 

Repeat Part A, but add a nonlinear regression line to the plot using `geom_smooth(method = "loess", se = FALSE)`, with no confidence band.

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

Does the non-linear regression model appear to provide a better or worse fit than the linear model in Part G?  Why? Are there any particular areas of the graph where the nonlinear regression line does not closely match the underlying data? If so, where?

**YOUR ANSWER HERE**

## F (5pts) 

Repeat Part E, but color the points by the food group (similar to Part B). Be sure to have only ONE single non-linear regression line in the plot. To do this, specify the `x` and `y` aesthetics globally in `ggplot()`, and the `color` aesthetic locally in `geom_point()`.

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

## G (5pts)

Repeat Part E, but include different non-linear regression lines to the plot, one for each food group. To do this, specify the `color` aesthetic globally in `ggplot()` along with `x` and `y`.

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

\newpage

# Problem 2: Correlations and inference (40 points)

## A (7pts) 

What is the correlation between `fat_100g` and `energy_100g`?

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

**YOUR ANSWER HERE**

Next, uncomment the following code, and answer:

```{r}
# food |>
#  select(fat_100g, energy_100g, sugars_100g, proteins_100g) |>
#  cor()
```

* What does the code do?  

**YOUR ANSWER HERE**

* Which pair of two unique variables has the strongest positive correlation?

**YOUR ANSWER HERE**

* Which pair of variables below has the strongest negative correlation?

**YOUR ANSWER HERE**

## B (8pts) 

* Fit a linear regression model for `energy_100g` and `fat_100g`, where `energy_100g` is the response.

* Assign this fit to an object called `energy_fat_lm`. 

* Then print out the model summary with `summary(energy_fat_lm)`.

* Look for "Multiple R-squared" in the summary output. Compute the square root of this number, and compare it with the correlation value in Part A.

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

**YOUR ANSWER HERE**

## C (20pts) 

Now, consider the following plot.

```{r}
# food |>
#   ggplot(aes(x = fat_100g, y = energy_100g, color = additives_n)) + 
#   geom_point(alpha = 0.5) + 
#   geom_smooth(method = "lm", se = FALSE)
```

This displays a linear regression for two subsets of data: `additives_n = No` (in red) and `additives_n = Yes` (in blue).

Next, consider the following output.

```{r}
# summary(lm(energy_100g ~ fat_100g * additives_n, data = food))
```

This is the **linear regression model with interactions** shown in the plot above.

Using this output, answer the following questions. Be sure to explain each answer.

* For `additives_n = No` foods, what is the estimated intercept?

**YOUR ANSWER HERE**

* For `additives_n = No` foods, what is the estimated slope?

**YOUR ANSWER HERE**

* For `additives_n = Yes` foods, what is the estimated intercept?

**YOUR ANSWER HERE**

* For `additives_n = Yes` foods, what is the estimated slope?

**YOUR ANSWER HERE**

## D (5pts) 

From the output in Part C, the term `fat_100g:additives_nYes` has a positive coefficient estimate and a very small p-value. In 1--2 sentences, interpret this result within the context of the graph in Part C?

**YOUR ANSWER HERE**

\newpage

# Problem 3: Plotting subsets of data (10 points)

Create scatterplots with `fat_100g` on the x-axis and `energy_100g` on the y-axis to compare the two following food groups: `Sugary snacks` and `Cereals and potatoes`. In each plot, include a non-linear trend line with no confidence band.

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

In 1--2 sentences, compare and contrast the trend lines.

**YOUR ANSWER HERE**
