用于区分现有和非现有对象的条件表达式?

时间:2014-02-13 00:58:34

标签: r

Package Caret,

我有一个我称之为model1的模型,model1根据数据集可以有三个结果:

model1 <- train(X[train,], Y[train], method='bag', trControl=myControl, preProcess=PP)

model1产生RMSE结果。

model1
Pre-processing: centered, scaled 
Resampling: Cross-Validation (2 fold) 

Summary of sample sizes: 39, 39, 39, 39, 39, 39, ... 

Resampling results across tuning parameters:

nprune  RMSE    Rsquared  RMSE SD  Rsquared SD
2       0.0419  0.0556    0.00279  0.0392     
7       0.0419  0.0549    0.00244  0.028      
12      0.042   0.0417    0.00214  0.0196     

Tuning parameter 'degree' was held constant at a value of 1
RMSE was used to select the optimal model using  the smallest value.
The final values used for the model were nprune = 7 and degree = 1.

model1生成一个带NA的RMSE。

model1
Pre-processing: centered, scaled 
Resampling: Cross-Validation (2 fold) 

Summary of sample sizes: 32, 33, 32, 32, 32, 32, ... 

Resampling results across tuning parameters:

nprune  RMSE    Rsquared  RMSE SD  Rsquared SD
NA       NA       NA       NA          NA     
NA       NA       NA       NA          NA  
NA       NA       NA       NA          NA    

model1产生如下错误并产生一个不存在的model1

+ Fold1.Rep1: vars=3 
model fit failed for Fold1.Rep1: vars=3 Error in bag.default(trainX, trainY, vars =    
tuneValue$.vars, ...) : 
entrada en evaluacion: recursivo por defecto o problemas anteriores?

model1
Error: objeto 'model1' no encontrado

我希望能够使用条件表达式来区分三个结果。我可以在NA的RMSE或具有数字结果的RMSE之间进行。

if(model1$results$RMSE[1]=="NA")0 else model1

然而,当model1在第三种情况下失败时,由于模型不存在,我无法找到将任何条件表达式组合在一起的方法,但是我希望能够在不存在时区分model1,在它生成时模型1 RMSE结果和model1产生带NA的RMSE时。您是否碰巧知道我是否可以通过条件表达式区分现有对象与现有对象?

谢谢

2 个答案:

答案 0 :(得分:2)

您正在寻找existsis.na

if (!exists("model1")) {
  # Model not built
} else if (is.na(model1$results$RMSE[1])) {
  # Model built with NA
} else {
  # Model built properly
}

答案 1 :(得分:1)

不确定,但也许exists"results" %in% names(model1)正是您要找的。