MeanFunction {glmmrBase}R Documentation

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

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, and name the parameters. The resulting X matrix is then a matrix of first-order partial derivatives. For example, one can specify '~ int + b_1*exp(b_2*x)'.

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.

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.

offset

An optional vector specifying the offset values

X

the fixed effects design matrix

Methods

Public methods


Method n()

Returns the number of observations

Usage
MeanFunction$n()
Arguments
...

ignored

Returns

The number of observations in the model

Examples
\dontshow{
setParallel(FALSE) # for the CRAN check
}
df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 2, 'int'] <- 1
mf1 <- MeanFunction$new(formula = ~ int ,
                        data=df,
                        parameters = c(-1,1)
                        )
mf1$n()

Method new()

Create a new MeanFunction object

Usage
MeanFunction$new(
  formula,
  data,
  parameters = NULL,
  offset = 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.

offset

A vector of offset values (optional)

verbose

Logical indicating whether to report detailed output

Returns

A MeanFunction object

Examples
\dontshow{
setParallel(FALSE) # for the CRAN check
}
df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 2, '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)
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 attempts to replace the current column names of the data

Examples
\dontshow{
setParallel(FALSE) # for the CRAN 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$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
\dontshow{
setParallel(FALSE) # for the CRAN 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$subset_rows(1:20) 

Method linear_predictor()

Returns the linear predictor

Returns the linear predictor, X * beta

Usage
MeanFunction$linear_predictor()
Returns

A vector


Method any_nonlinear()

Returns a logical indicating whether the mean function contains non-linear functions of model parameters. Mainly used internally.

Usage
MeanFunction$any_nonlinear()
Returns

None. Called for effects


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.

Examples


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


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

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


df <- nelder(~(cl(4)*t(5)) > ind(5))
df$int <- 0
df[df$cl <= 2, '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) 

[Package glmmrBase version 0.9.2 Index]