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 |
data |
See the general documentation under |
fit |
A function taking two arguments, of which the first is the |
crit |
A function taking one argument and returning a single value. The argument is the return value of the function passed in |
elim |
A function taking one argument and returning a single value. The argument is the return value of the function passed in |
REML |
A logical indicating if the fitting function wishes to distinguish between fits differing in fixed effects (for which |
buildmerControl |
Control arguments for buildmer — see the general documentation under |
See Also
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)
}