buildcustom {buildmer}R Documentation

Use buildmer to perform stepwise elimination using a custom fitting function

Description

Use buildmer to perform stepwise elimination using a custom fitting function

Usage

buildcustom(
  formula,
  data = NULL,
  fit = function(p, formula) stop("'fit' not specified"),
  crit = function(p, ref, alt) stop("'crit' not specified"),
  elim = function(x) stop("'elim' not specified"),
  REML = FALSE,
  buildmerControl = buildmerControl()
)

Arguments

formula

See the general documentation under buildmer-package

data

See the general documentation under buildmer-package

fit

A function taking two arguments, of which the first is the buildmer parameter list p and the second one is a formula. The function must return a single object, which is treated as a model object fitted via the provided formula. The function must return an error ('stop()') if the model does not converge.

crit

A function taking one argument and returning a single value. The argument is the return value of the function passed in fit, and the returned value must be a numeric indicating the goodness of fit, where smaller is better (like AIC or BIC).

elim

A function taking one argument and returning a single value. The argument is the return value of the function passed in crit, and the returned value must be a logical indicating if the small model must be selected (return TRUE) or the large model (return FALSE).

REML

A logical indicating if the fitting function wishes to distinguish between fits differing in fixed effects (for which p$reml will be set to FALSE) and fits differing only in the random part (for which p$reml will be TRUE). Note that this ignores the usual semantics of buildmer's optional REML argument, because they are redundant: if you wish to force REML on or off, simply code it so in your custom fitting function.

buildmerControl

Control arguments for buildmer — see the general documentation under buildmerControl

See Also

buildmer-package

Examples

## Use \code{buildmer} to do stepwise linear discriminant analysis
library(buildmer)
migrant[,-1] <- scale(migrant[,-1])
flipfit <- function (p,formula) {
    # The predictors must be entered as dependent variables in a MANOVA
    # (i.e. the predictors must be flipped with the dependent variable)
    Y <- model.matrix(formula,migrant)
    m <- lm(Y ~ 0+migrant$changed)
    # the model may error out when asking for the MANOVA
    test <- try(anova(m))
    if (inherits(test,'try-error')) test else m
}
crit.F <- function (p,a,b) { # use whole-model F
    pvals <- anova(b)$'Pr(>F)' # not valid for backward!
    pvals[length(pvals)-1]
}
crit.Wilks <- function (p,a,b) {
    if (is.null(a)) return(crit.F(p,a,b)) #not completely correct, but close as F approximates X2
    Lambda <- anova(b,test='Wilks')$Wilks[1]
    p <- length(coef(b))
    n <- 1
    m <- nrow(migrant)
    Bartlett <- ((p-n+1)/2-m)*log(Lambda)
    pchisq(Bartlett,n*p,lower.tail=FALSE)
}

# First, order the terms based on Wilks' Lambda
model <- buildcustom(changed ~ friends.nl+friends.be+multilingual+standard+hearing+reading+
       attention+sleep+gender+handedness+diglossic+age+years,buildmerControl=list(
       fit=flipfit,crit=crit.Wilks,direction='order'))
# Now, use the six most important terms (arbitrary choice) in the LDA
if (require('MASS')) {
model <- lda(changed ~ diglossic + age + reading + friends.be + years + 
       multilingual,data=migrant)
}

[Package buildmer version 2.11 Index]