Learn R Programming

glmmrBase (version 0.2.5)

MeanFunction: R6 Class representing a mean function/linear predictor

Description

R6 Class representing a mean function/linear predictor

R6 Class representing a mean function/linear predictor

Arguments

Public fields

formula

model formula for the fixed effects

data

Data frame with data required to build X

parameters

A vector of parameter values for \(\beta\) used for simulating data and calculating covariance matrix of observations for non-linear models.

X

the fixed effects design matrix

Methods


Method n()

Returns the number of observations

Usage

MeanFunction$n()

Arguments

...

ignored

Returns

The number of observations in the model

Examples

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$n()


Method check()

Checks if any changes have been made and updates

Checks if any changes have been made and updates, usually called automatically.

Usage

MeanFunction$check(verbose = TRUE)

Arguments

verbose

Logical whether to report if any changes detected.

Returns

NULL

Examples

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$parameters <- c(0,0)
mf1$check()


Method new()

Create a new MeanFunction object

Usage

MeanFunction$new(formula, data = NULL, parameters = NULL, verbose = FALSE)

Arguments

formula

A formula object that describes the mean function, see Details

data

(Optional) A data frame containing the covariates in the model, named in the model formula

parameters

(Optional) A vector with the values of the parameters \(\beta\) to use in data simulation and covariance calculations. If the parameters are not specified then they are initialised to 0.

verbose

Logical indicating whether to report detailed output

Returns

A MeanFunction object

Examples

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1),
                        )


Method print()

Prints details about the object

Usage

MeanFunction$print()

Arguments

...

ignored


Method update_parameters()

Updates the model parameters

Usage

MeanFunction$update_parameters(parameters, verbose = FALSE)

Arguments

parameters

A vector of parameters for the mean function.

verbose

Logical indicating whether to provide more detailed feedback


Method colnames()

Returns or replaces the column names of the data in the object

Usage

MeanFunction$colnames(names = NULL)

Arguments

names

If NULL then the function prints the column names, if a vector of names, then it attemps to replace the current column names of the data

Examples

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$colnames(c("cluster","time","individual","treatment"))
mf1$colnames()


Method subset_rows()

Keeps a subset of the data and removes the rest

All indices not in the provided vector of row numbers will be removed from both the data and fixed effects design matrix X.

Usage

MeanFunction$subset_rows(index)

Arguments

index

Rows of the data to keep

Returns

NULL

Examples

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$subset_rows(1:20) 


Method subset_cols()

Keeps a subset of the columns of X

All indices not in the provided vector of column numbers will be removed from the fixed effects design matrix X.

Usage

MeanFunction$subset_cols(index)

Arguments

index

Columns of X to keep

Returns

NULL

Examples

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$subset_cols(1:2) 


Method clone()

The objects of this class are cloneable with this method.

Usage

MeanFunction$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Details

For the generalised linear mixed model

$$Y \sim F(\mu,\sigma)$$ $$\mu = h^-1(X\beta + Z\gamma)$$ $$\gamma \sim MVN(0,D)$$

this class defines the fixed effects design matrix X. The mean function is defined by a model formula, data, and parameters. A new instance can be generated with $new(). The class will generate the relevant matrix X automatically. See glmmrBase for a detailed guide on model specification.

Specification of the mean function follows standard model formulae in R. For example for a stepped-wedge cluster trial model, a typical mean model is \(E(y_{ijt}|\delta)=\beta_0 + \tau_t + \beta_1 d_{jt} + z_{ijt}\delta\) where \(\tau_t\) are fixed effects for each time period. The formula specification for this would be `~ factor(t) + int` where `int` is the name of the variable indicating the treatment.

One can also include non-linear functions of variables in the mean function. These are handled in the analyses by first-order approximation.

If not all of `formula`, `data`, and `parameters` are not specified then the linked matrices are not calculated. These options can be later specified, or updated via a Model object. If these arguments are updated or changed then call `self$check()` to update linked matrices. Updating of parameters is automatic if using the `update_parameters()` member function.

Using `update_parameters()` is the preferred way of updating the parameters of the mean or covariance objects as opposed to direct assignment, e.g. `self$parameters <- c(...)`. The function calls check functions to automatically update linked matrices with the new parameters. If using direct assignment, call `self$check()` afterwards.

Examples

Run this code

## ------------------------------------------------
## Method `MeanFunction$n`
## ------------------------------------------------

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$n()

## ------------------------------------------------
## Method `MeanFunction$check`
## ------------------------------------------------

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$parameters <- c(0,0)
mf1$check()

## ------------------------------------------------
## Method `MeanFunction$new`
## ------------------------------------------------

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1),
                        )

## ------------------------------------------------
## Method `MeanFunction$colnames`
## ------------------------------------------------

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$colnames(c("cluster","time","individual","treatment"))
mf1$colnames()

## ------------------------------------------------
## Method `MeanFunction$subset_rows`
## ------------------------------------------------

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$subset_rows(1:20) 

## ------------------------------------------------
## Method `MeanFunction$subset_cols`
## ------------------------------------------------

df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 5, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$subset_cols(1:2) 

Run the code above in your browser using DataLab