1

私は優れた R パッケージのキャレットを使用しており、複数のトレーニング データ セットのリストに対して train 関数を実行したいと考えています。さて、train 関数のドキュメントには、データ引数はデータ フレームである必要があると書かれていることがわかりました。そのため、私がやろうとしていることは単純に不可能であり、これはキャレットの拡張として提案されたほうがよいかもしれませんが、誰かがこれをやろうとしたかどうかを確認してください。

説明のためにソナー データを使用して、2 つのデータ フレームで構成されるリスト (both という名前) を作成しました。それぞれが別個のトレーニング データセットです。次に、リスト内の各要素に train 関数を適用するために mapply を使用しています。残念ながら、恐ろしい結果が得られています。具体的には、pls1.3..A[[2]] のメトリックが pls1.3..B2 のメトリックと同じになることを望んでいました。ご覧のとおり、そうではありません。奇妙なことに、pls1.3..A[[1]] は pls1.3..B1 と一致します。私が間違っていることは明らかですか、それとも(今は)不可能なのでしょうか?(1.4 GHz Intel Core i5 Mac で R 3.1.1 を実行しています。)

再現可能なコード (およびコメントアウトされた出力) は次のとおりです。

    require(doMC)
    registerDoMC(cores = 2) 

    library(caret) 
    library(mlbench) 
    data(Sonar) 
    set.seed(1234) 
    inTrain <- createDataPartition(y = Sonar$Class, 
                                   p = .75, 
                                    list = FALSE) 

    training <- Sonar[ inTrain,] 
    training2  <- Sonar[-inTrain,] 

    both <- list(training, training2) 
    #both_test <- list(training[c(1:100),], training2[c(1:35),]) #SILLY test data for functionality testing only 

    set.seed(1234) 

    labels <- list() 
    for(i in 1:length(both)) { 
        labels[i] <- list(both[[i]]$Class) 
        } 

    #NEW CODE -- ADDED BASED ON @Josh W's comment -- removing the label (Class) variable from the feature matrix
    both <- lapply(both, function(x) {
        subset(x[,c(1:60)])
        })

    #NEW CODE -- changed from using the formula implementation of caret to the x (feature matrix), y (label/outcome vector)

    pls1.3..A <- mapply(function(x,y) train(x, y, method = "pls", preProc = c("center", "scale")), x = both, y = labels, SIMPLIFY = FALSE) 
    pls1.3..A 

    #[[1]]
    #Partial Least Squares 

    #157 samples
    # 60 predictor
    #  2 classes: 'M', 'R' 

    #Pre-processing: centered, scaled 
    #Resampling: Bootstrapped (25 reps) 

    #Summary of sample sizes: 157, 157, 157, 157, 157, 157, ... 

    #Resampling results across tuning parameters:

    #  ncomp  Accuracy   Kappa      Accuracy SD  Kappa SD  
    #  1      0.6889679  0.3756821  0.06015197   0.11605511
    #  2      0.7393776  0.4742204  0.04962609   0.09775688
    #  3      0.7410997  0.4793703  0.04856698   0.09412599

    #Accuracy was used to select the optimal model using  the largest value.
    #The final value used for the model was ncomp = 3. 

    #[[2]]
    #Partial Least Squares 

    #51 samples
    #60 predictors
    # 2 classes: 'M', 'R' 

    #Pre-processing: centered, scaled 
    #Resampling: Bootstrapped (25 reps) 

    #Summary of sample sizes: 51, 51, 51, 51, 51, 51, ... 

    #Resampling results across tuning parameters:

    #  ncomp  Accuracy   Kappa      Accuracy SD  Kappa SD 
    #  1      0.6452693  0.2929118  0.08076455   0.1525176
    #  2      0.6468405  0.2902136  0.09686340   0.1790924
    #  3      0.6559113  0.3087227  0.08025215   0.1547317

    #Accuracy was used to select the optimal model using  the largest value.
    #The final value used for the model was ncomp = 3.          

    set.seed(1234)
    pls1.3..B1 <- train(both[[1]],
                    labels[[1]],
                    method = "pls",
                    preProc = c("center", "scale"))
    pls1.3..B1
    #Partial Least Squares 

    #157 samples
    # 60 predictor
    #  2 classes: 'M', 'R' 

    #Pre-processing: centered, scaled 
    #Resampling: Bootstrapped (25 reps) 

    #Summary of sample sizes: 157, 157, 157, 157, 157, 157, ... 

    #Resampling results across tuning parameters:

    #  ncomp  Accuracy   Kappa      Accuracy SD  Kappa SD  
    #  1      0.6889679  0.3756821  0.06015197   0.11605511
    #  2      0.7393776  0.4742204  0.04962609   0.09775688
    #  3      0.7410997  0.4793703  0.04856698   0.09412599

    #Accuracy was used to select the optimal model using  the largest value.
    #The final value used for the model was ncomp = 3. 

    set.seed(1234)
    pls1.3..B2 <- train(both[[2]],
                    labels[[2]],
                    method = "pls",
                    preProc = c("center", "scale"))
    pls1.3..B2

    #Partial Least Squares 

    #51 samples
    #60 predictors
    # 2 classes: 'M', 'R' 

    #Pre-processing: centered, scaled 
    #Resampling: Bootstrapped (25 reps) 

    #Summary of sample sizes: 51, 51, 51, 51, 51, 51, ... 

    #Resampling results across tuning parameters:

    #  ncomp  Accuracy   Kappa      Accuracy SD  Kappa SD 
    #  1      0.6127279  0.2518488  0.11925682   0.1959400
    #  2      0.6792163  0.3618657  0.09386771   0.1776549
    #  3      0.6673662  0.3343716  0.07524373   0.1476405

    #Accuracy was used to select the optimal model using  the largest value.
    #The final value used for the model was ncomp = 2.  
4

1 に答える 1