Skip to content

Commit

Permalink
some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinsimpson committed Jan 22, 2025
1 parent 529bd34 commit 2fd4870
Show file tree
Hide file tree
Showing 16 changed files with 53,193 additions and 714 deletions.
25 changes: 25 additions & 0 deletions day-1/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,31 @@ $$\log_e \left( \frac{\pi}{1-\pi} \right) = \beta_0 + \beta_1 X_i$$

---

# Link function

```{r logit-transformation, echo = FALSE, fig.align = "center"}
wasp <- read_csv(here("data", "darlingtonia.csv"), comment = "#",
col_types = "dl")
# plot
p1 <- ggplot(wasp,
aes(x = leafHeight, y = as.numeric(visited))
) +
geom_point() +
labs(
x = "Leaf Height [cm]",
y = "Probability of visitation"
)
df <- data.frame(p = (1:999) / 1000)
df <- transform(df, logitp = qlogis(p))
p2 <- p1 + geom_smooth(method = "lm", se = FALSE)
p3 <- ggplot(df, aes(x = p, y = logitp)) + geom_line() +
ylab(expression("Log Odds" ~~~ log * bgroup("(", frac(p, 1 - p),")"))) +
xlab("Probability of visitation") + coord_flip() # ? flip this
p2 + p3
```

---

# Logistic regression &mdash; *Darlingtonia*

```{r load-darl-data, echo = TRUE}
Expand Down
6 changes: 6 additions & 0 deletions day-1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@

---

# Link function

&lt;img src="index_files/figure-html/logit-transformation-1.svg" style="display: block; margin: auto;" /&gt;

---

# Logistic regression &amp;mdash; *Darlingtonia*


Expand Down
648 changes: 648 additions & 0 deletions day-1/index_files/figure-html/logit-transformation-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
914 changes: 457 additions & 457 deletions day-1/index_files/figure-html/plot-moth-glm1-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
150 changes: 75 additions & 75 deletions day-2/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1317,81 +1317,6 @@ x2_bs |>
---
class: inverse middle center big-subsection

# Penalties

---

# How wiggly?

$$
\int_{\mathbb{R}} [f^{\prime\prime}]^2 dx = \boldsymbol{\beta}^{\mathsf{T}}\mathbf{S}\boldsymbol{\beta}
$$

$\mathbf{S}$ is known as the penalty matrix

Penalty matrix doesn't have to be derivative based &mdash; *P-splines*

As long as we can write $\mathbf{S}$ so that it measures the complexity of smooth as a function of the parameters of the smooth we're OK

---

# TPRS basis

```{r, out.width = "85%", fig.align = "center"}
new_df <- with(mcycle, tibble(times = evenly(times, n = 100)))
bfun <- basis(s(times), data = new_df, constraints = TRUE)
draw(bfun) + facet_wrap(~ .bf)
```

---

# TPRS basis

```{r}
m <- gam(accel ~ s(times), dat = mcycle, method = "REML")
S <- penalty(m, smooth = "s(times)")
S
```

---

# TPRS basis & penalty

.center[
```{r, echo = FALSE, dev = "png", out.width = "100%", dpi = 300}
library("patchwork")
p1 <- draw(bfun) + facet_wrap(~ .bf)
p2 <- draw(S)
p1 + p2 + plot_layout(ncol = 2)
```
]
---

# CRS basis & penalty

```{r}
m <- gam(accel ~ s(times, bs = "cr"), data = mcycle, method = "REML")
S <- penalty(m, smooth = "s(times)")
# draw(S)
```

---

# CRS basis & penalty

.center[
```{r, echo = FALSE, dev = "png", out.width = "100%", dpi = 300}
library("patchwork")
bfun_cc <- basis(s(times, bs = "cr"), data = new_df, constraints = TRUE)
p1 <- draw(bfun_cc) + facet_wrap(~ .bf)
p2 <- draw(S)
p1 + p2 + plot_layout(ncol = 2)
```
]

---
class: inverse middle center big-subsection

# gratia 📦

---
Expand Down Expand Up @@ -1472,3 +1397,78 @@ draw(m_sim, residuals = TRUE, overall_uncertainty = TRUE,
unconditional = TRUE, rug = FALSE)
```
]

---
class: inverse middle center big-subsection

# Penalties

---

# How wiggly?

$$
\int_{\mathbb{R}} [f^{\prime\prime}]^2 dx = \boldsymbol{\beta}^{\mathsf{T}}\mathbf{S}\boldsymbol{\beta}
$$

$\mathbf{S}$ is known as the penalty matrix

Penalty matrix doesn't have to be derivative based &mdash; *P-splines*

As long as we can write $\mathbf{S}$ so that it measures the complexity of smooth as a function of the parameters of the smooth we're OK

---

# TPRS basis

```{r, out.width = "85%", fig.align = "center"}
new_df <- with(mcycle, tibble(times = evenly(times, n = 100)))
bfun <- basis(s(times), data = new_df, constraints = TRUE)
draw(bfun) + facet_wrap(~ .bf)
```

---

# TPRS basis

```{r}
m <- gam(accel ~ s(times), dat = mcycle, method = "REML")
S <- penalty(m, smooth = "s(times)")
S
```

---

# TPRS basis & penalty

.center[
```{r, echo = FALSE, dev = "png", out.width = "100%", dpi = 300}
library("patchwork")
p1 <- draw(bfun) + facet_wrap(~ .bf)
p2 <- draw(S)
p1 + p2 + plot_layout(ncol = 2)
```
]
---

# CRS basis & penalty

```{r}
m <- gam(accel ~ s(times, bs = "cr"), data = mcycle, method = "REML")
S <- penalty(m, smooth = "s(times)")
# draw(S)
```

---

# CRS basis & penalty

