3

編集: Dwin がコメントで指摘したように、以下のコードは ROC 曲線用ではありません。ROC 曲線は、 (以下で行うように) ではtなく、変動でインデックス付けする必要があります。lambda機会があれば、以下のコードを編集します。

以下は、バイナリの結果を予測する glmnet の ROC 曲線を作成する試みです。以下のコードで、glmnet の結果を近似するマトリックスをシミュレートしました。ご存じのとおり、入力のn x p行列が与えられると、glmnet は、ラムダの 100 の異なる値について予測確率 [$\Pr(y_i = 1)$]のn x 100 行列を出力します。ラムダのさらなる変化が予測力の増加を停止した場合、出力は 100 より狭くなります。以下の glmnet 予測確率のシミュレートされた行列は、250x69 の行列です。

まず、glmnet ROC 曲線をプロットする簡単な方法はありますか? 第二に、そうでない場合、以下のアプローチは正しいと思われますか? 第三に、(1) 偽陽性/真陽性の確率、または (2) 単に観察された偽陽性/真陽性の割合をプロットすることに関心がありますか?

set.seed(06511)

# Simulate predictions matrix
phat = as.matrix(rnorm(250,mean=0.35, sd = 0.12))
lambda_effect = as.matrix(seq(from = 1.01, to = 1.35, by = 0.005))
phat = phat %*% t(lambda_effect)


#Choose a cut-point
t = 0.5

#Define a predictions matrix
predictions = ifelse(phat >= t, 1, 0)

##Simulate y matrix
y_phat = apply(phat, 1, mean) + rnorm(250,0.05,0.10)
y_obs = ifelse(y_phat >= 0.55, 1, 0)

#percentage of 1 observations in the validation set, 
p = length(which(y_obs==1))/length(y_obs)

#   dim(testframe2_e2)

#probability of the model predicting 1 while the true value of the observation is 0, 
apply(predictions, 1, sum)

## Count false positives for each model
## False pos ==1, correct == 0, false neg == -1
error_mat = predictions - y_obs
## Define a matrix that isolates false positives
error_mat_fp = ifelse(error_mat ==1, 1, 0)
false_pos_rate = apply(error_mat_fp, 2,  sum)/length(y_obs)

# Count true positives for each model
## True pos == 2, mistakes == 1, true neg == 0
error_mat2 = predictions + y_obs
## Isolate true positives
error_mat_tp = ifelse(error_mat2 ==2, 1, 0)
true_pos_rate = apply(error_mat_tp, 2,  sum)/length(y_obs)


## Do I care about (1) this probability OR (2) simply the observed rate?
## (1)
#probability of false-positive, 
p_fp = false_pos_rate/(1-p)
#probability of true-positive, 
p_tp = true_pos_rate/p

#plot the ROC, 
plot(p_fp, p_tp)


## (2)
plot(false_pos_rate, true_pos_rate)

これについて SO で 1 つの質問がありますが、答えは大まかで、正しくありませんでした: glmnet lasso ROC charts

4

2 に答える 2

8

ROCRAUC の計算と ROC 曲線のプロットに使用するオプション:

library(ROCR)
library(glmnet)
library(caret)

df <- data.matrix(… ) # dataframe w/ predictor variables & a response variable
                      # col1 = response var; # cols 2:10 = predictor vars

# Create training subset for model development & testing set for model performance testing
inTrain <- createDataPartition(df$ResponsVar, p = .75, list = FALSE)
Train <- df[ inTrain, ]
Test <- df[ -inTrain, ]

# Run model over training dataset
lasso.model <- cv.glmnet(x = Train[,2:10], y = Train[,1], 
                         family = 'binomial', type.measure = 'auc')

# Apply model to testing dataset
Test$lasso.prob <- predict(lasso.model,type="response", 
                           newx = Test[,2:10], s = 'lambda.min')
pred <- prediction(Test$lasso.prob, Test$ResponseVar)

# calculate probabilities for TPR/FPR for predictions
perf <- performance(pred,"tpr","fpr")
performance(pred,"auc") # shows calculated AUC for model
plot(perf,colorize=FALSE, col="black") # plot ROC curve
lines(c(0,1),c(0,1),col = "gray", lty = 4 )

上記のTest$lasso.prob場合、異なるラムダを入力して、各値での予測力をテストできます。

于 2014-08-04T14:29:40.747 に答える