The easiest way to determine if a model formula has only interception

In the syntax of the formula R, sometimes the user specifies a very simple model that has only interception, for example

fit = lm(Response ~ 1, data)

These models allow simplification of relatively more complex models, for example. lm(Response ~ A + B + A:B, ...), and I would like to have an easy way to detect when the RHS equation contains only 1and no other terms. Text manipulations seem possible, but are there other ways to do this using the R class formulaor other methods?

+4
source share
2 answers

terms, , , , LHS ~1:

fit = lm(Response ~ 1, data=data.frame(Response=1:10))
identical(formula(fit)[-2],~1)
  • , , (~,Response,1): , ( LHS), - (RHS). [-2] .

@G.Grothendieck ( ) , update, 0:

identical(update(formula(fit), 0 ~ .), 0 ~ 1) 
+6

-

names(coef(fit))

"()", .


- "terms". , lm . :

f <- Response ~ 1

terms(f) "terms". lmObject .

attr(terms(fit), "intercept")
## to use formula only without actually fitting a model, do
## attr(terms(f), "intercept")

1, ; 0, .

,

length(attr(terms(fit), "term.labels"))
## to use formula only without actually fitting a model, do
## attr(terms(f), "terms.labels")

0, ; 0, .

+7

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


All Articles