我真的很喜欢这样的软件包caret
,但是不幸的是,我读到您不能完全指定formula
in gam
。
“当您将Train与该模型一起使用时,您(此时)无法指定gam公式。插入符号具有一个内部函数,该函数根据每个预测变量所具有的唯一水平等来计算公式。换句话说,train当前确定哪个项是平滑的,并且是普通的老式线性主效应。”
来源:https : //stackoverflow.com/questions/20044014/error-with-train-from-caret-package-using-method-gam
但是如果您train
选择平滑项,在这种情况下,无论如何它都会生成您的模型。在这种情况下,默认性能指标是RMSE,但是您可以使用函数的summaryFunction
参数进行更改trainControl
。
我认为LOOCV的主要缺点之一是,当数据集很大时,它会永远花下去。由于您的数据集很小并且可以很快运行,所以我认为这是一个明智的选择。
希望这可以帮助。
library(mgcv)
library(caret)
set.seed(0)
dat <- gamSim(1, n = 400, dist = "normal", scale = 2)
b <- train(y ~ x0 + x1 + x2 + x3,
data = dat,
method = "gam",
trControl = trainControl(method = "LOOCV", number = 1, repeats = 1),
tuneGrid = data.frame(method = "GCV.Cp", select = FALSE)
)
print(b)
summary(b$finalModel)
输出:
> print(b)
Generalized Additive Model using Splines
400 samples
9 predictors
No pre-processing
Resampling:
Summary of sample sizes: 399, 399, 399, 399, 399, 399, ...
Resampling results
RMSE Rsquared
2.157964 0.7091647
Tuning parameter 'select' was held constant at a value of FALSE
Tuning parameter 'method' was held constant at a value of GCV.Cp
> summary(b$finalModel)
Family: gaussian
Link function: identity
Formula:
.outcome ~ s(x0) + s(x1) + s(x2) + s(x3)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.9150 0.1049 75.44 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
edf Ref.df F p-value
s(x0) 5.173 6.287 4.564 0.000139 ***
s(x1) 2.357 2.927 103.089 < 2e-16 ***
s(x2) 8.517 8.931 84.308 < 2e-16 ***
s(x3) 1.000 1.000 0.441 0.506929
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R-sq.(adj) = 0.726 Deviance explained = 73.7%
GCV = 4.611 Scale est. = 4.4029 n = 400