我正在尝试使用glm在我的逻辑回归模型的训练数据上找到AUC
我将数据拆分为训练和测试集,使用glm拟合了Logistic回归模型回归模型,计算了预测值并尝试找到AUC
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert__orari);
lunedi_inizio_uno = (EditText)findViewById(R.id.editText_uno_lunedi_inizio);
lunediUno = (CheckBox)findViewById(R.id.checkBox_uno_lunedi);
lunedi_inizio_uno.addTextChangedListener(new TextWatcher() {
int keyDell;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
lunedi_inizio_uno.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
keyDell = 1;
int prevL = 0;
return false;
}
});
if (keyDell == 0) {
int len = lunedi_inizio_uno.getText().length();
if(len == 5) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + " ");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}if(len == 6) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + "-");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}if(len == 7) {
lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + " ");
lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
}
} else {
keyDell = 0;
}
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
lunediUno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
lunedi_inizio_uno.setText("Chiuso");
}
}
});
}
答案 0 :(得分:0)
我喜欢使用performance
库中的ROCR
命令。
library(ROCR)
# responsev = response variable
d.prediction<-prediction(predict(fit, type="response"), train$responsev)
d.performance<-performance(d.prediction,measure = "tpr",x.measure="fpr")
d.test.prediction<-prediction(predict(fit,newdata=d.test, type="response"), d.test$DNF)
d.test.prefermance<-performance(d.test.prediction, measure="tpr", x.measure="fpr")
# What is the actual numeric performance of our model?
performance(d.prediction,measure="auc")
performance(d.test.prediction,measure="auc")
答案 1 :(得分:0)
另一个用户友好的选择是使用caret
库,这使得在R中拟合和比较回归/分类模型非常简单。以下示例代码使用GermanCredit
数据集来预测信用使用逻辑回归模型的价值。该代码改编自以下博客:https://www.r-bloggers.com/evaluating-logistic-regression-models/。
library(caret)
## example from https://www.r-bloggers.com/evaluating-logistic-regression-models/
data(GermanCredit)
## 60% training / 40% test data
trainIndex <- createDataPartition(GermanCredit$Class, p = 0.6, list = FALSE)
GermanCreditTrain <- GermanCredit[trainIndex, ]
GermanCreditTest <- GermanCredit[-trainIndex, ]
## logistic regression based on 10-fold cross-validation
trainControl <- trainControl(
method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary
)
fit <- train(
form = Class ~ Age + ForeignWorker + Property.RealEstate + Housing.Own +
CreditHistory.Critical,
data = GermanCreditTrain,
trControl = trainControl,
method = "glm",
family = "binomial",
metric = "ROC"
)
## AUC ROC for training data
print(fit)
## AUC ROC for test data
## See https://topepo.github.io/caret/measuring-performance.html#measures-for-class-probabilities
predictTest <- data.frame(
obs = GermanCreditTest$Class, ## observed class labels
predict(fit, newdata = GermanCreditTest, type = "prob"), ## predicted class probabilities
pred = predict(fit, newdata = GermanCreditTest, type = "raw") ## predicted class labels
)
twoClassSummary(data = predictTest, lev = levels(predictTest$obs))