Glm switches coefficient names for interaction

I am using R code:

dat<-data.frame(p1=c(0,1,1,0,0), GAMMA.1=c(1,2,3,4,3), VAR1=c(2,2,1,3,4), GAMMA.2=c(1,1,3,4,1))
form <- p1 ~ GAMMA.1:VAR1 + GAMMA.2:VAR1
mod <- glm(formula=form, data=dat, family=binomial)
(coef <- coefficients(mod))

# (Intercept) GAMMA.1:VAR1 VAR1:GAMMA.2 
#   1.7974974   -0.2563667   -0.2181079 

As we can see, the names coeffor the interaction are GAMMA.2:VAR1not in the same order as in form(instead, we have VAR1:GAMMA.2). For several reasons, I need a conclusion.

# (Intercept) GAMMA.1:VAR1   GAMMA.2:VAR1
#   1.7974974   -0.2563667   -0.2181079 

without changing the names of the coefficients afterwards. In particular, I need the same names for the coefficients that I used in the object form(without switching, as in the code above). May I say glm()do not switch interaction names?

+3
source share
2 answers

- , . terms.formula, termsform, C. , termsform, , ( keep.order , , ).

terms.formula "" termsform, terms.formula , , ? .


terms.formula , , .

dat<-data.frame(p1=c(0,1,1,0,0), GAMMA.1=c(1,2,3,4,3), VAR1=c(2,2,1,3,4), GAMMA.2=c(1,1,3,4,1))
form <- p1 ~ GAMMA.1:VAR1 + GAMMA.2:VAR1
new.names<-labels(terms(form,data=dat,keep.order=TRUE))
names(new.names)<-as.character(form[[3]][-1])
new.names
# GAMMA.1:VAR1   GAMMA.2:VAR1 
# "GAMMA.1:VAR1" "VAR1:GAMMA.2" 

, .

+2

.

- , . GAMMA.1, VAR1, GAMMA.2. , :

form <- p1 ~ VAR1:GAMMA.1 + VAR1:GAMMA.2
mod <- glm(formula=form, data=dat, family=binomial)
coefficients(mod)

# (Intercept) VAR1:GAMMA.1 VAR1:GAMMA.2 
#   1.7974974   -0.2563667   -0.2181079 

, :

rhs_terms <- c("(Intercept)",as.character(form[[3]][2:length(form[[3]])]))
(coef <- setNames(coefficients(mod), rhs_terms))

# (Intercept) GAMMA.1:VAR1 GAMMA.2:VAR1 
#   1.7974974   -0.2563667   -0.2181079 
+1

All Articles