我正在尝试使用glmnet函数创建一个只进行岭回归的函数并返回系数。该函数接受x,y值。
myfun <- function(x,y)
{
data<-cbind(x,y)
model<-model.matrix(y~., data=data)
ridgedata = model[,-1]
train<- sample(1:dim(ridgedata)[1], round(0.8*dim(ridgedata)[1]))
test<- setdiff(1:dim(ridgedata)[1],train)
grid =10^ seq (10,-2, length =100)
ridge_model<-glmnet(x[train,],y[train],alpha=0, lambda =grid)
return(coeff(ridge_model))
}
但是当我运行glmnet函数时,我得到以下错误:
Error in elnet(x, is.sparse, ix, jx, y, weights, offset, type.gaussian, :
(list) object cannot be coerced to type 'double'
在elnet函数中,对应强制的代码似乎是
“weights = as.double(weights)”
它似乎是一个1s的向量。但我不明白为什么会导致错误...
这是我的函数调用:
mydata <- read.csv("regress.csv")
x<- mydata[,1:4]
y<- mydata[,5]
myfun(x,y)
我不确定导致错误的原因。任何的意见都将会有帮助。感谢。
答案 0 :(得分:1)
之所以会出现此错误,是因为当需要矩阵时,您会将data.frame作为x提供给glmnet。查看代码,您创建了一个model.matrix,它应该是输入:
myfun <- function(x,y)
{
data<-cbind(x,y)
model<-model.matrix(y~., data=data)
ridgedata = model[,-1]
n= nrow(ridgedata)
train<- sample(n, round(0.8*n))
test<- setdiff(1:n,train)
grid =10^ seq (10,-2, length =100)
# bug is here
ridge_model<-glmnet(ridgedata[train,],y[train],alpha=0, lambda =grid)
return(coef(ridge_model))
}
head(myfun(mtcars[,-1],mtcars[,1]))
6 x 100 sparse Matrix of class "dgCMatrix"
[[ suppressing 100 column names ‘s0’, ‘s1’, ‘s2’ ... ]]
(Intercept) 1.997692e+01 1.997692e+01 1.997692e+01 1.997692e+01
cyl -1.794456e-09 -2.372165e-09 -3.135862e-09 -4.145425e-09
disp -2.549069e-11 -3.369719e-11 -4.454570e-11 -5.888679e-11
hp -5.048989e-11 -6.674466e-11 -8.823252e-11 -1.166382e-10
drat 4.829898e-09 6.384841e-09 8.440384e-09 1.115769e-08
wt -3.313208e-09 -4.379866e-09 -5.789924e-09 -7.653939e-09