我正在尝试调整glm
的{{1}}方法,以用于广义线性混合效果模型。我使用以下代码来设置函数:
caret::train
这是对`GLMERmod <- list(type="Classification", library="lme4", loop=NULL)
parameters <- data.frame(parameter="parameter", class="character",
label="parameter")
GLMERmod$parameters <- parameters
grid <- function (x, y, len = NULL, search = "grid"){
data.frame(parameter = "none")
}
GLMERmod$grid <- grid
fit <-
function (x, y, wts, param, lev, last, classProbs, ...)
{
dat <- if (is.data.frame(x))
x
else as.data.frame(x)
dat$.outcome <- y
if (length(levels(y)) > 2)
stop("glm models can only use 2-class outcomes")
theDots <- list(...)
if (!any(names(theDots) == "family")) {
theDots$family <- if (is.factor(y))
binomial()
else gaussian()
}
if (!is.null(wts))
theDots$weights <- wts
modelArgs <- c(list(formula = as.formula(".outcome ~ . + (1 | IDNO)"),
data = dat, control=glmerControl(optimizer="bobyqa"), nAGQ=10,
theDots))
out <- do.call("glmer", modelArgs)
out$call <- NULL
out
}
GLMERmod$fit <- fit
predict <-
function (modelFit, newdata, submodels = NULL)
{
if (!is.data.frame(newdata))
newdata <- as.data.frame(newdata)
if (modelFit$problemType == "Classification") {
probs <- predict(modelFit, newdata, type = "response")
out <- ifelse(probs < 0.5, modelFit$obsLevel[1],
modelFit$obsLevel[2])
}
else {
out <- predict(modelFit, newdata, type = "response")
}
out
}
GLMERmod$predict <- predict
prob <-
function (modelFit, newdata, submodels = NULL)
{
if (!is.data.frame(newdata))
newdata <- as.data.frame(newdata)
out <- predict(modelFit, newdata, type = "response")
out <- cbind(1 - out, out)
dimnames(out)[[2]] <- modelFit$obsLevels
out
}
GLMERmod$prob <- prob
varImp <-
function (object, ...)
{
values <- summary(object)$coef
varImps <- abs(values[-1, grep("value$", colnames(values))])
out <- data.frame(varImps)
colnames(out) <- "Overall"
if (!is.null(names(varImps)))
rownames(out) <- names(varImps)
out
}
GLMERmod$varImp <- varImp
predictors <-
function (x, ...)
predictors(x$terms)
GLMERmod$predictors <- predictors
levels <-
function (x)
if (any(names(x) == "obsLevels")) x$obsLevels else NULL
GLMERmod$levels <- levels
trim <-
function (x)
{
x$y = c()
x$model = c()
x$residuals = c()
x$fitted.values = c()
x$effects = c()
x$qr$qr = c()
x$linear.predictors = c()
x$weights = c()
x$prior.weights = c()
x$data = c()
x$family$variance = c()
x$family$dev.resids = c()
x$family$aic = c()
x$family$validmu = c()
x$family$simulate = c()
attr(x$terms, ".Environment") = c()
attr(x$formula, ".Environment") = c()
x
}
GLMERmod$trim <- trim
sort <-
function (x)
x
GLMERmod$sort <- sort`
方法的代码的简单修改。但是,当我运行我的模型(如下所示)时,我收到许多错误消息。
我尝试使用基本的glm
函数,但是存在扩展问题(glmer
会使这很容易处理以及交叉验证和模型诊断)。
train
然后,我尝试将> glmer1 <- glmer(Case.Status ~ . + (1 | IDNO), data=TB_Train.glmer,
family=binomial, control=glmerControl(optimizer="bobyqa"), nAGQ=10)
fixed-effect model matrix is rank deficient so dropping 21 columns /
coefficients
Some predictor variables are on very different scales: consider
rescalingmaxfun < 10 * length(par)^2 is not recommended.
Error in na.fail.default(list(Case.Status = c(2L, 2L, 2L, 2L, 1L, 1L, :
missing values in object
与此代码一起使用:
train
我收到此错误:
GLMER <- train(Case.Status ~ . + (1 | IDNO), data=TB_Train.glmer,
method=GLMERmod, trControl=trainControl(method="none", classProbs = TRUE),
preProc = c("center", "scale"), metric="ROC")
将IDNO变量转换为数字:
�|� not meaningful for factors
Show Traceback
Error in na.fail.default(list(Case.Status = c(2L, 2L, 2L, 2L, 1L, 1L, :
missing values in object
拼出所有变量以避免零差异警告,但仍然:
These variables have zero variances: 1 | IDNOTRUEthe condition has length > 1
and only the first element will be usedfixed-effect model matrix is rank
deficient so dropping 1 column / coefficient
Show Traceback
Error: inherits(family, "family") is not TRUE
任何人都知道这些错误来自哪里以及我如何解决它们?