Skip to content

Commit

Permalink
renames pages
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-watson committed Jun 23, 2023
1 parent b382ab6 commit f8870aa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 126 deletions.
11 changes: 9 additions & 2 deletions content/docs/glmmr/model/model_code.md
Expand Up @@ -13,6 +13,9 @@ The arguments to create a new `Model` object are:
* `var_par`: Optionally, the (starting) value of the scale parameter.
* `data`: A data frame with the data for the model. This can be omitted if data are provided separately to the `mean` and `covariance` arguments via their lists.
* `family`: An R family.
* `offset`: An optional vector of offset values
* `trials`: For binomially distributed data, the number of trials can be specified here.
* `weights`: An optional vector of regression weights. For a Gaussian model, these weights divide the individual level variance. For non-Gaussian models they weight the log-likelihood.
We discuss each of these below. Examples are provided [here](model_examples).

## Formula
Expand Down Expand Up @@ -54,14 +57,18 @@ where we also provide logit, inverse, and identity link functions for the specif
Data are required to build the matrices and perform the calculations. When we want to analyse a study design prior to data being collected we will have to create our own covariate data for this purpose. We provide several useful functions to create `data.frame`s with different structures to support generating dummy data, see [Generating data](../creating_data).

## Parameter values
Parameter values are required to generate the matrices of the GLMM or act as starting values for model fitting algorithms. If they are not specified then default values are used. Parameter values are given separately to the `mean` and `covariance` arguments when specifying a new model object; values are passed as a vector in the order they appear in the formula. For example, for the mean function we might specify (where we include the formula for clarity):
Parameter values are required to generate the matrices of the GLMM or act as starting values for model fitting algorithms. If they are not specified then random parameter values are used (Uniform(-2,2) for fixed effect parameters, and Uniform(0,1) for covariance paramters). Parameter values are given separately to the `mean` and `covariance` arguments when specifying a new model object; values are passed as a vector in the order they appear in the formula. For example, for the mean function we might specify (where we include the formula for clarity):
```
mean = list(formula = ~x1 + x2,
parameters = c(-1,1,2))
```
to get the formula (or starting values) $-1 + x_1 + 2x_2$. Similarly, for a covariance function:
```
covariance = list(formula = ~ (1|gr(cl)*ar0(t)),
paramters = c(0.05, 0.8))
```




# Examples

58 changes: 2 additions & 56 deletions content/docs/glmmr/model/model_examples.md
@@ -1,61 +1,7 @@
---
title: Coding the model
title: Model Examples
weight: 1
---

