How to get response variable name from summary.glm

Consider the following:

set.seed(1)
y <- rbinom(10, 1, prob=.5)
x <- runif(10)
m <- glm(y~x, family=binomial)
s <- summary(m)

I am looking for some function foo(s)that I can use to return "y".

+4
source share
3 answers

May be the best answer, but

as.character(attributes(s$terms)$variables[[2]])

work

+5
source

Another option is

R> strsplit(as.character(s$call)[2],"\\s~\\s")[[1]][1]
[1] "y"
+4
source

If you use the function terms(), you can do

with(attributes(terms(m)), as.character(variables[response+1]))
# [1] "y"

This should be reliable for many different formulas. This is a similar method used by the function delete.response().

+3
source

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


All Articles