I'm trying to run the code:?inds<-which(c != 0 ), but it gave me error:?Error in base::which(x, arr.ind, useNames, ...) :?? argument to 'which' is not logical Here is code: alphas <- seq(0, 1, by=.002)mses <- numeric(501)mins <- numeric(501)maxes <- numeric(501)for(i in 1:501){? cvfits <- cv.glmnet(Train2, Train$Item_Outlet_Sales, alpha=alphas[i], nfolds=32)? loc <- which(cvfits$lambda==cvfits$lambda.min)? maxes[i] <- cvfits$lambda %>% max? mins[i] <- cvfits$lambda %>% min? mses[i] <- cvfits$cvm[loc]}`%ni%`<-Negate(`%in%`)c<-coef(cvfits,s='lambda.1se',exact=TRUE)inds<-which(c != 0 ) Here is the c content looks like:?> c33 x 1 sparse Matrix of class "dgCMatrix"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1(Intercept) ? ? ? ? ? ? ? ? ? ? ?7.476895931Item_Fat_Content.Low.Fat ? ? ? ? . ? ? ? ? ?Item_Fat_Content.Regular ? ? ? ? . ? ? ? ? ?Item_Type.Breads ? ? ? ? ? ? ? ? . ? ? ? ? ?Item_Type.Breakfast ? ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Canned ? ? ? ? ? ? ? ? 0.003430669Item_Type.Dairy ? ? ? ? ? ? ? ? -0.022579673Item_Type.Frozen.Foods ? ? ? ? ?-0.008216547Item_Type.Fruits.and.Vegetables ?. ? ? ? ? ?Item_Type.Hard.Drinks ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Health.and.Hygiene ? ? . ? ? ? ? ?Item_Type.Household ? ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Meat ? ? ? ? ? ? ? ? ? . ? ? ? ? ?Item_Type.Others ? ? ? ? ? ? ? ? . ? ? ? ? ?Item_Type.Seafood ? ? ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Snack.Foods ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Soft.Drinks ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Starchy.Foods ? ? ? ? ?. ? ? ? ? ?Outlet_Establishment_Year.1987 ?-0.345927916Outlet_Establishment_Year.1997 ? 1.692678186Outlet_Establishment_Year.1998 ?-2.259508290Outlet_Establishment_Year.1999 ? . ? ? ? ? ?Outlet_Establishment_Year.2002 ?-0.032971913Outlet_Establishment_Year.2004 ? 1.756230495Outlet_Establishment_Year.2007 ? . ? ? ? ? ?Outlet_Establishment_Year.2009 ?-0.549210057Outlet_Size.Medium ? ? ? ? ? ? ? 0.056897825Outlet_Size.Small ? ? ? ? ? ? ? -1.706006538Outlet_Location_Type.Tier.3 ? ? ?0.373456218Item_Identifier_CombinedFD ? ? ? . ? ? ? ? ?Item_MRP ? ? ? ? ? ? ? ? ? ? ? ? 0.505435352Item_Weight ? ? ? ? ? ? ? ? ? ? ?. ? ? ? ? ?Item_Visibility_MeanRatio ? ? ? -0.007274202 [[alternative HTML version deleted]]
The `c` function is extremely common. You CAN redefine this object as a numeric variable if you want but I strongly recommend against it. I don't recognize the function you are using to create cvfits, but it looks like the coef method for that object is not returning a numeric vector, so your comparison is with some other kind of object. Notice how ugly your code looks below... only you can fix this by setting your email program to send plain text to the mailing list. -- Sent from my phone. Please excuse my brevity. On March 22, 2017 5:15:04 AM PDT, Allan Tanaka <allantanaka11 at yahoo.com> wrote:>I'm trying to run the code:?inds<-which(c != 0 ), but it gave me >error:?Error in base::which(x, arr.ind, useNames, ...) :?? argument to >'which' is not logical >Here is code: >alphas <- seq(0, 1, by=.002)mses <- numeric(501)mins <- >numeric(501)maxes <- numeric(501)for(i in 1:501){? cvfits <- >cv.glmnet(Train2, Train$Item_Outlet_Sales, alpha=alphas[i], nfolds=32)? >loc <- which(cvfits$lambda==cvfits$lambda.min)? maxes[i] <- >cvfits$lambda %>% max? mins[i] <- cvfits$lambda %>% min? mses[i] <- >cvfits$cvm[loc]}`%ni%`<-Negate(`%in%`)c<-coef(cvfits,s='lambda.1se',exact=TRUE)inds<-which(c >!= 0 ) >Here is the c content looks like:?> c33 x 1 sparse Matrix of class >"dgCMatrix"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1(Intercept) ? ? >? ? ? ? ? ? ? ? ?7.476895931Item_Fat_Content.Low.Fat ? ? ? ? . ? ? ? ? >?Item_Fat_Content.Regular ? ? ? ? . ? ? ? ? ?Item_Type.Breads ? ? ? ? ? >? ? ? . ? ? ? ? ?Item_Type.Breakfast ? ? ? ? ? ? ?. ? ? ? ? >?Item_Type.Canned ? ? ? ? ? ? ? ? 0.003430669Item_Type.Dairy ? ? ? ? ? >? ? ? -0.022579673Item_Type.Frozen.Foods ? ? ? ? >?-0.008216547Item_Type.Fruits.and.Vegetables ?. ? ? ? ? >?Item_Type.Hard.Drinks ? ? ? ? ? ?. ? ? ? ? >?Item_Type.Health.and.Hygiene ? ? . ? ? ? ? ?Item_Type.Household ? ? ? >? ? ? ?. ? ? ? ? ?Item_Type.Meat ? ? ? ? ? ? ? ? ? . ? ? ? ? >?Item_Type.Others ? ? ? ? ? ? ? ? . ? ? ? ? ?Item_Type.Seafood ? ? ? ? >? ? ? ?. ? ? ? ? ?Item_Type.Snack.Foods ? ? ? ? ? ?. ? ? ? ? >?Item_Type.Soft.Drinks ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Starchy.Foods ? >? ? ? ?. ? ? ? ? ?Outlet_Establishment_Year.1987 >?-0.345927916Outlet_Establishment_Year.1997 ? >1.692678186Outlet_Establishment_Year.1998 >?-2.259508290Outlet_Establishment_Year.1999 ? . ? ? ? ? >?Outlet_Establishment_Year.2002 >?-0.032971913Outlet_Establishment_Year.2004 ? >1.756230495Outlet_Establishment_Year.2007 ? . ? ? ? ? >?Outlet_Establishment_Year.2009 ?-0.549210057Outlet_Size.Medium ? ? ? ? >? ? ? 0.056897825Outlet_Size.Small ? ? ? ? ? ? ? >-1.706006538Outlet_Location_Type.Tier.3 ? ? >?0.373456218Item_Identifier_CombinedFD ? ? ? . ? ? ? ? ?Item_MRP ? ? ? >? ? ? ? ? ? ? ? ? 0.505435352Item_Weight ? ? ? ? ? ? ? ? ? ? ?. ? ? ? ? >?Item_Visibility_MeanRatio ? ? ? -0.007274202 > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
> I'm trying to run the code:?inds<-which(c != 0 ), but it gave me error:?Error in > base::which(x, arr.ind, useNames, ...) :?? argument to 'which' is not logicalYour coefficients are not a numeric vector. You'll have to sort that out by extracting or testing the individual values. A look at your output _tells you_ that the object is a sparse matrix of class dgCMatrix . Looking that up would tell you that the nonzero elements are in slot x of the object. So it may be unnecessary to ask which are zero; maybe you just need c at x (though I also would recommend you avoid using common function names as variable names) If you do need to test elements for zeroes, though, note that '==' and '!=' are not usually recommended for comparisons with zero owing to finite numerical representation. So that may well be unwise. See the Note in ?'=='. S Ellison> Here is code: > alphas <- seq(0, 1, by=.002)mses <- numeric(501)mins <- numeric(501)maxes > <- numeric(501)for(i in 1:501){? cvfits <- cv.glmnet(Train2, > Train$Item_Outlet_Sales, alpha=alphas[i], nfolds=32)? loc <- > which(cvfits$lambda==cvfits$lambda.min)? maxes[i] <- cvfits$lambda %>% > max? mins[i] <- cvfits$lambda %>% min? mses[i] <- cvfits$cvm[loc]}`%ni%`<- > Negate(`%in%`)c<-coef(cvfits,s='lambda.1se',exact=TRUE)inds<-which(c != 0 > ) Here is the c content looks like:?> c33 x 1 sparse Matrix of class > "dgCMatrix"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1(Intercept) ? ? ? ? ? ? ? ? ? ? ?7.476895931Item_Fa > t_Content.Low.Fat ? ? ? ? . ? ? ? ? ?Item_Fat_Content.Regular ? ? ? ? . ? ? ? ? ?Item_Type > .Breads ? ? ? ? ? ? ? ? . ? ? ? ? ?Item_Type.Breakfast ? ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Canned > ? ? ? ? ? ? 0.003430669Item_Type.Dairy ? ? ? ? ? ? ? ? - > 0.022579673Item_Type.Frozen.Foods ? ? ? ? ?- > 0.008216547Item_Type.Fruits.and.Vegetables ?. ? ? ? ? ?Item_Type.Hard.Drinks > ? ? ? ? ?. ? ? ? ? ?Item_Type.Health.and.Hygiene ? ? . ? ? ? ? ?Item_Type.Household > ? ?. ? ? ? ? ?Item_Type.Meat ? ? ? ? ? ? ? ? ? . ? ? ? ? ?Item_Type.Others ? ? ? ? ? ? ? ? . ? ? ? ? ?Item > _Type.Seafood ? ? ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Snack.Foods ? ? ? ? ? ?. ? ? ? ? ?Item_Type.S > oft.Drinks ? ? ? ? ? ?. ? ? ? ? ?Item_Type.Starchy.Foods ? ? ? ? ?. ? ? ? ? ?Outlet_Establishm > ent_Year.1987 ?- > 0.345927916Outlet_Establishment_Year.1997 ? 1.692678186Outlet_Establishm > ent_Year.1998 ?- > 2.259508290Outlet_Establishment_Year.1999 ? . ? ? ? ? ?Outlet_Establishment_Y > ear.2002 ?- > 0.032971913Outlet_Establishment_Year.2004 ? 1.756230495Outlet_Establishm > ent_Year.2007 ? . ? ? ? ? ?Outlet_Establishment_Year.2009 ?- > 0.549210057Outlet_Size.Medium ? ? ? ? ? ? ? 0.056897825Outlet_Size.Small > - > 1.706006538Outlet_Location_Type.Tier.3 ? ? ?0.373456218Item_Identifier_Com > binedFD ? ? ? . ? ? ? ? ?Item_MRP ? ? ? ? ? ? ? ? ? ? ? ? 0.505435352Item_Weight > . ? ? ? ? ?Item_Visibility_MeanRatio ? ? ? -0.007274202 > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK