0

How do I fix this? This is the code I wrote before. Do the number of observations have to be equal? Would appreciate any help!

DATA4=policeexcel2[,c("age", "gender", "raceethnicity", "month", "city","state", "latitude", "longitude", "tract_ce", "cause", "armed", "pop", "share_white", "share_hispanic", "share_black", "p_income", "h_income", "county_income", "comp_income")]

y1<-DATA4$share_white
x1<- model_matrix(DATA4, share_white~.*.)[,-1]
var.names1=names(x1)
dim(x1)

set.seed(216)
cvmod.0=cv.glmnet(y=y1,x=as.matrix(x1),alpha=0)

Error in glmnet(x, y, weights = weights, offset = offset, lambda = lambda, : number of observations in y (467) not equal to the number of rows of x (461)

Vkara
  • 7
  • 2
  • you probably have some missing values. Try `DATA4 <- na.omit(DATA4)` as the second line of your code (i.e. after defining `DATA4` but before doing anything else) – Ben Bolker Nov 11 '19 at 02:31
  • Please provide a sample dataset otherwise it will be very difficult to help you out. Please see https://stackoverflow.com/q/5963269/6123824 – UseR10085 Nov 11 '19 at 05:00

0 Answers0