Pau*_*tos 4 r gam cross-validation mgcv r-caret
我想用插入符号交叉验证GAM模型.我的GAM模型有一个二元结果变量,纬度和经度坐标对的各向同性平滑,然后是线性预测变量.使用mgcv时的典型语法是:
gam1 <- gam( y ~ s(lat , long) + x1 + x2, family = binomial(logit) )
Run Code Online (Sandbox Code Playgroud)
我不太确定如何使用插入符号中的train函数指定此模型.这或多或少是我的语法:
cv <- train(y ~ lat + long + x1 + x2,
data = data,
method = "gam",
family = "binomial",
trControl = trainControl(method = "LOOCV", number=1, repeats=),
tuneGrid = data.frame(method = "GCV.Cp", select = FALSE))
Run Code Online (Sandbox Code Playgroud)
问题是我只想平滑lat和long,并且x1和x2被视为线性.
谢谢!
李哲源*_*李哲源 10
看到有人mgcv在外面使用是非常有趣的mgcv.经过一番研究,我在这里挫败你:使用mgcvwith caret是一个坏主意,至少在目前的支持下caret.
如果你使用的话,让我问你几个基本问题caret:
te或指定张量积样条ti?如果你想知道caret::train在做什么method = "gam",请查看它的拟合程序:
getModelInfo(model = "gam", regex = FALSE)$gam$fit
function(x, y, wts, param, lev, last, classProbs, ...) {
dat <- if(is.data.frame(x)) x else as.data.frame(x)
modForm <- caret:::smootherFormula(x)
if(is.factor(y)) {
dat$.outcome <- ifelse(y == lev[1], 0, 1)
dist <- binomial()
} else {
dat$.outcome <- y
dist <- gaussian()
}
modelArgs <- list(formula = modForm,
data = dat,
select = param$select,
method = as.character(param$method))
## Intercept family if passed in
theDots <- list(...)
if(!any(names(theDots) == "family")) modelArgs$family <- dist
modelArgs <- c(modelArgs, theDots)
out <- do.call(getFromNamespace("gam", "mgcv"), modelArgs)
out
}
Run Code Online (Sandbox Code Playgroud)
你看到了这modForm <- caret:::smootherFormula(x)条线?那条线是关键,而其他线只是模型调用的常规构造.那么,让我们检查一下GAM公式caret正在构建的内容:
caret:::smootherFormula
function (data, smoother = "s", cut = 10, df = 0, span = 0.5,
degree = 1, y = ".outcome")
{
nzv <- nearZeroVar(data)
if (length(nzv) > 0)
data <- data[, -nzv, drop = FALSE]
numValues <- sort(apply(data, 2, function(x) length(unique(x))))
prefix <- rep("", ncol(data))
suffix <- rep("", ncol(data))
prefix[numValues > cut] <- paste(smoother, "(", sep = "")
if (smoother == "s") {
suffix[numValues > cut] <- if (df == 0)
")"
else paste(", df=", df, ")", sep = "")
}
if (smoother == "lo") {
suffix[numValues > cut] <- paste(", span=", span, ",degree=",
degree, ")", sep = "")
}
if (smoother == "rcs") {
suffix[numValues > cut] <- ")"
}
rhs <- paste(prefix, names(numValues), suffix, sep = "")
rhs <- paste(rhs, collapse = "+")
form <- as.formula(paste(y, rhs, sep = "~"))
form
}
Run Code Online (Sandbox Code Playgroud)
简而言之,它创造了附加的,单变量的光滑.这是GAM首次提出时的经典形式.
为此mgcv,如前所列,您将失去大量控制权.
为了验证这一点,让我为您的案例构建一个类似的示例:
set.seed(0)
dat <- gamSim(eg = 2, scale = 0.2)$data[1:3]
dat$a <- runif(400)
dat$b <- runif(400)
dat$y <- with(dat, y + 0.3 * a - 0.7 * b)
# y x z a b
#1 -0.30258559 0.8966972 0.1478457 0.07721866 0.3871130
#2 -0.59518832 0.2655087 0.6588776 0.13853856 0.8718050
#3 -0.06978648 0.3721239 0.1850700 0.04752457 0.9671970
#4 -0.17002059 0.5728534 0.9543781 0.03391887 0.8669163
#5 0.55452069 0.9082078 0.8978485 0.91608902 0.4377153
#6 -0.17763650 0.2016819 0.9436971 0.84020039 0.1919378
Run Code Online (Sandbox Code Playgroud)
所以我们的目标是适合一个模型:y ~ s(x, z) + a + b.数据y是高斯数据,但这无关紧要; 它不会影响如何caret使用mgcv.
cv <- train(y ~ x + z + a + b, data = dat, method = "gam", family = "gaussian",
trControl = trainControl(method = "LOOCV", number=1, repeats=1),
tuneGrid = data.frame(method = "GCV.Cp", select = FALSE))
Run Code Online (Sandbox Code Playgroud)
您可以提取最终模型:
fit <- cv[[11]]
Run Code Online (Sandbox Code Playgroud)
那么它使用什么配方?
fit$formula
#.outcome ~ s(x) + s(z) + s(a) + s(b)
Run Code Online (Sandbox Code Playgroud)
看到?除了"添加,单变量"之外,它还将所有内容保留为mgcv::s默认值:默认值bs = "tp",默认值k = 10等.