Skip to content

Commit

Permalink
Update Quiz0 wrap and R review
Browse files Browse the repository at this point in the history
  • Loading branch information
gpleiss committed Sep 11, 2024
1 parent 9e02a93 commit 2e5e1db
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"hash": "00096b6cfd1ad274a2aab231017fbef6",
"hash": "a182d4c0edef7dd0c7f534c6ee0aa900",
"result": {
"markdown": "---\nlecture: \"00 Quiz 0 fun\"\nformat: revealjs\nmetadata-files: \n - _metadata.yml\n---\n---\n---\n\n## {{< meta lecture >}} {.large background-image=\"gfx/smooths.svg\" background-opacity=\"0.3\"}\n\n[Stat 406]{.secondary}\n\n[{{< meta author >}}]{.secondary}\n\nLast modified -- 13 September 2023\n\n\n\n$$\n\\DeclareMathOperator*{\\argmin}{argmin}\n\\DeclareMathOperator*{\\argmax}{argmax}\n\\DeclareMathOperator*{\\minimize}{minimize}\n\\DeclareMathOperator*{\\maximize}{maximize}\n\\DeclareMathOperator*{\\find}{find}\n\\DeclareMathOperator{\\st}{subject\\,\\,to}\n\\newcommand{\\E}{E}\n\\newcommand{\\Expect}[1]{\\E\\left[ #1 \\right]}\n\\newcommand{\\Var}[1]{\\mathrm{Var}\\left[ #1 \\right]}\n\\newcommand{\\Cov}[2]{\\mathrm{Cov}\\left[#1,\\ #2\\right]}\n\\newcommand{\\given}{\\ \\vert\\ }\n\\newcommand{\\X}{\\mathbf{X}}\n\\newcommand{\\x}{\\mathbf{x}}\n\\newcommand{\\y}{\\mathbf{y}}\n\\newcommand{\\P}{\\mathcal{P}}\n\\newcommand{\\R}{\\mathbb{R}}\n\\newcommand{\\norm}[1]{\\left\\lVert #1 \\right\\rVert}\n\\newcommand{\\snorm}[1]{\\lVert #1 \\rVert}\n$$\n\n\n\n\n\n## Why this class?\n\n* Most say requirements.\n* Interest in ML/Stat learning\n* Expressions of love/affection for Stats/CS/ML\n* Enjoyment of past similar classes\n\n## Why this class?\n\nMore idiosyncratic:\n\n::: incremental\n\n- \"Professor received Phd from CMU, must be an awesome  researcher.\"\n- \"Learn strategies.\"\n- (paraphrase) \"Course structure with less weight on exam helps with anxiety\"\n- (paraphrase) \"I love coding in R and want more of it\"\n- \"Emmmmmmmmmmmmmmmm, to learn some skills from Machine Learning and finish my minor🙃.\"\n- \"destiny\"\n- \"challenges from ChatGPT\"\n- \"I thought Daniel Mcdonald is a cool prof...\"\n- \"I have heard this is the most useful stat course in UBC.\"\n\n:::\n\n\n## Syllabus Q\n\n\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-1-1.svg){fig-align='center'}\n:::\n:::\n\n\n\n## Programming languages\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-2-1.svg){fig-align='center'}\n:::\n:::\n\n\n---\n\n## Matrix inversion\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\nlibrary(MASS)\nX <- matrix(c(5, 3, 1, -1), nrow = 2)\nX\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n [,1] [,2]\n[1,] 5 1\n[2,] 3 -1\n```\n:::\n\n```{.r .cell-code}\nsolve(X)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n [,1] [,2]\n[1,] 0.125 0.125\n[2,] 0.375 -0.625\n```\n:::\n\n```{.r .cell-code}\nginv(X)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n [,1] [,2]\n[1,] 0.125 0.125\n[2,] 0.375 -0.625\n```\n:::\n\n```{.r .cell-code}\nX^(-1)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n [,1] [,2]\n[1,] 0.2000000 1\n[2,] 0.3333333 -1\n```\n:::\n:::\n\n\n\n\n## Linear models\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\ny <- X %*% c(2, -1) + rnorm(2)\ncoefficients(lm(y ~ X))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n(Intercept) X1 X2 \n 4.8953718 0.9380314 NA \n```\n:::\n\n```{.r .cell-code}\ncoef(lm(y ~ X))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n(Intercept) X1 X2 \n 4.8953718 0.9380314 NA \n```\n:::\n\n```{.r .cell-code}\nsolve(t(X) %*% X) %*% t(X) %*% y\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n [,1]\n[1,] 2.161874\n[2,] -1.223843\n```\n:::\n\n```{.r .cell-code}\nsolve(crossprod(X), crossprod(X, y))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n [,1]\n[1,] 2.161874\n[2,] -1.223843\n```\n:::\n:::\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\nX \\ y # this is Matlab\n```\n\n::: {.cell-output .cell-output-error}\n```\nError: <text>:1:3: unexpected '\\\\'\n1: X \\\n ^\n```\n:::\n:::\n\n\n\n## Pets and plans\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-6-1.svg){fig-align='center'}\n:::\n:::\n\n\n---\n\n## Grade predictions\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-7-1.svg){fig-align='center'}\n:::\n:::\n\n\n* 4 people say 100%\n* 24 say 90%\n* 25 say 85%\n* 27 say 80%\n* Lots of clumping\n\n\n. . .\n\n1 said 35, and 1 said 50. Woof!\n\n## Prediction accuracy (last year)\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-8-1.svg){fig-align='center'}\n:::\n:::\n\n\n## Prediction accuracy (last year)\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\nsummary(lm(actual ~ predicted - 1, data = acc))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\nCall:\nlm(formula = actual ~ predicted - 1, data = acc)\n\nResiduals:\n Min 1Q Median 3Q Max \n-63.931 -2.931 1.916 6.052 21.217 \n\nCoefficients:\n Estimate Std. Error t value Pr(>|t|) \npredicted 0.96590 0.01025 94.23 <2e-16 ***\n---\nSignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\nResidual standard error: 10.2 on 137 degrees of freedom\n (8 observations deleted due to missingness)\nMultiple R-squared: 0.9848,\tAdjusted R-squared: 0.9847 \nF-statistic: 8880 on 1 and 137 DF, p-value: < 2.2e-16\n```\n:::\n:::\n",
"engine": "knitr",
"markdown": "---\nlecture: \"00 Quiz 0 fun\"\nformat: revealjs\nmetadata-files: \n - _metadata.yml\n---\n\n\n## {{< meta lecture >}} {.large background-image=\"gfx/smooths.svg\" background-opacity=\"0.3\"}\n\n[Stat 406]{.secondary}\n\n[{{< meta author >}}]{.secondary}\n\nLast modified -- 10 September 2024\n\n\n\n\n\n$$\n\\DeclareMathOperator*{\\argmin}{argmin}\n\\DeclareMathOperator*{\\argmax}{argmax}\n\\DeclareMathOperator*{\\minimize}{minimize}\n\\DeclareMathOperator*{\\maximize}{maximize}\n\\DeclareMathOperator*{\\find}{find}\n\\DeclareMathOperator{\\st}{subject\\,\\,to}\n\\newcommand{\\E}{E}\n\\newcommand{\\Expect}[1]{\\E\\left[ #1 \\right]}\n\\newcommand{\\Var}[1]{\\mathrm{Var}\\left[ #1 \\right]}\n\\newcommand{\\Cov}[2]{\\mathrm{Cov}\\left[#1,\\ #2\\right]}\n\\newcommand{\\given}{\\ \\vert\\ }\n\\newcommand{\\X}{\\mathbf{X}}\n\\newcommand{\\x}{\\mathbf{x}}\n\\newcommand{\\y}{\\mathbf{y}}\n\\newcommand{\\P}{\\mathcal{P}}\n\\newcommand{\\R}{\\mathbb{R}}\n\\newcommand{\\norm}[1]{\\left\\lVert #1 \\right\\rVert}\n\\newcommand{\\snorm}[1]{\\lVert #1 \\rVert}\n\\newcommand{\\tr}[1]{\\mbox{tr}(#1)}\n\\newcommand{\\brt}{\\widehat{\\beta}^R_{s}}\n\\newcommand{\\brl}{\\widehat{\\beta}^R_{\\lambda}}\n\\newcommand{\\bls}{\\widehat{\\beta}_{ols}}\n\\newcommand{\\blt}{\\widehat{\\beta}^L_{s}}\n\\newcommand{\\bll}{\\widehat{\\beta}^L_{\\lambda}}\n\\newcommand{\\U}{\\mathbf{U}}\n\\newcommand{\\D}{\\mathbf{D}}\n\\newcommand{\\V}{\\mathbf{V}}\n$$\n\n\n\n\n\n## Why this class?\n\n- \"This is a required course\"\n- \"Heard it's fun!\"\n- \"I want to graduate\"\n- \"8am!\" (/s)\n- \"I want to learn a lot about ML.\"\n- \"I want to dig deeper after taking 306.\"\n- \"I want to get... more comfortable with R.\"\n- \"At first it was because everyone I knew was taking it.\"\n- \"Practical skills in preperation for future careers\"\n\n\n## Syllabus Q\n\n\n\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-1-1.svg){fig-align='center'}\n:::\n:::\n\n\n\n\n## Programming languages\n\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-2-1.svg){fig-align='center'}\n:::\n:::\n\n\n\n<!--\n## Matrix inversion\n\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\nlibrary(MASS)\nX <- matrix(c(5, 3, 1, -1), nrow = 2)\nX\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2]\n[1,] 5 1\n[2,] 3 -1\n```\n\n\n:::\n\n```{.r .cell-code}\nsolve(X)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2]\n[1,] 0.125 0.125\n[2,] 0.375 -0.625\n```\n\n\n:::\n\n```{.r .cell-code}\nginv(X)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2]\n[1,] 0.125 0.125\n[2,] 0.375 -0.625\n```\n\n\n:::\n\n```{.r .cell-code}\nX^(-1)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2]\n[1,] 0.2000000 1\n[2,] 0.3333333 -1\n```\n\n\n:::\n:::\n\n\n\n\n\n## Linear models\n\n\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\ny <- X %*% c(2, -1) + rnorm(2)\ncoefficients(lm(y ~ X))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n(Intercept) X1 X2 \n 4.8953718 0.9380314 NA \n```\n\n\n:::\n\n```{.r .cell-code}\ncoef(lm(y ~ X))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n(Intercept) X1 X2 \n 4.8953718 0.9380314 NA \n```\n\n\n:::\n\n```{.r .cell-code}\nsolve(t(X) %*% X) %*% t(X) %*% y\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1]\n[1,] 2.161874\n[2,] -1.223843\n```\n\n\n:::\n\n```{.r .cell-code}\nsolve(crossprod(X), crossprod(X, y))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1]\n[1,] 2.161874\n[2,] -1.223843\n```\n\n\n:::\n:::\n\n::: {.cell layout-align=\"center\"}\n\n```{.r .cell-code}\nX \\ y # this is Matlab\n```\n\n::: {.cell-output .cell-output-error}\n\n```\nError: <text>:1:3: unexpected '\\\\'\n1: X \\\n ^\n```\n\n\n:::\n:::\n\n---\n-->\n\n## Plans\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-6-1.svg){fig-align='center'}\n:::\n:::\n\n## Grade predictions\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![](00-quiz-0-wrap_files/figure-revealjs/unnamed-chunk-7-1.svg){fig-align='center'}\n:::\n:::\n\n* 5 people say 100%\n* 18 say 90%\n* 20 say 85%\n* 41 say 80%\n",
"supporting": [
"00-quiz-0-wrap_files"
],
Expand Down
4 changes: 2 additions & 2 deletions _freeze/schedule/slides/00-r-review/execute-results/html.json

Large diffs are not rendered by default.

110 changes: 29 additions & 81 deletions schedule/slides/00-quiz-0-wrap.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,31 @@ metadata-files:

## Why this class?

* Most say requirements.
* Interest in ML/Stat learning
* Expressions of love/affection for Stats/CS/ML
* Enjoyment of past similar classes

## Why this class?

More idiosyncratic:

::: incremental

- "Professor received Phd from CMU, must be an awesome  researcher."
- "Learn strategies."
- (paraphrase) "Course structure with less weight on exam helps with anxiety"
- (paraphrase) "I love coding in R and want more of it"
- "Emmmmmmmmmmmmmmmm, to learn some skills from Machine Learning and finish my minor🙃."
- "destiny"
- "challenges from ChatGPT"
- "I thought Daniel Mcdonald is a cool prof..."
- "I have heard this is the most useful stat course in UBC."

:::
- "This is a required course"
- "Heard it's fun!"
- "I want to graduate"
- "8am!" (/s)
- "I want to learn a lot about ML."
- "I want to dig deeper after taking 306."
- "I want to get... more comfortable with R."
- "At first it was because everyone I knew was taking it."
- "Practical skills in preperation for future careers"


## Syllabus Q

```{r load-quiz, include=FALSE}
quiz <- read_rds(here::here(
"..", "..", "..", "WinterT1-2023", "gh-class-management", "lab0",
"lab0-anon.rds"
))
library(tidyverse)
quiz <- tibble(read.csv("canvas_quiz0.csv"))
```

```{r}
#| echo: false
#| fig-height: 6
#| fig-width: 12
quiz |>
ggplot(aes(str_wrap(syllabus, 20))) +
geom_bar(fill = blue) +
ggplot(aes(str_wrap(syllabus_fact, 20))) +
geom_bar(fill = "blue") +
scale_y_continuous(expand = expansion(c(0,0.05))) +
xlab("")
```
Expand All @@ -62,14 +47,13 @@ quiz |>
#| fig-height: 6
#| fig-width: 12
quiz |>
ggplot(aes(str_wrap(r_coding, 20))) +
geom_bar(fill = blue) +
ggplot(aes(str_wrap(r_experience, 20))) +
geom_bar(fill = "blue") +
scale_y_continuous(expand = expansion(c(0, 0.05))) +
xlab("")
```

---

<!--
## Matrix inversion
```{r}
Expand Down Expand Up @@ -97,76 +81,40 @@ solve(crossprod(X), crossprod(X, y))
#| error: true
X \ y # this is Matlab
```
---
-->


## Pets and plans
## Plans

```{r}
#| echo: false
#| fig-height: 6
#| fig-width: 12
library(cowplot)
p1 <- quiz %>%
ggplot(aes(str_wrap(pets, 30))) +
geom_bar(fill = blue) +
coord_flip() +
scale_y_continuous(expand = expansion(c(0, 0.05))) +
xlab("")
p2 <- quiz %>%
ggplot(aes(str_wrap(plans, 20))) +
geom_bar(fill = orange) +
coord_flip() +
quiz %>%
ggplot(aes(str_wrap(graduation_plans, 20))) +
geom_bar(fill = "orange") +
scale_y_continuous(expand = expansion(c(0, 0.05))) +
xlab("")
plot_grid(p1, p2)
```

---

## Grade predictions

```{r, echo=FALSE, fig.height=4, fig.width=12, fig.align='center'}
#| echo: false
#| fig-height: 4
#| fig-width: 12
quiz |>
ggplot(aes(grade)) +
ggplot(aes(predicted_grade)) +
geom_histogram(
fill = orange, colour = "black",
fill = "orange", colour = "black",
breaks = c(0, 50, 54, 59, 63, 67, 71, 75, 79, 84, 89, 101)
) +
scale_y_continuous(expand = expansion(c(0, 0.05)))
```

* `r sum(quiz$grade == 100)` people say 100%
* `r sum(quiz$grade == 90)` say 90%
* `r sum(quiz$grade == 85)` say 85%
* `r sum(quiz$grade == 80)` say 80%
* Lots of clumping


. . .

1 said 35, and 1 said 50. Woof!

## Prediction accuracy (last year)

```{r}
#| echo: false
acc <- read_rds(here::here(
"..", "..", "..", "WinterT1-2022", "final-grades",
"prediction-accuracy.rds"
)) |>
rename(predicted = pfg, actual = fg)
ggplot(acc, aes(predicted, actual)) +
geom_point(colour = orange) +
geom_abline(slope = 1, intercept = 0) +
coord_equal(xlim = c(0, 100), ylim = c(0, 100))
```

## Prediction accuracy (last year)

```{r}
summary(lm(actual ~ predicted - 1, data = acc))
```
* `r sum(quiz$predicted_grade == 100)` people say 100%
* `r sum(quiz$predicted_grade == 90)` say 90%
* `r sum(quiz$predicted_grade == 85)` say 85%
* `r sum(quiz$predicted_grade == 80)` say 80%
25 changes: 20 additions & 5 deletions schedule/slides/00-r-review.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ fun <- function(x) {
fun(14)
```

. . .

Non-trivial cases are `data-masking` environments.

```{r}
Expand Down Expand Up @@ -614,13 +616,23 @@ What you need is a Reproducible Example or `reprex`.
* This is a small chunk of code that
1. runs in it's own environment
1. and produces the error.

#

The best way to do this is with the `{reprex}` package.

---

::: flex
::: w-50
![](gfx/reprex_github_1.png){fig-align="center" fig-alt="Reprex example 1"}
:::

::: w-50
![](gfx/reprex_github_2.png){fig-align="center" fig-alt="Reprex example 2"}
:::
:::


## The `{reprex}` package

## Reproducible examples, How it works {.smaller}

1. Open a new `.R` script.

Expand Down Expand Up @@ -658,6 +670,8 @@ Because Reprex runs in it's own environment, it doesn't have access to any of th
- lacking basic data types (e.g., hash maps)
- informal classes (`class(x) <- "a weird new class!"`)
- tonnes of functions/data/objects in the global namespace
- `3 == "3"` (evaluates to TRUE?!!?!)
- (Somehow R is weirder than [Javascript](https://www.facebook.com/BangaloreRocks/videos/wat-a-lightning-talk-by-gary-bernhardt-from-codemash-2012/10150507601498579/))
- Rscript executable treats code *differently than the R REPL*

# Understanding `{tidyverse}`
Expand Down Expand Up @@ -795,6 +809,7 @@ This technically works...but at a minimum it makes it hard to extend pipe sequen



<!--
## A new one...
R loves weird pipes. Just stick to `|>`, it results in clear code.
Expand All @@ -809,7 +824,7 @@ tib <- tibble(x = 1:5, z = 6:10)
tib %<>% mutate(b = x + z)
tib
```

-->


## Data processing in `{dplyr}` {.smaller}
Expand Down
Binary file added schedule/slides/gfx/reprex_github_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added schedule/slides/gfx/reprex_github_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2e5e1db

Please sign in to comment.