glmnet fit error: number of observations in y not equal to the number of rows of x

I'm new to Lasso regression and am trying to get glmnet to work in preparation for lasso regression. Unfortunately, I hit a problem pretty early on. Could someone let me know what I've done wrong? Thank you!

df0<- read_csv("data_cleaned.csv")

#Categorical variables set to factors

df0$PL_Binary_Score<-as.factor(df0$PL_Binary_Score)
df0$Gender<-as.factor(df0$Gender)
df0$Ethnicity<-as.factor(df0$Ethnicity)
df0$Age<-as.factor(df0$Age)
df0$Location<-as.factor(df0$Location)
df0$Income<-as.factor(df0$Income)
df0$Education<-as.factor(df0$Education)
df0$Working_From_Home<-as.factor(df0$Working_From_Home)

#Omit all NAs
PL<-na.omit(df0)

fit <- PL[c("Social_Media_Time", 
                 "Ethnicity",
                 "Age",
                 "Location",
                 "Income",
                 "Education",
                 "Working_From_Home",
                 "Traditional_Time_Hrs", 
                 "Private_Use",
                 "Anxiety_Diagnosed", 
                 "Gender", 
                 "Public_Use")]

PL.2<- as.matrix(fit)

fit = glmnet(PL.2, "PL_Binary_Score")

I get the following error:

number of observations in y (1) not equal to the number of rows of x (287).

I'm a bit confused because there are no missing values that could explain a difference in row length.

Is PL_Binary_Score a vector of y values? Try

fit = glmnet(PL.2, PL_Binary_Score)

With quotes around it, glmnet will think it is a single value.

That worked great! Thank you

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.