Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用混淆矩阵和roc曲线检验logistic回归模型的质量_R_Logistic Regression - Fatal编程技术网

用混淆矩阵和roc曲线检验logistic回归模型的质量

用混淆矩阵和roc曲线检验logistic回归模型的质量,r,logistic-regression,R,Logistic Regression,我尝试使用逻辑回归模型进行分类 以下是我的工作: library(ROCR) data<-read.csv("c:/InsideNetwork.csv"); s1 <- sample(which(data$Active==1),3000) s2 <- sample(which(data$Active==0),6000) train <- data[c(s1,s2),] test <- data[c(-s1,-s2),] m<-glm(Active~Var1+

我尝试使用逻辑回归模型进行分类

以下是我的工作:

library(ROCR)
data<-read.csv("c:/InsideNetwork.csv");
s1 <- sample(which(data$Active==1),3000)
s2 <- sample(which(data$Active==0),6000)
train <- data[c(s1,s2),]
test  <- data[c(-s1,-s2),]
m<-glm(Active~Var1+Var2+Var3,data=train,family=binomial())
test$score<-predict(m,type="response", test)
pred<-prediction(test$score, test$Active)
perf<-performance(pred,"tpr","fpr")
plot(perf, lty=1)
库(ROCR)

数据使用以下辅助功能:

pred_df <- data.frame(dep_var = test$Active, score = test$score)
confusion_matrix(pred_df, cutoff = 0.2)

辅助函数
#“为给定的预测集绘制混淆矩阵,并返回该表。
#'
#“@param dataframe data.frame。必须包含\code{score}和\code{dep_var}
#"栏目,。将计算这些值的混淆矩阵。
#'所述列必须都是数字。
#“@param”。指定大于1的数字的截止点
#'用于预测,否则为0。默认值为0.5。
#“@param plot.it”符合逻辑。是否将混淆矩阵绘制为
#“四折图。默认值为\code{TRUE}。
#“@param xlab字符。行的标签(\code{dep_var})。默认值
#'是\代码{c(“dep_var=0”,“dep_var=1”)}。
#“@param ylab字符。行的标签(\code{score})。默认值
#'是\代码{c(“分数=0”,“分数=1”)}。
#“@param标题字符。四折图的标题(如果已绘制)。
#“@返回一张桌子。混乱矩阵表。

非常感谢,完美的解决方案。我的结果是:混淆矩阵(pred_df,cutoff=0.5)分数=0分数=1 dep_var=0 245129 16368 dep_var=1 626 5130可以吗?这取决于您的应用程序。你的结果似乎很受欢迎。尝试向上或向下移动
截止开关
,直到左下角和右上角(红色)的面积小于其他(绿色)的面积。数据是关于电信网络的。我试图找到愿意离开运营商的用户。我将截止值改为0.5,结果如下:分数=0分数=1流失=0 250616 10881流失=1 729 5027我可以这样解释:使用这个模型,我可以找到16K用户,这些用户被归类为将离开运营商的用户,其中30%将真正这样做,将其与随机模型~2%进行比较,我认为这是一个相当好的结果,但您的解释是正确的,并且对逻辑回归也不错。:)好的,我明白了,你能给我推荐一些阅读材料吗?在那里我可以学到更好的机器学习算法和模型,我可以用在我的数据上?我知道你也有波兰血统,我是波兰人
confusion_matrix(data.frame(score = rank(iris$Sepal.Length)/nrow(iris),
                 dep_var = as.integer(iris$Species != 'setosa')), cutoff = 0.5)

#             score = 0 score = 1
# dep_var = 0        49         1
# dep_var = 1        24        76
#' Plot a confusion matrix for a given prediction set, and return the table.
#'
#' @param dataframe data.frame. Must contain \code{score} and \code{dep_var}
#'    columns. The confusion matrix will be calculated for these values.
#'    The mentioned columns must both be numeric.
#' @param cutoff numeric. The cutoff at which to assign numbers greater a 1
#'    for prediction purposes, and 0 otherwise. The default is 0.5.
#' @param plot.it logical. Whether or not to plot the confusion matrix as a
#'    four fold diagram. The default is \code{TRUE}.
#' @param xlab character. The labels for the rows (\code{dep_var}). The default
#'    is \code{c("dep_var = 0", "dep_var = 1")}.
#' @param ylab character. The labels for the rows (\code{score}). The default
#'    is \code{c("score = 0", "score = 1")}.
#' @param title character. The title for the fourfoldplot, if it is graphed.
#' @return a table. The confusion matrix table.
confusion_matrix <- function(dataframe, cutoff = 0.2, plot.it = TRUE,
                             xlab = c("dep_var = 0", "dep_var = 1"),
                             ylab = c("score = 0", "score = 1"), title = NULL) {
  stopifnot(is.data.frame(dataframe) &&
              all(c('score', 'dep_var') %in% colnames(dataframe)))
  stopifnot(is.numeric(dataframe$score) && is.numeric(dataframe$dep_var))


  dataframe$score <- ifelse(dataframe$score <= cutoff, 0, 1)
  categories <- dataframe$score * 2 + dataframe$dep_var
  confusion <- matrix(tabulate(1 + categories, 4), nrow = 2)
  colnames(confusion) <- ylab
  rownames(confusion) <- xlab
  if (plot.it) fourfoldplot(confusion, color = c("#CC6666", "#99CC99"),
                            conf.level = 0, margin = 1, main = title)
  confusion

}