繁体   English   中英

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

[英]R: Why train() function in caret returns null model (Call: NULL)?

例如,我使用train构建如下的逻辑回归:

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调用,而不是NULL 谢谢。

我看不到要提供给摘要的设置,因为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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM