【问题标题】:Logistic Ridge Regression predict ROC/ AUC and accuracy testingLogistic Ridge 回归预测 ROC/AUC 和准确性测试
【发布时间】:2017-09-28 17:20:14
【问题描述】:

我正在尝试拟合 Logistic Ridge 回归并开发模型如下;我需要编码方面的帮助来测试它的准确性和带有阈值的 ROC/AUC 曲线。

我的编码如下:

拟合模型

library(glmnet)
library(caret)

data1<-read.csv("D:\\Research\\Final2.csv",header=T,sep=",")
str(data1)
'data.frame':   154 obs. of  12 variables:
$ Earningspershare : num  12 2.69 8.18 -0.91 3.04 ...
 $ NetAssetsPerShare: num  167.1 17.2 41.1 14.2 33 ...
$ Dividendpershare : num  3 1.5 1.5 0 1.25 0 0 0 0 0.5 ...
 $ PE               : num  7.35 8.85 6.66 -5.27 18.49 ...
 $ PB               : num  0.53 1.38 1.33 0.34 1.7 0.23 0.5 3.1 0.5 0.3 ...
$ ROE              : num  0.08 0.16 0.27 -0.06 0.09 -0.06 -0.06 0.15 0.09 0.
 $ ROA              : num  0.02 0.09 0.14 -0.03 0.05 -0.04 -0.05 0.09 0.03 0
$ Log_MV           : num  8.65 10.38 9.81 8.3 10.36 ..
$ Return_yearly    : int  0 1 0 0 0 0 0 0 0 0 ...
$ L3               : int  0 0 0 0 0 0 0 0 0 0 ...
$ L6               : int  0 0 0 0 0 0 0 0 0 0 ...
$ Sector           : int  2 2 2 2 2 1 2 2 4 1 ...

smp_size <- floor(0.8 * nrow(data1))
set.seed(123)
train_ind <- sample(seq_len(nrow(data1)), size = smp_size)
train <- data1[train_ind, ]
test <- data1[-train_ind, ]

train$Return_yearly <-as.factor(train$Return_yearly)
train$L3 <-as.factor(train$L3)
train$L6 <-as.factor(train$L6)
train$Sector <-as.factor(train$Sector)

train$L3 <-model.matrix( ~ L3 - 1, data=train)
train$L6 <-model.matrix( ~ L6 - 1, data=train)
train$Sector<-model.matrix( ~ Sector - 1, data=train)

x <- model.matrix(Return_yearly ~., train)
y <- train$Return_yearly

ridge.mod <- glmnet(x, y=as.factor(train$Return_yearly), family='binomial', alpha=0, nlambda=100, lambda.min.ratio=0.0001)

set.seed(1)
cv.out <- cv.glmnet(x, y=as.factor(train$Return_yearly), family='binomial', alpha=0, nfolds = 5, type.measure = "auc", nlambda=100, lambda.min.ratio=0.0001)
plot(cv.out)
best.lambda <- cv.out$lambda.min
best.lambda
[1] 5.109392

测试模型

test$L3 <-as.factor(test$L3)
test$L6 <-as.factor(test$L6)
test$Sector <-as.factor(test$Sector)
test$Return_yearly <-as.factor(test$Return_yearly)

test$L3 <-model.matrix( ~ L3 - 1, data=test)
test$L6 <-model.matrix( ~ L6 - 1, data=test)
test$Sector<-model.matrix( ~ Sector - 1, data=test)

newx <- model.matrix(Return_yearly ~., test)
y.pred <- as.matrix(ridge.mod,newx=newx, type="class",data=test)

比较准确性测试;弹出错误,无法继续

compare <- cbind (actual=test$Return_yearly, y.pred)
Warning message:
   In cbind(actual = test$Return_yearly, y.pred) :
   number of rows of result is not a multiple of vector length (arg 1)

【问题讨论】:

  • 你为什么用as.matrix代替y.pred而不是predict
  • 没有它就会弹出错误,因为:cbind2(1, newx) 中的错误 %*% nbeta : Cholmod 错误 'X 和/或 Y 有错误的尺寸' 在文件 ../MatrixOps/cholmod_sdmult.c ,第 90 行

标签: r


【解决方案1】:

如果没有可重现的数据集,这是一个猜测:

将 L3 和 L6 转换为因子后,训练矩阵和测试矩阵具有不同的列。默认情况下, as.factor() 在一个因子中创建与唯一值一样多的级别,因此如果训练/测试拆分偶然具有不同的 L3 或 L6 唯一值,则 model.matrix( ) 也会有所不同。

可能的解决方案:执行 as.factor() 之前 训练/测试拆分,或提供完整级别的 as.factor,如

train$L3 <- as.factor(train$L3, levels = unique(data1$L3))

【讨论】:

  • 你提到的两种方法我都试过了。当“在训练/测试拆分之前执行 as.factor()”时,错误弹出为“terms.formula(object, data = data) 中的错误:'data' 参数类型错误”。如您所展示的,当提供带有完整级别的 as.factor 时,错误弹出为“错误:意外')' in "train$L3
  • 我喜欢分享数据集,但不知道如何在stackoverflow上分享
  • 第一个错误可能是当您的实际数据集称为data1 时,您尝试传入变量data。第二个错误是因为你最后有一个额外的)
  • 嗨nobit,你能详细说明第一个错误,当实际数据集称为data1时传入变量数据,我不是编程背景。谢谢
  • 我解决了这个问题并继续使用method1进行测试。非常感谢
【解决方案2】:

使用以下代码绘制准确度和灵敏度。

ROC_Pre <- prediction(ROC_Pre, data$LSD)
ROC <- performance(ROC_Pre, "tpr", "fpr")
plot(ROC)

【讨论】:

    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2016-06-03
    • 2018-08-24
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多