简体   繁体   English

如何在函数中绘制插入符号训练对象

[英]How to plot a caret train object in a function

I want to write e function that trains a regression model using the train function of the caret package and plots the resampling profile. 我想编写一个e函数,该函数使用caret packagetrain函数来train回归模型并绘制重采样配置文件。 My problem is that no plot is shown and no error is thrown. 我的问题是没有显示任何图并且没有引发任何错误。 This should be a reproducible example: 这是一个可重现的示例:

trFun <- function(){
  library( caret )
  library( mlbench )
  data( BostonHousing )

  lmFit <- train(
    medv ~ .,
    data=BostonHousing,
    method="pls"
  )

  #trellis.par.set( caretTheme() )
  plot( lmFit )

  lmFit
}

fit <- trFun()

plot( fit )

So plot( lmfit ) inside trFun doesn't plot anything, while plot( fit ) outside the function does the plot. 所以, plot( lmfit )trFun没有什么阴谋,而plot( fit )外的函数做的情节。 The trellis.par.set( caretTheme() ) statement seems to be irrelevant. trellis.par.set( caretTheme() )语句似乎无关紧要。

My question is: how can I plot the resampling profile inside trFun ? 我的问题是:如何在trFun绘制重采样配置文件?

This is the sessionInfo() output: 这是sessionInfo()输出:

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.4.3 tools_3.4.3    yaml_2.1.16

Try this: 尝试这个:

trFun <- function() {
  library( caret )
  library( mlbench )
  data( BostonHousing )

  lmFit <- train(
    medv ~ .,
    data=BostonHousing,
    method="pls"
  )

  #trellis.par.set( caretTheme() )
  print(plot( lmFit ))

  lmFit
}

trFun()

在此处输入图片说明

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

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