Learn R Programming

glmmrBase (version 0.1.2)

MeanFunction: R6 Class representing a mean function function and data

Description

R6 Class representing a mean function function and data

R6 Class representing a mean function function and data

Arguments

Public fields

formula

model formula for the fixed effects

data

Data frame with data required to build X

family

One of the family function used in R's glm functions. See family for details

parameters

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

randomise

A function that generates a new set of values representing the treatment allocation in an experimental study

treat_var

A string naming the column in data that represents the treatment variable in data. Used to identify where to replace allocation when randomiser is used.

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),
                        family = stats::binomial()
                        )
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),
                        family = stats::binomial()
                        )
mf1$parameters <- c(0,0)
mf1$check()


Method new()

Create a new MeanFunction object

Usage

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

Arguments

formula

A formula object that describes the mean function, see Details

data

A data frame containing the covariates in the model, named in the model formula

family

A family object expressing the distribution and link function of the model, see family

parameters

A vector with the values of the parameters \(\beta\) to use in data simulation and covariance calculations

verbose

Logical indicating whether to report detailed output

random_function

A string naming a function in the global environment that produces a vector of data describing a new treatment allocation in an experimental model. When used, the output of this function replaces the column of data named by `treat_var`

treat_var

The name of a column in data (or the name to give a new column) that a random treatment allocation generated by `random_function` replaces.

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),
                        family = stats::binomial()
                        )


Method print()

Prints details about the object

Usage

MeanFunction$print()

Arguments

...

ignored


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),
                        family = stats::binomial()
                        )
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),
                        family = stats::binomial()
                        )
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),
                        family = stats::binomial()
                        )
mf1$subset_cols(1:2) 


Method rerandomise()

Generates a new random allocation

If a randomising function has been provided then a new random allocation will be generated, and will replace the exisitng data at `treat_var` in the X matrix

Usage

MeanFunction$rerandomise()

Arguments

...

ignored

Returns

Nothing is returned, the X matrix is updated


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 family F, link function h, and 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.

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),
                        family = stats::binomial()
                        )
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),
                        family = stats::binomial()
                        )
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),
                        family = stats::binomial()
                        )

## ------------------------------------------------
## 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),
                        family = stats::binomial()
                        )
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),
                        family = stats::binomial()
                        )
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),
                        family = stats::binomial()
                        )
mf1$subset_cols(1:2) 

Run the code above in your browser using DataLab