MeanFunction | R Documentation |
R6 Class representing a mean function function and data
R6 Class representing a mean function function and data
For the generalised linear mixed model
Y \sim F(μ,σ)
μ = h^-1(Xβ + Zγ)
γ \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.
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}|δ)=β_0 + τ_t + β_1 d_{jt} + z_{ijt}δ where τ_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. Available functions are the same as for the covariance functions see Covariance. The user can add additional functions by specifying a new function that takes as an input a named list with elements data and pars, and outputs a matrix with the linearised components. The function name must begin with 'd', e.g. the function to provide a first order approximation to the exponential function (see fexp) is named 'dfexp'.
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 β 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
n()
Returns the number of observations
MeanFunction$n()
...
ignored
The number of observations in the model
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 = binomial() ) mf1$n()
check()
Checks if any changes have been made and updates
Checks if any changes have been made and updates, usually called automatically.
MeanFunction$check(verbose = TRUE)
verbose
Logical whether to report if any changes detected.
NULL
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 = binomial() ) mf1$parameters <- c(0,0) mf1$check()
new()
Create a new MeanFunction object
MeanFunction$new( formula, data, family, parameters, verbose = FALSE, random_function = NULL, treat_var = NULL )
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 β 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.
A MeanFunction object
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 = binomial() )
print()
Prints details about the object
MeanFunction$print()
...
ignored
colnames()
Returns or replaces the column names of the data in the object
MeanFunction$colnames(names = NULL)
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
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 = binomial() ) mf1$colnames(c("cluster","time","individual","treatment")) mf1$colnames()
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.
MeanFunction$subset_rows(index)
index
Rows of the data to keep
NULL
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 = binomial() ) mf1$subset_rows(1:20)
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.
MeanFunction$subset_cols(index)
index
Columns of X to keep
NULL
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 = binomial() ) mf1$subset_cols(1:2)
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
MeanFunction$rerandomise()
...
ignored
Nothing is returned, the X matrix is updated
clone()
The objects of this class are cloneable with this method.
MeanFunction$clone(deep = FALSE)
deep
Whether to make a deep clone.
## ------------------------------------------------ ## 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 = 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 = 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 = 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 = 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 = 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 = binomial() ) mf1$subset_cols(1:2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.