Search code examples
rdatatableregressionnacoefficients

Creating Data Table of Regression Coefficients


I have a model with the following regression coefficient values:

           (Intercept)                 radius              perimeter            compactness          concavepoints 
         -2.3003926746           0.0743984303          -0.0111031732          -2.5826629017           5.3127565914 
         radius.stderr      smoothness.stderr     compactness.stderr       concavity.stderr           radius.worst 
          0.4256225882          16.9805981122          -3.8819567231           0.9488969352           0.1408605366 
         texture.worst             area.worst        concavity.worst         symmetry.worst fractaldimension.worst 
          0.0105317616          -0.0009867991           0.3504860653           0.8536208289           4.7503948408 

I want to make a data table with the variable names in one column, and the corresponding regression coefficient values in the other column. This is what I have tried so far:

var_names = coef(summary(model_B))[, 0]
coef_vals = coef(summary(model_B))[, 1]
data.table(Variables=c(var_names), RegressionCoefficients = c(coef_values))

But I get the following output with the 'Variables' column all NA:


Variables RegressionCoefficients
<dbl> <dbl>
NA  -2.3003926746           
NA  0.0743984303            
NA  -0.0111031732           
NA  -2.5826629017           
NA  5.3127565914            
NA  0.4256225882            
NA  16.9805981122           
NA  -3.8819567231           
NA  0.9488969352            
NA  0.1408605366    

Solution

  • Use names to access the names of the coefficients.

    var_names=names(coef(model_B))
    coef_vals=coef(model_B)
    data.table(Variables=var_names, RegressionCoefficients=coef_vals)
    
         Variables RegressionCoefficients
    1: (Intercept)           2.984208e-16
    2:      radius           1.000000e+00
    3:   perimeter           1.000000e+00