0

enter image description here!

Please see the above picture to see the summary

I want each element of the summary to be converted into a list. This is the summary data and I want to create separate lists such as

predictors = ['Age','WorkClassLocal-gov', ..., HoursWork]
z value = [14.762, -6.719, ..., 16.714]
pr>|z| = [2e-16,6.46e-10,...,2e-16]
...

The summary is as follows

Call: glm(formula = Salary ~ ., family = binomial, data = train) 

Coefficients: 
                     Estimate Std. Error z value Pr(>|z|)
(Intercept)        -7.614e+00  4.525e-01 -16.826  < 2e-16 *** 
Age                 2.626e-02  1.779e-03  14.762  < 2e-16 *** 
WorkClassLocal-gov -7.214e-01  1.168e-01  -6.179 6.46e-10 *** 
HoursWork           2.965e-02  1.774e-03  16.714  < 2e-16 ***

I am not able to do this. Please help me do this.

ekoam
  • 8,744
  • 1
  • 9
  • 22
lala
  • 11
  • 5
  • I want each element of the summary to be converted into a list. This is the summary data and I want to create separate lists such as predictors = ['Age','WorkClassLocal-gov', ..., HoursWork] z value = [14.762, -6.719, ..., 16.714] pr>|z| = [2e-16,6.46e-10,...,2e-16] – lala Oct 23 '20 at 01:50
  • The summary is as follows : Call: glm(formula = Salary ~ ., family = binomial, data = train) Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -7.614e+00 4.525e-01 -16.826 < 2e-16 *** Age 2.626e-02 1.779e-03 14.762 < 2e-16 *** WorkClassLocal-gov -7.214e-01 1.168e-01 -6.179 6.46e-10 *** HoursWork 2.965e-02 1.774e-03 16.714 < 2e-16 *** – lala Oct 23 '20 at 01:50
  • Some of those (as I recall) are calculated on the fly by the summary function. Your best bet is probably to look at the `summary.glm` code and borrow the parts you need. – Gregor Thomas Oct 23 '20 at 02:52
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Images aren't helpful because we can't copy/paste the code or data into R to test. – MrFlick Oct 23 '20 at 03:00

1 Answers1

1

We don't have your data to work with, but we can show the principle of how this can be done using the built-in mtcars data set.

First we create a model:

model <- glm(mpg ~ ., data = mtcars)

Next, we store the result of the summary function, which actually produces a list-like object of class summary.glm.

model_summary <- summary(model)

The coefficient table is stored as a matrix inside this object, as a member called coefficients:

model_summary$coefficients
#>                Estimate  Std. Error    t value   Pr(>|t|)
#> (Intercept) 12.30337416 18.71788443  0.6573058 0.51812440
#> cyl         -0.11144048  1.04502336 -0.1066392 0.91608738
#> disp         0.01333524  0.01785750  0.7467585 0.46348865
#> hp          -0.02148212  0.02176858 -0.9868407 0.33495531
#> drat         0.78711097  1.63537307  0.4813036 0.63527790
#> wt          -3.71530393  1.89441430 -1.9611887 0.06325215
#> qsec         0.82104075  0.73084480  1.1234133 0.27394127
#> vs           0.31776281  2.10450861  0.1509915 0.88142347
#> am           2.52022689  2.05665055  1.2254035 0.23398971
#> gear         0.65541302  1.49325996  0.4389142 0.66520643
#> carb        -0.19941925  0.82875250 -0.2406258 0.81217871

We can easily convert this to a data frame, and convert its row names to their own column:

coefs <- as.data.frame(model_summary$coefficients)

coefs$variable <- rownames(coefs)

Although a data frame is actually a list of variables (and it would be more useful to leave the result in data frame format for most applications), if you want to convert it to a list you can simply do:

as.list(coefs)
#> $Estimate
#>  [1] 12.30337416 -0.11144048  0.01333524 -0.02148212  0.78711097 -3.71530393
#>  [7]  0.82104075  0.31776281  2.52022689  0.65541302 -0.19941925
#> 
#> $`Std. Error`
#>  [1] 18.71788443  1.04502336  0.01785750  0.02176858  1.63537307  1.89441430
#>  [7]  0.73084480  2.10450861  2.05665055  1.49325996  0.82875250
#> 
#> $`t value`
#>  [1]  0.6573058 -0.1066392  0.7467585 -0.9868407  0.4813036 -1.9611887
#>  [7]  1.1234133  0.1509915  1.2254035  0.4389142 -0.2406258
#> 
#> $`Pr(>|t|)`
#>  [1] 0.51812440 0.91608738 0.46348865 0.33495531 0.63527790 0.06325215
#>  [7] 0.27394127 0.88142347 0.23398971 0.66520643 0.81217871
#> 
#> $variable
#>  [1] "(Intercept)" "cyl"         "disp"        "hp"          "drat"       
#>  [6] "wt"          "qsec"        "vs"          "am"          "gear"       
#> [11] "carb"
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87