.center[
```{r, echo = FALSE, dev = "png", out.width = "100%", dpi = 300}
library("patchwork")
bfun_cc <- basis(s(times, bs = "cr"), data = new_df, constraints = TRUE)
p1 <- draw(bfun_cc) + facet_wrap(~ .bf)
p2 <- draw(S)
p1 + p2 + plot_layout(ncol = 2)
```
]
172 changes: 86 additions & 86 deletions day-2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1232,92 +1232,6 @@
---
class: inverse middle center big-subsection

# Penalties

---

# How wiggly?

$$
\int_{\mathbb{R}} [f^{\prime\prime}]^2 dx = \boldsymbol{\beta}^{\mathsf{T}}\mathbf{S}\boldsymbol{\beta}
$$

`\(\mathbf{S}\)` is known as the penalty matrix

Penalty matrix doesn't have to be derivative based &amp;mdash; *P-splines*

As long as we can write `\(\mathbf{S}\)` so that it measures the complexity of smooth as a function of the parameters of the smooth we're OK

---

# TPRS basis


```r
new_df &lt;- with(mcycle, tibble(times = evenly(times, n = 100)))
bfun &lt;- basis(s(times), data = new_df, constraints = TRUE)
draw(bfun) + facet_wrap(~ .bf)
```

&lt;img src="index_files/figure-html/unnamed-chunk-6-1.svg" width="85%" style="display: block; margin: auto;" /&gt;

---

# TPRS basis


```r
m &lt;- gam(accel ~ s(times), dat = mcycle, method = "REML")
S &lt;- penalty(m, smooth = "s(times)")
S
```

```
## # A tibble: 81 × 6
## .smooth .type .penalty .row .col .value
## &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
## 1 s(times) TPRS s(times) F1 F1 13.1
## 2 s(times) TPRS s(times) F1 F2 2.71
## 3 s(times) TPRS s(times) F1 F3 6.61
## 4 s(times) TPRS s(times) F1 F4 2.05
## 5 s(times) TPRS s(times) F1 F5 6.49
## 6 s(times) TPRS s(times) F1 F6 -2.38
## 7 s(times) TPRS s(times) F1 F7 7.97
## 8 s(times) TPRS s(times) F1 F8 0.450
## 9 s(times) TPRS s(times) F1 F9 0
## 10 s(times) TPRS s(times) F2 F1 2.71
## # ℹ 71 more rows
```

---

# TPRS basis &amp; penalty

.center[
&lt;img src="index_files/figure-html/unnamed-chunk-8-1.png" width="100%" /&gt;
]
---

# CRS basis &amp; penalty


```r
m &lt;- gam(accel ~ s(times, bs = "cr"), data = mcycle, method = "REML")
S &lt;- penalty(m, smooth = "s(times)")
# draw(S)
```

---

# CRS basis &amp; penalty

.center[
&lt;img src="index_files/figure-html/unnamed-chunk-10-1.png" width="100%" /&gt;
]

---
class: inverse middle center big-subsection

# gratia 📦

---
Expand Down Expand Up @@ -1417,6 +1331,92 @@

.center[
&lt;img src="index_files/figure-html/draw-mcycle-options-plot-1.svg" width="95%" /&gt;
]

---
class: inverse middle center big-subsection

# Penalties

---

# How wiggly?

$$
\int_{\mathbb{R}} [f^{\prime\prime}]^2 dx = \boldsymbol{\beta}^{\mathsf{T}}\mathbf{S}\boldsymbol{\beta}
$$

`\(\mathbf{S}\)` is known as the penalty matrix

Penalty matrix doesn't have to be derivative based &amp;mdash; *P-splines*

As long as we can write `\(\mathbf{S}\)` so that it measures the complexity of smooth as a function of the parameters of the smooth we're OK

---

# TPRS basis


```r
new_df &lt;- with(mcycle, tibble(times = evenly(times, n = 100)))
bfun &lt;- basis(s(times), data = new_df, constraints = TRUE)
draw(bfun) + facet_wrap(~ .bf)
```

&lt;img src="index_files/figure-html/unnamed-chunk-6-1.svg" width="85%" style="display: block; margin: auto;" /&gt;

---

# TPRS basis


```r
m &lt;- gam(accel ~ s(times), dat = mcycle, method = "REML")
S &lt;- penalty(m, smooth = "s(times)")
S
```

```
## # A tibble: 81 × 6
## .smooth .type .penalty .row .col .value
## &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
## 1 s(times) TPRS s(times) F1 F1 13.1
## 2 s(times) TPRS s(times) F1 F2 2.71
## 3 s(times) TPRS s(times) F1 F3 6.61
## 4 s(times) TPRS s(times) F1 F4 2.05
## 5 s(times) TPRS s(times) F1 F5 6.49
## 6 s(times) TPRS s(times) F1 F6 -2.38
## 7 s(times) TPRS s(times) F1 F7 7.97
## 8 s(times) TPRS s(times) F1 F8 0.450
## 9 s(times) TPRS s(times) F1 F9 0
## 10 s(times) TPRS s(times) F2 F1 2.71
## # ℹ 71 more rows
```

---

# TPRS basis &amp; penalty

.center[
&lt;img src="index_files/figure-html/unnamed-chunk-8-1.png" width="100%" /&gt;
]
---

# CRS basis &amp; penalty


```r
m &lt;- gam(accel ~ s(times, bs = "cr"), data = mcycle, method = "REML")
S &lt;- penalty(m, smooth = "s(times)")
# draw(S)
```

---

# CRS basis &amp; penalty

.center[
&lt;img src="index_files/figure-html/unnamed-chunk-10-1.png" width="100%" /&gt;
]
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
Expand Down
Loading

0 comments on commit 2fd4870

Please sign in to comment.