0

I am trying to obtain the lambda value, I have found this question: R glmnet : "(list) object cannot be coerced to type 'double' "

However cannot figure out how to make it apply. here is the code:

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")

I then get the error:

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

And I cannot figure out how to fix this. What does the error even mean? Any hints would be greatly appreciated!
This is what the data looks like:

> 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
Community
  • 1
  • 1
rannoudanames
  • 139
  • 1
  • 8
  • 1
    Does the issue still occur if you remove `V1`? My first guess is that you have a factor or character variable in your data. Also, instead of `c(2,3,4,5,6,7,8)`, just use `2:8`. – Phil Apr 04 '17 at 04:54
  • I figured out a solution, there is a function called scale that does the trick and allows the last line of code to work as it requiers matrix – rannoudanames Apr 04 '17 at 14:50
  • Please provide a detailed answer so that others who may run into a similar situation in the future can apply your solution. – Phil Apr 04 '17 at 15:51

1 Answers1

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)

This allows to have matrix form which is required for the glmnet

rannoudanames
  • 139
  • 1
  • 8