# A `Model` object
The `glmmrBase` package defines the `Model` class, which is built on the [R6](https://r6.r-lib.org/articles/Introduction.html) class system. R6 classes are object orientated, which means that when you create a new object of an R6 class, it has a set of methods and objects that "belong" to it. In the case of the `Model` class, it generates and stores all the relevant matrices for the GLMM internally. One can then call functions that belong the object to access or use the stored data. We will demonstrate this below.

The arguments to create a new `Model` object are:
* `formula`: A fomrula specifying the GLMM, see [Formula specification](model_specification). This can be omitted and separate formula specified to the arguments to `covariance` and `mean` below.
* `covariance`: An optional list. The list can contain a formula, parameter values, and data.
* `mean`: An optional list. The list can contain a formula, parameter values, and data.
* `var_par`: Optionally, the (starting) value of the scale parameter.
* `data`: A data frame with the data for the model. This can be omitted if data are provided separately to the `mean` and `covariance` arguments via their lists.
* `family`: An R family.
We discuss each of these below and then provide some examples.

## Formula
The formula for the model. The formula can either be a single formula including both fixed and random effects, or each component can be specified separately in the argument for the mean and covariance. Separate specifications may be desirable if one wants to quick change the mean or covariance separately in an existing `Model` object. See [Formula specification](model_specification) for more details.

## Family and link function
The package currently supports the following families and link functions, which are specified using the standard R families in the `stats` package. Link functions are specified in the same way, for example `binomial(link="logit")`.

| Family | Link functions | code |
|--------|-------------------------|--------------|
| Gaussian | Identity, log | `gaussian()` |
| Binomial | Logit, log, identity | `binomial()` |
| Poisson | Log, Identity | `poisson()` |
| Gamma | Log, Inverse, Identity| `gamma()` |
| Beta | Logit | `Beta()` |


The Beta family is provided by the package function `Beta()`, which generates a barebones list specifying the family and link. We use a mean-variance parameterisation of the Beta family. The likelihood is:

$$
f(y_i | \mu_i, \phi) = \frac{y_i^{\mu_i\phi - 1}(1-y_i)^{(1-\mu_i)\phi - 1}}{B(\mu_i\phi, (1-\mu_i)\phi)}
$$

where $B()$ is the Beta function, and we use logit link

$$
\log\left( \frac{\mu_i}{1-\mu_i} \right) = \mathbf{x}_i\beta + \mathbf{z}_i \mathbf{u}
$$

We similarly use a mean-variance parameterisation for the Gamma regression function:

$$
f(y_i | \mu_i, \nu) = \frac{1}{\Gamma(\nu)}\left( \frac{\nu y_i}{\mu_i} \right)^\nu \exp \left( -\frac{\nu y_i}{\mu_i} \right) \frac{1}{y}
$$

where we also provide logit, inverse, and identity link functions for the specification of $\mu_i$.

## Data
Data are required to build the matrices and perform the calculations. When we want to analyse a study design prior to data being collected we will have to create our own covariate data for this purpose. We provide several useful functions to create `data.frame`s with different structures to support generating dummy data, see [Generating data](../creating_data).

## Parameter values



# Examples

A set of examples to be linked here
7 changes: 7 additions & 0 deletions content/docs/glmmr/model/model_fitting.md
@@ -0,0 +1,7 @@
---
title: Model fitting
weight: 1
---

# Model fitting
The package offers several methods for maximum likelihood model fitting based around two approaches: Markov Chain Monte Carlo Maximum Likelihood (MCML) and Laplace approximation. Some of these methods replicate abilities found in other packages and software, which are listed on the software page. A description of the algorithms can be found on the algorithms page, and multiple examples can be found linked in this submenu.
60 changes: 3 additions & 57 deletions content/docs/glmmr/model/model_functions.md
@@ -1,61 +1,7 @@
---
title: Model Class Specification
title: Model Class Functions
weight: 1
---

# `Model` class specification
The `glmmrBase` package defines the `Model` class, which is built on the [R6](https://r6.r-lib.org/articles/Introduction.html) class system. R6 classes are object orientated, which means that when you create a new object of an R6 class, it has a set of methods and objects that "belong" to it. In the case of the `Model` class, it generates and stores all the relevant matrices for the GLMM internally. One can then call functions that belong the object to access or use the stored data. We discuss the

The arguments to create a new `Model` object are:
* `formula`: A fomrula specifying the GLMM, see [Formula specification](model_specification). This can be omitted and separate formula specified to the arguments to `covariance` and `mean` below.
* `covariance`: An optional list. The list can contain a formula, parameter values, and data.
* `mean`: An optional list. The list can contain a formula, parameter values, and data.
* `var_par`: Optionally, the (starting) value of the scale parameter.
* `data`: A data frame with the data for the model. This can be omitted if data are provided separately to the `mean` and `covariance` arguments via their lists.
* `family`: An R family.
We discuss each of these below. Examples are provided [here](model_examples).

## Formula
The formula for the model. The formula can either be a single formula including both fixed and random effects, or each component can be specified separately in the argument for the mean and covariance. Separate specifications may be desirable if one wants to quick change the mean or covariance separately in an existing `Model` object. See [Formula specification](model_specification) for more details.

## Family and link function
The package currently supports the following families and link functions, which are specified using the standard R families in the `stats` package. Link functions are specified in the same way, for example `binomial(link="logit")`.

| Family | Link functions | code |
|--------|-------------------------|--------------|
| Gaussian | Identity, log | `gaussian()` |
| Binomial | Logit, log, identity | `binomial()` |
| Poisson | Log, Identity | `poisson()` |
| Gamma | Log, Inverse, Identity| `gamma()` |
| Beta | Logit | `Beta()` |


The Beta family is provided by the package function `Beta()`, which generates a barebones list specifying the family and link. We use a mean-variance parameterisation of the Beta family. The likelihood is:

$$
f(y_i | \mu_i, \phi) = \frac{y_i^{\mu_i\phi - 1}(1-y_i)^{(1-\mu_i)\phi - 1}}{B(\mu_i\phi, (1-\mu_i)\phi)}
$$

where $B()$ is the Beta function, and we use logit link

$$
\log\left( \frac{\mu_i}{1-\mu_i} \right) = \mathbf{x}_i\beta + \mathbf{z}_i \mathbf{u}
$$

We similarly use a mean-variance parameterisation for the Gamma regression function:

$$
f(y_i | \mu_i, \nu) = \frac{1}{\Gamma(\nu)}\left( \frac{\nu y_i}{\mu_i} \right)^\nu \exp \left( -\frac{\nu y_i}{\mu_i} \right) \frac{1}{y}
$$

where we also provide logit, inverse, and identity link functions for the specification of $\mu_i$.

## Data
Data are required to build the matrices and perform the calculations. When we want to analyse a study design prior to data being collected we will have to create our own covariate data for this purpose. We provide several useful functions to create `data.frame`s with different structures to support generating dummy data, see [Generating data](../creating_data).

## Parameter values



# Examples

# `Model` class functions
A full list of functions to be added here.
10 changes: 8 additions & 2 deletions content/docs/glmmr/model/model_specification.md
Expand Up @@ -35,10 +35,16 @@ $\vert . \vert$ is the Euclidean distance. $K_a$ is the modified Bessel function
*Variable $y$ is defined as $x/r$ where $r$ is the effective range. For the compactly supported functions $d$ is the number of dimensions in `x`.
**Permissible in one or two dimensions. ***Only permissible in one dimension. ****Permissible in up to three dimensions.

One combines functions to provide the desired covariance function. For example, for a stepped-wedge cluster trial we could consider the standard specification with an exchangeable random effect for the cluster level, and a separate exchangeable random effect for the cluster-period, which would be `~(1|gr(j))+(1|gr(j,t))`. Alternatively, we could consider an autoregressive cluster-level random effect that decays exponentially over time so that, for persons $i$ in cluster $j$ at time $t$, $Cov(y_{ijt},y_{i'jt}) = \theta_1$, for $i\neq i'$, $Cov(y_{ijt},y_{i'jt'}) = \theta_1 \theta_2^{\vert t-t' \vert}$ for $t \neq t'$, and $Cov(y_{ijt},y_{i'j't}) = 0$ for $j \neq j'$. This function would be specified as `~(1|gr(j)*ar1(t))`.
One combines functions to provide the desired covariance function. For example, for a stepped-wedge cluster trial we could consider the standard specification with an exchangeable random effect for the cluster level, and a separate exchangeable random effect for the cluster-period, which would be `~(1|gr(j))+(1|gr(j,t))`. Alternatively, we could consider an autoregressive cluster-level random effect that decays exponentially over time so that, for persons $i$ in cluster $j$ at time $t$, $Cov(y_{ijt},y_{i'jt}) = \theta_1$, for $i\neq i'$, $Cov(y_{ijt},y_{i'jt'}) = \theta_1 \theta_2^{\vert t-t' \vert}$ for $t \neq t'$, and $Cov(y_{ijt},y_{i'j't}) = 0$ for $j \neq j'$. This function would be specified as `~(1|gr(j)*ar(t))`.

The above elements can be combined to create relatively complex random effects structures such as `(x|gr(j))+(1|fexp(x,y)*ar(t))`. The current version does not yet support correlated random effects terms, but this is planned for a future version.
The above elements can be combined to create relatively complex random effects structures such as `(x|gr(j))+(1|fexp(x,y)*ar(t))`. The current version does not yet support correlated random effects terms, but this is planned for a future version. Additional functions can be added on request.

## Adding the fixed effects
The fixed effects are specified in the same was as other R packages and the random effects can just be added onto the end. For example, to continue our stepped-wedge cluster trial example, we may want to include fixed effect indicators for each time period `t`, and indicator for the intervention `int`, and remove the intercept: `~ factor(t)+ int - 1 + (1|gr(j)*ar1(t))`.

We can also specify non-linear fixed effects models and include desired parameter names in the formula. For example, the fixed effect specification
$$$
\eta_i = \beta_0 + \beta_1*int_i + \beta_2\exp(\beta_3 x_i)
$$$
can be written as `~ int + beta_2*exp(beta_3 * x)`. The interpreter assumes that any token (i.e. string of characters) that isn't the name of a variable or function is the name of a parameter. The formula is stored as a sequence of mathematical operations. An auto-differentiation scheme is used to calculate the relevant derivative for model fitting and other operations.

7 changes: 7 additions & 0 deletions content/docs/glmmr/model/optimal_designs.md
@@ -0,0 +1,7 @@
---
title: Model Class Functions
weight: 1
---

# c-Optimal experimental designs
A full description of the algorithms and examples to be added here.
12 changes: 3 additions & 9 deletions content/menu/index.md
Expand Up @@ -11,11 +11,13 @@ headless: true
- [Model Class Functions]({{< relref "/docs/glmmr/model/model_functions" >}})
- [Examples]({{< relref "/docs/glmmr/model/model_examples" >}})
- [Generating data]({{< relref "/docs/glmmr/creating_data" >}})
- [Model fitting]({{< relref "/docs/glmmr/model/model_fitting" >}})
- [Optimal designs]({{< relref "/docs/glmmr/model/optimal_designs" >}})
<br />

- [**Cluster Trials**]({{< relref "/docs/cluster" >}})
- [Power calculations]({{< relref "/docs/cluster/power" >}})
- [Estimating power]({{< relref "/docs/cluster/power/approximations" >}})
- [Estimating power example]({{< relref "/docs/cluster/power/approximations" >}})
- [Simulation]({{< relref "/docs/cluster/power/simulation" >}})
- [Comparison of methods]({{< relref "/docs/cluster/power/comparisons" >}})
<br />
Expand All @@ -26,12 +28,4 @@ headless: true
- [Girling Algorithm]({{< relref "/other/girlingalgo" >}})
<br />

- **Shortcodes**
- [Buttons]({{< relref "/docs/shortcodes/buttons" >}})
- [Columns]({{< relref "/docs/shortcodes/columns" >}})
- [Expand]({{< relref "/docs/shortcodes/expand" >}})
- [Hints]({{< relref "/docs/shortcodes/hints" >}})
- [KaTex]({{< relref "/docs/shortcodes/katex" >}})
- [Mermaid]({{< relref "/docs/shortcodes/mermaid" >}})
- [Tabs]({{< relref "/docs/shortcodes/tabs" >}})
<br />

0 comments on commit f8870aa

Please sign in to comment.