2017-04-04 52 views
0

我試圖獲得λ值鍵入「雙」,我發現這樣一個問題:R glmnet : "(list) object cannot be coerced to type 'double' "[R glmnet錯誤:對象無法被強制

但無法弄清楚如何使它適用。這裏是代碼:

faba <- read.table("abalone.txt",sep=",") 
faba$y <- ifelse(faba$V9>9,1,0) 
head(faba) 

xtrain <- faba[1:3133,1:8] 
ytrain <- faba[1:3133,10] 
xtest <- faba[-c(1:3133),1:8] 
ytest <- faba[-c(1:3133),10] 


#find mean, std in xtrain 
mean_xtrain <- sapply(xtrain[2:8], mean) 
sd_xtrain <- sapply(xtrain[2:8], sd) 


ytrain_norm <- as.factor(ytrain) 
ytest_norm <- as.factor(ytest) 


#standardise xtrain with overall mean,sd 
xtrain_norm <- faba[1:3133,1:8] 
for(i in c(2,3,4,5,6,7,8)){ 
    xtrain_norm[i] <- (xtrain_norm[i]-mean_xtrain[i-1])/sd_xtrain[i-1] 
} 


#standardise xtest with xtrain mean,sd 
xtest_norm <- faba[-c(1:3133),1:8] 
for(i in c(2,3,4,5,6,7,8)){ 
    xtest_norm[i] <- (xtest_norm[i]-mean_xtrain[i-1])/sd_xtrain[i-1] 
} 


cvstd <- cv.glmnet(xtrain_norm, ytrain, family = "binomial", 
        alpha = 0, nfolds = 10, type.measure = "class") 

然後我得到的錯誤:

Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : 
    (list) object cannot be coerced to type 'double' 

我無法弄清楚如何解決這個問題。這個錯誤甚至意味着什麼?任何提示將不勝感激!
這就是數據的模樣:

> head(faba) 
    V1 V2 V3 V4  V5  V6  V7 V8 V9 y 
1 M 0.455 0.365 0.095 0.5140 0.2245 0.1010 0.150 15 1 
2 M 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.070 7 0 
3 F 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.210 9 0 
4 M 0.440 0.365 0.125 0.5160 0.2155 0.1140 0.155 10 1 
5 I 0.330 0.255 0.080 0.2050 0.0895 0.0395 0.055 7 0 
6 I 0.425 0.300 0.095 0.3515 0.1410 0.0775 0.120 8 0 
+1

是否問題仍然如果去掉'V1'發生矩陣形式?我的第一個猜測是你的數據中有一個因子或字符變量。另外,不要使用'c(2,3,4,5,6,7,8)',只需使用'2:8'。 – Phil

+0

我想出了一個解決方案,有一個稱爲scale的函數來實現這個技巧,並允許最後一行代碼在請求者矩陣中工作。 – rannoudanames

+0

請提供詳細的答案,以便將來可能遇到類似情況的其他人可以運用你的解決方案 – Phil

回答

0
faba <- read.table("abalone.txt",sep=",") 
faba$y <- ifelse(faba$V9>9,1,0) 
head(faba) 

xtrain <- faba[1:3133,1:8] 
ytrain <- faba[1:3133,10] 
xtest <- faba[-c(1:3133),1:8] 
ytest <- faba[-c(1:3133),10] 

#find mean, std in xtrain 
(mean_xtrain <- sapply(xtrain[2:8], mean)) 
(sd_xtrain <- sapply(xtrain[2:8], sd)) 

#standardise xtrain with overall mean,sd 
xtrain_norm <- cbind(xtrain[,1],scale(xtrain[,2:8])) 
ytrain_norm <- as.factor(ytrain) 
#standardise xtest with xtrain mean,sd 
xtest_norm <- faba[-c(1:3133),1:8] 
xtest_norm <- cbind(xtest_norm[,1],scale(xtest_norm[,2:8],center=mean_xtrain,scale=sd_xtrain)) 
ytest_norm <- as.factor(ytest) 

這使得具有所需要的glmnet

相關問題