Skip to content

Commit

Permalink
1208 CS3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanrongen committed Aug 12, 2024
1 parent 9d95e88 commit 8e5ff87
Show file tree
Hide file tree
Showing 43 changed files with 85 additions and 19 deletions.

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions _freeze/site_libs/clipboard/clipboard.min.js

Large diffs are not rendered by default.

Binary file modified data_CS.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion materials/cs3_practical_linear-regression.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ We will use the data from the file `data/CS3-statedata.csv` data set for this ex

Perform a linear regression on the variables `life_exp` and `murder` and do the following:

1. Find the value of the slope and intercept coefficients for both regressions
1. Find the value of the slope and intercept coefficients.
2. Determine if the slope is significantly different from zero (i.e. is there a relationship between the two variables)
3. Produce a scatter plot of the data with the line of best fit superimposed on top.
4. Produce diagnostic plots and discuss with your (virtual) neighbour if you should have carried out a simple linear regression in each case
Expand Down
88 changes: 76 additions & 12 deletions materials/cs5_practical_multiple-linear-regression.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,76 @@ exec(open('setup_files/setup.py').read())
## R

### Libraries

```{r}
#| eval: false
# A collection of R packages designed for data science
library(tidyverse)
# Converts stats functions to a tidyverse-friendly format
library(rstatix)
# Creates diagnostic plots using ggplot2
library(ggResidpanel)
# Helper functions for tidying data
library(broom)
```

### Functions

```{r}
#| eval: false
#| warning: false
# Gets underlying data out of model object
broom::augment()
# Creates diagnostic plots
ggResidpanel::resid_panel()
# Performs an analysis of variance
stats::anova()
# Creates a linear model
stats::lm()
```

## Python

### Libraries

```{python}
#| eval: false
# A fundamental package for scientific computing in Python
import numpy as np
# A Python data analysis and manipulation tool
import pandas as pd
# Simple yet exhaustive stats functions.
import pingouin as pg
# Python equivalent of `ggplot2`
from plotnine import *
# Statistical models, conducting tests and statistical data exploration
import statsmodels.api as sm
# Convenience interface for specifying models using formula strings and DataFrames
import statsmodels.formula.api as smf
```

### Functions

```{python}
#| eval: false
# Reads in a .csv file
pandas.read_csv()
# Creates a model from a formula and data frame
statsmodels.formula.api.ols()
```

:::
:::

Expand Down Expand Up @@ -288,7 +352,7 @@ We write the model as follows:

```{python}
# create a linear model
model = smf.ols(formula= "pm2_5 ~ avg_temp + C(location) + rain_mm + wind_m_s + wind_m_s:location", data = pm2_5_py)
model = smf.ols(formula = "pm2_5 ~ avg_temp + C(location) + rain_mm + wind_m_s + wind_m_s:location", data = pm2_5_py)
# and get the fitted parameters of the model
lm_pm2_5_full_py = model.fit()
```
Expand All @@ -314,7 +378,7 @@ What to do? We'll explore this in more detail in the chapter on model comparison

```{python}
# create a linear model
model = smf.ols(formula= "pm2_5 ~ avg_temp + C(location) * wind_m_s", data = pm2_5_py)
model = smf.ols(formula = "pm2_5 ~ avg_temp + C(location) * wind_m_s", data = pm2_5_py)
# and get the fitted parameters of the model
lm_pm2_5_red_py = model.fit()
```
Expand Down Expand Up @@ -425,7 +489,7 @@ First, we define the model

```{python}
# create a linear model
model = smf.ols(formula= "pm2_5 ~ avg_temp + C(location) + wind_m_s",
model = smf.ols(formula = "pm2_5 ~ avg_temp + C(location) + wind_m_s",
data = pm2_5_py)
# and get the fitted parameters of the model
Expand Down Expand Up @@ -455,11 +519,11 @@ So our two equations would be as follows:

$PM2.5_{inner} = 19.04 + 0.016 \times avg\_temp - 0.50 \times wind\_m\_s$

$PM2.5_{outer} = (19.22 - 4.05) + 0.016 \times avg\_temp - 0.50 \times wind\_m\_s$
$PM2.5_{outer} = (19.04 - 4.05) + 0.016 \times avg\_temp - 0.50 \times wind\_m\_s$

gives

$PM2.5_{outer} = 15.17 + 0.016 \times avg\_temp - 0.50 \times wind\_m\_s$
$PM2.5_{outer} = 14.99 + 0.016 \times avg\_temp - 0.50 \times wind\_m\_s$

### Revisiting linear regression

Expand Down Expand Up @@ -505,7 +569,7 @@ First, we define the model

```{python}
# create a linear model
model = smf.ols(formula= "pm2_5 ~ wind_m_s",
model = smf.ols(formula = "pm2_5 ~ wind_m_s",
data = pm2_5_py)
# and get the fitted parameters of the model
Expand Down Expand Up @@ -602,7 +666,7 @@ First, we define the model:

```{python}
# create a linear model
model = smf.ols(formula= "pm2_5 ~ C(location)", data = pm2_5_py)
model = smf.ols(formula = "pm2_5 ~ C(location)", data = pm2_5_py)
# and get the fitted parameters of the model
lm_pm2_5_loc_py = model.fit()
```
Expand Down Expand Up @@ -674,7 +738,7 @@ We define the null model as follows:

```{python}
# create a linear model
model = smf.ols(formula= "pm2_5 ~ 1", data = pm2_5_py)
model = smf.ols(formula = "pm2_5 ~ 1", data = pm2_5_py)
# and get the fitted parameters of the model
lm_pm2_5_null_py = model.fit()
```
Expand Down Expand Up @@ -756,7 +820,7 @@ trees_py = pd.read_csv("data/CS5-trees.csv")

```{python}
# create a linear model
model = smf.ols(formula= "volume ~ height * girth",
model = smf.ols(formula = "volume ~ height * girth",
data = trees_py)
# and get the fitted parameters of the model
lm_trees_full_py = model.fit()
Expand Down Expand Up @@ -843,7 +907,7 @@ lm_trees_add

```{python}
# create a linear model
model = smf.ols(formula= "volume ~ height + girth",
model = smf.ols(formula = "volume ~ height + girth",
data = trees_py)
# and get the fitted parameters of the model
lm_trees_add_py = model.fit()
Expand Down Expand Up @@ -930,7 +994,7 @@ lm_height

```{python}
# create a linear model
model = smf.ols(formula= "volume ~ height",
model = smf.ols(formula = "volume ~ height",
data = trees_py)
# and get the fitted parameters of the model
lm_height_py = model.fit()
Expand Down Expand Up @@ -1016,7 +1080,7 @@ lm_girth

```{python}
# create a linear model
model = smf.ols(formula= "volume ~ girth",
model = smf.ols(formula = "volume ~ girth",
data = trees_py)
# and get the fitted parameters of the model
lm_girth_py = model.fit()
Expand Down

0 comments on commit 8e5ff87

Please sign in to comment.