forked from osbili/sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgam_example1.qmd
162 lines (129 loc) · 3.49 KB
/
bgam_example1.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
title: "Bayesian GAM Part1"
author: "Murray Logan"
date: today
date-format: "DD/MM/YYYY"
format:
html:
## Format
theme: [default, ../resources/ws-style.scss]
css: ../resources/ws_style.css
html-math-method: mathjax
## Table of contents
toc: true
toc-float: true
## Numbering
number-sections: true
number-depth: 3
## Layout
page-layout: full
fig-caption-location: "bottom"
fig-align: "center"
fig-width: 4
fig-height: 4
fig-dpi: 72
tbl-cap-location: top
## Code
code-fold: false
code-tools: true
code-summary: "Show the code"
code-line-numbers: true
code-block-border-left: "#ccc"
code-copy: true
highlight-style: atom-one
## Execution
execute:
echo: true
cache: true
## Rendering
embed-resources: true
crossref:
fig-title: '**Figure**'
fig-labels: arabic
tbl-title: '**Table**'
tbl-labels: arabic
engine: knitr
output_dir: "docs"
documentclass: article
fontsize: 12pt
mainfont: Arial
mathfont: LiberationMono
monofont: DejaVu Sans Mono
classoption: a4paper
bibliography: ../resources/references.bib
---
```{r setup, include=FALSE, warnings=FALSE, message=FALSE}
knitr::opts_chunk$set(cache.lazy = FALSE, tidy='styler')
```
# Preparations
Load the necessary libraries
```{r}
#| label: libraries
#| output: false
#| eval: true
#| warning: false
#| message: false
#| cache: false
library(tidyverse) #for data wrangling etc
library(cmdstanr) #for cmdstan
library(brms) #for fitting models in STAN
library(standist) #for exploring distributions
library(coda) #for diagnostics
library(bayesplot) #for diagnostics
library(ggmcmc) #for MCMC diagnostics
library(DHARMa) #for residual diagnostics
library(rstan) #for interfacing with STAN
library(emmeans) #for marginal means etc
library(broom) #for tidying outputs
library(tidybayes) #for more tidying outputs
library(HDInterval) #for HPD intervals
library(ggeffects) #for partial plots
library(broom.mixed)#for summarising models
library(posterior) #for posterior draws
library(ggeffects) #for partial effects plots
library(patchwork) #for multi-panel figures
library(bayestestR) #for ROPE
library(see) #for some plots
library(easystats) #framework for stats, modelling and visualisation
library(mgcv)
library(gratia)
theme_set(theme_grey()) #put the default ggplot theme back
source('helperFunctions.R')
```
# Scenario
This is an entirely fabricated example (how embarrising).
So here is a picture of some Red Grouse Chicks to compensate..
{#fig-grouse width="251" height="290"}
x y
-- --
2 3
4 5
8 6
10 7
14 4
: Format of data.gp.csv data file {#tbl-data.gp .table-condensed}
------ -----------------------------
**x** - a continuous predictor
**y** - a continuous response
------ -----------------------------
: Format of data.gp.csv data file {#tbl-data.gp1 .table-condensed}
# Read in the data
```{r readData, results='markdown', eval=TRUE}
data_gam <- read_csv("../data/data_gam.csv", trim_ws = TRUE)
```
# Exploratory data analysis
Model formula:
$$
\begin{align}
y_i &\sim{} \mathcal{N}(\mu_i, \sigma^2)\\
\mu_i &=\beta_0 + f(x_i)\\
f(x_i) &= \sum^k_{j=1}{b_j(x_i)\beta_j}
\end{align}
$$
where $\beta_0$ is the y-intercept, and $f(x)$ indicates an additive smoothing function of $x$.
# Fit the model
# MCMC sampling diagnostics
# Model validation
# Partial effects plots
# Model investigation
# Further analyses