我正在尝试进行套索回归,但我无法在 R 中很好地定义我的 X 和 Y。
#load data
>test.data<-read.spss("C:\\Users\\Inhib\\OneDrive\\documents\\dummy.sav",use.value.labels=TRUE, to.data.frame=TRUE)
>test.data #testing my data, it's all there so I won't add it here
#take columns 2 to 6 for X
>X<-as.matrix(test.data[,2:6])
# Column 1 is the predicted variable Y
>Y<-as.matrix(test.data[,1])
#Ok, let's fit it
fit<- glmnet(x, y, family="gaussian", alpha=0, lambda=0.001)
然后我收到此错误消息:
"Error in glmnet(x, y, family = "gaussian", alpha = 0, lambda = 0.001) :
number of observations in y (100) not equal to the number of rows of x (222)"
现在所有列的长度相同(222),但错误表明 Y 中只有 100 列,X 中只有 222 列。
#So I checked for Y here
>length(Y)
[1] 222
#Then checked for X
> length(X)
[1] 1110
现在,它肯定表明我错过了一些东西。显然矩阵大小不同,X 是 222*5 列,但我怎样才能让它工作呢?这意味着这个错误将会消失......我尝试了很多方法。我已经为此工作了几个小时,这确实阻止了我的进步,而且谷歌没有太多帮助。将不胜感激解决方案。