Skip to contents

Extract Coefficient Information from Models

Usage

extract.coef(model, ...)

# S3 method for default
extract.coef(model, ...)

# S3 method for lm
extract.coef(model, ...)

# S3 method for glm
extract.coef(model, ...)

# S3 method for rxLinMod
extract.coef(model, ...)

# S3 method for rxGlm
extract.coef(model, ...)

# S3 method for rxLogit
extract.coef(model, ...)

# S3 method for glmnet
extract.coef(model, lambda = stats::median(model$lambda), ...)

# S3 method for cv.glmnet
extract.coef(model, lambda = "lambda.min", ...)

# S3 method for maxLik
extract.coef(model, ...)

# S3 method for xgb.Booster
extract.coef(
  model,
  feature_names = NULL,
  removeNonSelected = TRUE,
  zero_threshold = 0.001,
  ...
)

Arguments

model

Model object to extract information from.

...

Further arguments

lambda

'lambda.min'

feature_names

Names of coefficients

removeNonSelected

If TRUE (default) do not return the non-selected (0) coefficients

zero_threshold

Since coefficients from xgboost are not exactly zero, this is the threshold under which a coefficient is considered zero

Value

A data.frame containing the coefficient, the standard error and the variable name.

Details

Gets the coefficient values and standard errors, and variable names from a model.

Author

Jared P. Lander

Examples

if (FALSE) {
library(ggplot2)
data(diamonds)
library(coefplot)
mod1 <- lm(price ~ carat + cut + x, data=diamonds)
mod2 <- glm(price > 10000 ~ carat + cut + x, data=diamonds, family=binomial(link="logit"))
mod3 <- lm(price ~ carat*cut + x, data=diamonds)
extract.coef(mod1)
extract.coef(mod2)
extract.coef(mod3)

mod4 <- rxLinMod(price ~ carat*cut + x, diamonds)
}

if (FALSE) {
library(ggplot2)
library(coefplot)
data(diamonds)
mod1 <- lm(price ~ carat + cut + x, data=diamonds)
extract.coef(mod1)
}

if (FALSE) {
library(ggplot2)
data(diamonds)
library(coefplot)
mod1 <- lm(price ~ carat + cut + x, data=diamonds)
extract.coef(mod1)
}

if (FALSE) {
library(ggplot2)
data(diamonds)
library(coefplot)
mod2 <- glm(price > 10000 ~ carat + cut + x, data=diamonds, family=binomial(link="logit"))
extract.coef(mod2)
}

if (FALSE) {
library(ggplot2)
data(diamonds)
mod3 <- rxLinMod(price ~ carat + cut + x, data=diamonds)
extract.coef(mod3)
}

if (FALSE) {
library(ggplot2)
data(diamonds)
mod4 <- rxGlm(price ~ carat + cut + x, data=diamonds)
mod5 <- rxGlm(price > 10000 ~ carat + cut + x, data=diamonds, fmaily="binomial")
extract.coef(mod4)
extract.coef(mod5)
}

if (FALSE) {
library(ggplot2)
data(diamonds)
mod6 <- rxLogit(price > 10000 ~ carat + cut + x, data=diamonds)
extract.coef(mod6)
}

if(requireNamespace('glmnet', quietly=TRUE)){
library(glmnet)
library(ggplot2)
library(useful)
data(diamonds)
diaX <- build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts = TRUE)
diaY <- build.y(price ~ carat + cut + x - 1, data=diamonds)
modG1 <- glmnet(x=diaX, y=diaY)
extract.coef(modG1)
}
#>                   Value SE Coefficient
#> (Intercept) -2172.33231 NA (Intercept)
#> carat        7607.76091 NA       carat
#> cutFair      -870.91269 NA     cutFair
#> cutGood       -50.45493 NA     cutGood
#> cutIdeal      163.14207 NA    cutIdeal

if(requireNamespace('glmnet', quietly=TRUE)){
library(glmnet)
library(ggplot2)
library(useful)
data(diamonds)
diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, 
 contrasts=FALSE)
diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds)
modG1 <- cv.glmnet(x=diaX, y=diaY, k=5)
extract.coef(modG1)
}
#>                     Value SE  Coefficient
#> (Intercept)   1305.104965 NA  (Intercept)
#> carat        10053.977529 NA        carat
#> cutFair      -1510.568409 NA      cutFair
#> cutGood       -354.484329 NA      cutGood
#> cutVery Good    22.573009 NA cutVery Good
#> cutPremium      -4.642002 NA   cutPremium
#> cutIdeal       319.428622 NA     cutIdeal
#> x             -950.756929 NA            x

# \dontshow{
if(requireNamespace('maxLik', quietly=TRUE))
{
# }
library(maxLik)
loglik <- function(param) {
 mu <- param[1]
 sigma <- param[2]
 ll <- -0.5*N*log(2*pi) - N*log(sigma) - sum(0.5*(x - mu)^2/sigma^2)
ll
}
x <- rnorm(1000, 1, 2) # use mean=1, stdd=2
N <- length(x)
res <- maxLik(loglik, start=c(0,1)) # use 'wrong' start values
extract.coef(res)
}
#> Loading required package: miscTools
#> 
#> Please cite the 'maxLik' package as:
#> Henningsen, Arne and Toomet, Ott (2011). maxLik: A package for maximum likelihood estimation in R. Computational Statistics 26(3), 443-458. DOI 10.1007/s00180-010-0217-1.
#> 
#> If you have questions, suggestions, or comments regarding the 'maxLik' package, please use a forum or 'tracker' at maxLik's R-Forge site:
#> https://r-forge.r-project.org/projects/maxlik/
#>      Value         SE Coefficient
#> 1 1.019756 0.06305976           1
#> 2 1.994177 0.04456984           2

# \dontshow{
if(requireNamespace('xgboost', quietly=TRUE)){# }
library(xgboost)
data(diamonds, package='ggplot2')
diaX <- useful::build.x(price ~ carat + cut + x, data=diamonds, contrasts=FALSE)
diaY <- useful::build.y(price ~ carat + cut + x, data=diamonds)
xg1 <- xgb.train(data=xgb.DMatrix(data=diaX, label=diaY), 
booster='gblinear',
objective='reg:squarederror', eval_metric='rmse',
nrounds=50
)
extract.coef(xg1)
extract.coef(xg1, zero_threshold=0)
extract.coef(xg1, feature_names=colnames(diaX))
}
#> # A tibble: 8 × 3
#>     Value    SE Coefficient 
#>     <dbl> <dbl> <chr>       
#> 1   115.     NA (Intercept) 
#> 2  7658.     NA carat       
#> 3 -4514.     NA cutFair     
#> 4 -3403.     NA cutGood     
#> 5 -3014.     NA cutVery Good
#> 6 -3086.     NA cutPremium  
#> 7 -2726.     NA cutIdeal    
#> 8    81.7    NA x