如何从 summary.glm 获取响应变量名称
How to get response variable name from summary.glm
考虑以下因素:
set.seed(1)
y <- rbinom(10, 1, prob=.5)
x <- runif(10)
m <- glm(y~x, family=binomial)
s <- summary(m)
我正在寻找可用于 return "y"
的函数 foo(s)
。
可能有更好的答案,但是
as.character(attributes(s$terms)$variables[[2]])
有效
另一种选择 -
R> strsplit(as.character(s$call)[2],"\s~\s")[[1]][1]
[1] "y"
如果使用terms()
功能,可以做到
with(attributes(terms(m)), as.character(variables[response+1]))
# [1] "y"
这对于许多不同的公式来说应该是稳健的。这与 delete.response()
函数使用的方法类似。
考虑以下因素:
set.seed(1)
y <- rbinom(10, 1, prob=.5)
x <- runif(10)
m <- glm(y~x, family=binomial)
s <- summary(m)
我正在寻找可用于 return "y"
的函数 foo(s)
。
可能有更好的答案,但是
as.character(attributes(s$terms)$variables[[2]])
有效
另一种选择 -
R> strsplit(as.character(s$call)[2],"\s~\s")[[1]][1]
[1] "y"
如果使用terms()
功能,可以做到
with(attributes(terms(m)), as.character(variables[response+1]))
# [1] "y"
这对于许多不同的公式来说应该是稳健的。这与 delete.response()
函数使用的方法类似。