---
title: "Using & Interpreting Linear Models"
subtitle: "w/ggstats"
theme: flatly 
date: today
toc: true
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Contents"
    # hide code?

execute:
  warning: false
  message: false
---

## ggstats: additional stats package for ggplot

The `ggstats` package provides new statistics, new geometries and new positions for `ggplot2` and a suite of functions to facilitate the creation of statistical plots.

1. Install `ggstats` as shown below.  
2. You may need to update `ggplot`.
3. Learn more [here](https://larmarange.github.io/ggstats/)

```{r, eval=FALSE}
library(ggplot)
install.packages("ggstats")
```

```{r}
library(ggstats)
```

## swiss dataset & the `lm()` function

The `swiss` dataset is a classic dataset of Fertility & Socioeconomic Indicators in Switzerland in 1888.

```{r}
# skim the help file
?swiss
```

With the function `lm()` we fit a linear model to data.  Specifically, we'll use regression: Estimating the relationship between a dependent variable and one or more independent variables.

Below we predict the `Fertility` variable based on the other data in `swiss` (least squares regression).  The coefficients of the model are contained in `mod1` and can be used to determine the strength of our model.

```{r}
# compute a least squares regression
mod1 <- lm(Fertility ~ ., data = swiss)

print(mod1)
```

## quick visualization of `lm()` output
The `ggstats` package allows for quick viewing of the coefficients. 

Remember p-value < 0.05 ( 5 % ) usually means statistically significant.  The Beta value on the x-axis represents the magnitude & direction between the variables, i.e.,  (+/- coorelation)

```{r}
ggcoef_model(mod1, plot_title = "Predicting Fertility")
```

## questions

1. Based on the plot, what variable is most highly coorelated with Fertility?
2. Which variable has no significant impact on Fertility?
3. Apply these data-viz techniques to the `penguins`. *You will have to select only the numeric variables first*. 
4. Repeat but after you create a new variable called `bill_ratio = bill_length_mm / bill_depth_mm`
