jaredlander / coefplot

Plotting model coefficients
Other
27 stars 19 forks source link

glmnet support but not cv.glmnet support #12

Closed jonrobinson2 closed 6 years ago

jonrobinson2 commented 8 years ago

Don't think I need to add a reproducible example as the error is actually pretty self explanatory (thanks R):

Error in UseMethod(generic = "extract.coef", object = model) : 
  no applicable method for 'extract.coef' applied to an object of class "cv.glmnet"

Any plans on updating the package to be able to handle these kinds of objects since they fall under the glmnet family of models?

jaredlander commented 8 years ago

Strange, it works on my computer. Make sure you have the latest version. Either way, I'm refactoring the code so it should work better in the future.

library(useful)
library(coefplot)
library(glmnet)

dataDir <- "data"
acs <- read.table(file.path(dataDir, "acs_ny.csv"), sep=",", header=TRUE, stringsAsFactors=FALSE)
acs$Income <- with(acs, FamilyIncome >= 150000)

# build predictor matrix
# do not include the intercept as glmnet will add that automatically
acsX <- build.x(Income ~ NumBedrooms + NumChildren + NumPeople + 
                    NumRooms + NumUnits + NumVehicles + NumWorkers + 
                    OwnRent + YearBuilt + ElectricBill + FoodStamp + 
                    HeatingFuel + Insurance + Language - 1, 
                data=acs, contrasts=FALSE)

# build response predictor
acsY <- build.y(Income ~ NumBedrooms + NumChildren + NumPeople + 
                    NumRooms + NumUnits + NumVehicles + NumWorkers + 
                    OwnRent + YearBuilt + ElectricBill + FoodStamp + 
                    HeatingFuel + Insurance + Language - 1, data=acs)

acs1 <- cv.glmnet(x=acsX, y=acsY, family="binomial")

coefplot(acs1, lambda='lambda.1se')
jonrobinson2 commented 8 years ago

Hm. I downloaded it from the http://cran.rstudio.com repo today.

On Tue, Apr 12, 2016 at 7:14 PM, Jared Lander notifications@github.com wrote:

Strange, it works on my computer. Make sure you have the latest version. Either way, I'm refactoring the code so it should work better in the future.

library(useful) library(coefplot) library(glmnet)

dataDir <- "data" acs <- read.table(file.path(dataDir, "acs_ny.csv"), sep=",", header=TRUE, stringsAsFactors=FALSE) acs$Income <- with(acs, FamilyIncome >= 150000)

build predictor matrix

do not include the intercept as glmnet will add that automatically

acsX <- build.x(Income ~ NumBedrooms + NumChildren + NumPeople + NumRooms + NumUnits + NumVehicles + NumWorkers + OwnRent + YearBuilt + ElectricBill + FoodStamp + HeatingFuel + Insurance + Language - 1, data=acs, contrasts=FALSE)

build response predictor

acsY <- build.y(Income ~ NumBedrooms + NumChildren + NumPeople + NumRooms + NumUnits + NumVehicles + NumWorkers + OwnRent + YearBuilt + ElectricBill + FoodStamp + HeatingFuel + Insurance + Language - 1, data=acs)

acs1 <- cv.glmnet(x=acsX, y=acsY, family="binomial")

coefplot(acs1, lambda='lambda.1se')

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/jaredlander/coefplot/issues/12#issuecomment-209145267

jaredlander commented 8 years ago

Have you tried installing the github version? That might be the ticket.

devtools::install_github('jaredlander/coefplot')
jonrobinson2 commented 8 years ago

Hm, still not working w/ the GitHub package.

> coefplot::extract.coef(glmnet::cv.glmnet(x=as.matrix(iris[,1:3]), y=runif(n=nrow(iris)), alpha=0))
Error in UseMethod(generic = "extract.coef", object = model) : 
  no applicable method for 'extract.coef' applied to an object of class "cv.glmnet"
jaredlander commented 8 years ago

That's interesting. This works:

coefplot:::extract.coef.cv.glmnet(acs1)

But not

coefplot:::extract.coef(acs1)

Pretty sure it has something to do with S3 dispatching since glmnet objects have multiple classes. That will need more investigation. Though you can still use coefplot as normal.

coefplot(acs1, 'lambda.1se')
jonrobinson2 commented 8 years ago

Thanks Jared. Quite interesting design choice to make them multi-class.