View object created by `glm` as a list in R

In R, the glm regression glm creates an object of class glm , which is a list.

Like a list, I should be able to view an object of the glm class as a list without any formatting. However, as.list does not seem to do this.

For example, if fit is the model installed by the glm function:

 > as.list(fit) Call: glm(formula = V4 ~ V3 + V2 + V1, family = Gamma, data = data) Coefficients: (Intercept) V3 V2 V1 1.349 1.593 1.577 1.127 Degrees of Freedom: 9999 Total (ie Null); 9996 Residual Null Deviance: 2137 Residual Deviance: 2048 AIC: -30180 

On the other hand, other functions that apply to the list work correctly, for example names , which will create 30 names of the corresponding list.

In addition, I can view individual items in the same way as for any other list:

 > fit$coefficients (Intercept) V3 V2 V1 1.349282 1.593067 1.576868 1.127067 

Is there any pre-existing function that allows me to view fit in my list form without formatting?

As I said above, I could create my own function using list names, but this is not necessary for such a simple task.

+4
source share
1 answer

Although fit has a list, it has a glm class, so automatic printing sends the print method print.glm() . As shown below, as.list() saves the class of the object, so it doesn't help you at all.

 fit <- glm(speed~dist, data=cars) ## A silly example class(fit) # [1] "glm" "lm" class(as.list(fit)) # [1] "glm" "lm" exists("print.glm") # [1] TRUE 

Any of the following will print fit as a list.

 unclass(fit) ## Returns and immediately auto-prints object of class "list" ## using print.default() print.default(fit) ## Bypasses method dispatch, directly calling desired print ## method 
+7
source

Source: https://habr.com/ru/post/1487952/


All Articles