Skip to content

Commit

Permalink
Explain GLMM practical.
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviergimenez committed Nov 12, 2023
1 parent 5fe200b commit dc1db55
Show file tree
Hide file tree
Showing 22 changed files with 2,193 additions and 1,268 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ Text and figures are licensed under Creative Commons Attribution [CC BY 4.0](htt

+ Replace crazy function using cosinus by prediction w/ rainfall/temperature (beware standardisation); Or do both

+ Debug GLMM practical

+ Complete example on survival, w/ hypothesis testing, illustrate with Bayes factors? See https://rstudio-pubs-static.s3.amazonaws.com/358672_09291d0b37ce43f08cf001cfd25c16c2.html, https://stackoverflow.com/questions/60278806/bayes-factor-in-r-with-jaggs, https://www.martinmodrak.cz/2021/03/28/three-ways-to-compute-a-bayes-factor/, http://yourdomain.com/statistics,/modeling/2017/07/07/BF_computation.html ou encore https://michael-franke.github.io/statistics,/modeling/2017/07/07/BF_computation.html

+ Properly introduce GLMs; Illustrate how to find p from logit(p)
Expand All @@ -44,8 +42,6 @@ Text and figures are licensed under Creative Commons Attribution [CC BY 4.0](htt

+ Finish up writing that book

+ ~~Add a plot with several lines from posterior distribution of regression parameters to a plot of mean response function of a covariate; then get the credible interval on the prediction~~

+ Prior predictive check for logistic storks and lmm plants
sample_mu <- rnorm( 1e4 , 178 , 20 )
sample_sigma <- runif( 1e4 , 0 , 50 )
Expand All @@ -65,6 +61,9 @@ dens( prior_h )
+ Add something on equivalence w/ MLE: say binomial lik $Bin(n,k)$ and beta prior $Beta(a,b)$ then posterior is beta $Beta(a+k, b+n-k)$; posterior mean is $(a+k)/(a+b+n)$ which can be written $(1-w)(a/a+b) + w k/n$. Posterior mean is weighted average of prior mean and MLE. When sample size is big, $n$ tends to infinity and posterior mean tends to MLE, whatever the prior. Same reasoning with variance shows that Bayes gives reasonable results, even w/ small sample size


+ ~~Debug GLMM practical~~

+ ~~Add a plot with several lines from posterior distribution of regression parameters to a plot of mean response function of a covariate; then get the credible interval on the prediction~~

+ ~~Make code/outputs fit in slides~~

Expand Down
42 changes: 42 additions & 0 deletions docs/practicals/practical9.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,47 @@ round(fit$BUGSoutput$summary[, -c(4,6)], 3)

---

Convert regression coefficients from scaled to non-scaled and compare to values used to generate data (from <https://stats.stackexchange.com/questions/361995/how-to-convert-coefficients-from-quadratic-function-from-scaled-to-not-scaled-co>)

```{r}
sbzero <- fit$BUGSoutput$sims.matrix[,'mu.a']
sbun <- fit$BUGSoutput$sims.matrix[,'b[1]']
sbdeux <- fit$BUGSoutput$sims.matrix[,'b[2]']
mu <- mean(boo)
sg <- sd(boo)
```

---

```{r}
bzero <- sbzero - sbun*mu/sg + sbdeux*mu^2/(sg^2)
hist(bzero)
abline(v = -14, col = "red", lwd = 2)
mean(bzero)
```

---

```{r}
bun <- sbdeux/sg - 2 * sbdeux * mu / (sg^2)
hist(bun)
abline(v = 1.8, col = "red", lwd = 2)
mean(bun)
```

---

```{r}
bdeux <- sbdeux/(sg^2)
hist(bdeux)
abline(v = - 0.045, col = "red", lwd = 2)
mean(bdeux)
```

---
## Frequentist approach

.tiny-font[
```{r}
library(lme4)
Expand All @@ -213,3 +254,4 @@ Parameter estimates obtained with both approaches are very similar.
visreg::visreg(fit_lme4, xvar = 'Temp')
```
]

129 changes: 101 additions & 28 deletions docs/practicals/practical9.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
### Olivier Gimenez
]
.date[
### last updated: 2023-11-11
### last updated: 2023-11-12
]

---
Expand Down Expand Up @@ -142,13 +142,13 @@
```

```
## Transect Temperature Anemones Temp
## 1 1 20.33973 53 0.1338975
## 2 1 20.47855 48 0.2125261
## 3 1 20.61737 40 0.2911548
## 4 1 20.75619 41 0.3697834
## 5 1 20.89500 32 0.4484121
## 6 1 21.03382 35 0.5270407
## Transect Temperature Anemones Temp
## 1 1 18.31928 48 -0.9742340
## 2 1 18.31673 56 -0.9757048
## 3 1 18.31417 51 -0.9771756
## 4 1 18.31162 51 -0.9786464
## 5 1 18.30906 48 -0.9801172
## 6 1 18.30651 39 -0.9815880
```
]

Expand Down Expand Up @@ -237,26 +237,98 @@

```
## mean sd 2.5% 50% 97.5% Rhat n.eff
## a[1] 3.748 0.046 3.659 3.750 3.838 1.001 3000
## a[2] 3.612 0.054 3.508 3.612 3.716 1.001 3000
## a[3] 4.096 0.035 4.027 4.097 4.163 1.001 3000
## a[4] 4.486 0.050 4.386 4.487 4.581 1.001 3000
## a[5] 4.057 0.042 3.978 4.056 4.138 1.001 3000
## a[6] 3.401 0.053 3.295 3.400 3.507 1.002 1700
## a[7] 3.621 0.068 3.484 3.621 3.754 1.003 1100
## a[8] 3.562 0.038 3.486 3.563 3.636 1.002 910
## a[9] 3.866 0.033 3.798 3.867 3.930 1.001 1800
## a[10] 3.853 0.041 3.774 3.854 3.934 1.001 3000
## b[1] -0.040 0.028 -0.094 -0.039 0.014 1.001 3000
## b[2] -0.129 0.017 -0.163 -0.129 -0.095 1.001 3000
## deviance 1333.022 4.910 1325.304 1332.328 1344.842 1.001 3000
## mu.a 3.828 0.131 3.570 3.829 4.068 1.001 3000
## sigma.a 0.377 0.121 0.229 0.351 0.664 1.006 3000
## a[1] 4.142 0.050 4.045 4.142 4.240 1.001 3000
## a[2] 4.290 0.059 4.177 4.289 4.405 1.002 1100
## a[3] 4.372 0.049 4.276 4.371 4.468 1.001 3000
## a[4] 3.579 0.098 3.391 3.578 3.767 1.001 1900
## a[5] 4.286 0.028 4.228 4.286 4.341 1.001 3000
## a[6] 4.840 0.030 4.782 4.840 4.898 1.001 3000
## a[7] 3.366 0.046 3.276 3.366 3.453 1.001 3000
## a[8] 4.175 0.032 4.113 4.175 4.237 1.001 2600
## a[9] 4.630 0.023 4.585 4.631 4.677 1.001 3000
## a[10] 3.563 0.043 3.476 3.563 3.648 1.001 3000
## b[1] -0.023 0.029 -0.079 -0.023 0.035 1.001 2800
## b[2] -0.153 0.023 -0.196 -0.153 -0.109 1.001 3000
## deviance 1401.831 4.854 1394.333 1401.129 1413.014 1.002 1100
## mu.a 4.125 0.191 3.746 4.127 4.500 1.001 3000
## sigma.a 0.568 0.167 0.341 0.535 0.980 1.007 580
```
]

---

Convert regression coefficients from scaled to non-scaled and compare to values used to generate data (from &lt;https://stats.stackexchange.com/questions/361995/how-to-convert-coefficients-from-quadratic-function-from-scaled-to-not-scaled-co&gt;)


```r
sbzero &lt;- fit$BUGSoutput$sims.matrix[,'mu.a']
sbun &lt;- fit$BUGSoutput$sims.matrix[,'b[1]']
sbdeux &lt;- fit$BUGSoutput$sims.matrix[,'b[2]']

mu &lt;- mean(boo)
sg &lt;- sd(boo)
```

---


```r
bzero &lt;- sbzero - sbun*mu/sg + sbdeux*mu^2/(sg^2)
hist(bzero)
abline(v = -14, col = "red", lwd = 2)
```

![](practical9_files/figure-html/unnamed-chunk-11-1.png)&lt;!-- --&gt;

```r
mean(bzero)
```

```
## [1] -15.89737
```

---


```r
bun &lt;- sbdeux/sg - 2 * sbdeux * mu / (sg^2)
hist(bun)
abline(v = 1.8, col = "red", lwd = 2)
```

![](practical9_files/figure-html/unnamed-chunk-12-1.png)&lt;!-- --&gt;

```r
mean(bun)
```

```
## [1] 1.939693
```

---


```r
bdeux &lt;- sbdeux/(sg^2)
hist(bdeux)
abline(v = - 0.045, col = "red", lwd = 2)
```

![](practical9_files/figure-html/unnamed-chunk-13-1.png)&lt;!-- --&gt;

```r
mean(bdeux)
```

```
## [1] -0.05066283
```

---
## Frequentist approach

.tiny-font[

```r
Expand All @@ -271,15 +343,15 @@
## Family: poisson ( log )
## Formula: Anemones ~ Temp + I(Temp^2) + (1 | Transect)
## Data: data
## AIC BIC logLik deviance df.resid
## 1382.2781 1395.4714 -687.1391 1374.2781 196
## AIC BIC logLik deviance df.resid
## 1461.916 1475.109 -726.958 1453.916 196
## Random effects:
## Groups Name Std.Dev.
## Transect (Intercept) 0.3026
## Transect (Intercept) 0.4552
## Number of obs: 200, groups: Transect, 10
## Fixed Effects:
## (Intercept) Temp I(Temp^2)
## 3.83058 -0.04063 -0.12902
## 4.12492 -0.02394 -0.15259
```
]

Expand All @@ -293,8 +365,9 @@
visreg::visreg(fit_lme4, xvar = 'Temp')
```

![](practical9_files/figure-html/unnamed-chunk-11-1.svg)&lt;!-- --&gt;
![](practical9_files/figure-html/unnamed-chunk-15-1.svg)&lt;!-- --&gt;
]

</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit dc1db55

Please sign in to comment.