R:为什么插入符中的train()函数返回空模型(调用:NULL)?

时间:2018-11-14 06:47:21

标签: r r-caret

例如,我使用火车来建立如下所示的逻辑回归:

min

它将显示

library(caret)

n <- 1000
df <- data.frame(x1=runif(n, min=0, max=1),
                 x2=runif(n, min=0, max=1),
                 y = rep(c('N', 'Y'), n/2)[sample(n, n)])
lmFit <- train(y ~ ., data=df, method='glm', family = binomial)
summary(lmFit)

是否有设置使其显示正确的Call: NULL .... 呼叫而不是glm? 谢谢。

1 个答案:

答案 0 :(得分:2)

我看不到要提供给摘要的设置,因为summary.train方法会剥离调用值:

> class(lmFit)
[1] "train"         "train.formula"
> getAnywhere(summary.train)
A single object matching ‘summary.train’ was found
It was found in the following places
  registered S3 method for summary from namespace caret
  namespace:caret
with value

function (object, ...) 
summary(object$finalModel, ...)
<bytecode: 0x1bc4f820>
<environment: namespace:caret>

> names(lmFit)
 [1] "method"       "modelInfo"    "modelType"    "results"      "pred"         "bestTune"     "call"        
 [8] "dots"         "metric"       "control"      "finalModel"   "preProcess"   "trainingData" "resample"    
[15] "resampledCM"  "perfNames"    "maximize"     "yLimits"      "times"        "levels"       "terms"       
[22] "coefnames"    "xlevels"

您可以对该函数进行变形,使其使用cat输出调用,或将其作为summary对象的一部分返回,或同时返回。或者,您可以将其添加到脚本中:

> lmFit$call
train.formula(form = y ~ ., data = df, method = "glm", family = binomial)