2014-02-13 47 views
1

包插入符號,現有對象與不存在對象之間的差異條件表達式?

我有一個模型,我稱之爲模型1,模型1可以根據數據集三種結果:

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當它不存在, model1生成RMSE結果,model1生成RMSE時生成NA。你會碰巧知道我是否可以通過條件表達式來區分一個不存在的對象和現有的對象嗎?

謝謝

回答

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

不知道,但也許exists"results" %in% names(model1)是你在找什麼。

相關問題