2017-09-28 58 views
0

我試圖擬合Logistic嶺迴歸並開發瞭如下模型;我需要編碼的幫助來測試準確性和ROC/AUC曲線的閾值。Logistic嶺迴歸預測ROC/AUC和準確性使用R的測試代碼

我的編碼如下:

擬合模型

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) 
+0

.pred'而不是'predict'? – useR

+0

錯誤彈出沒有它,因爲:錯誤在cbind2(1,newx)%*%nbeta: Cholmod錯誤'X和/或Y有錯誤的尺寸'文件../MatrixOps/cholmod_sdmult.c,第90行 – Erandi

回答

0

沒有一個可重複的數據集這裏有一個猜測:

火車和測試矩陣具有不同的列L3和L6轉換爲因素的結果。默認情況下,as.factor()以一個因子創建儘可能多的級別,因爲它有唯一的值,所以如果碰巧train/test split具有不同的L3或L6唯一值,那麼由model.matrix創建的虛擬變量的數量( )也會有所不同。

可能的解決辦法:你爲什麼要使用`as.matrix`的`在y之前火車/測試分裂,或與完整的供應水平做as.factor as.factor(),像

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

我有嘗試了你提到的兩種方法。當「在訓練/測試拆分前做as.factor()」時,錯誤彈出爲「Error.alpha.formula(object,data = data): 'data'參數的類型錯誤。 (列車$ L3,levels = unique(data1 $ L3))),在列車$ L3 <-as.factor(列車$ L3,levels = unique(data1 $ L3))中顯示錯誤時彈出爲「Error:unexpected' 「 – Erandi

+0

我喜歡分享數據集,但是我不知道如何在stackoverflow上分享 – Erandi

+0

第一個錯誤可能是你試圖在你的實際數據集名爲data1時傳入變量'data'。第二個錯誤是因爲你有一個額外的'''在最後。 – nobits