Specifying a reference level in R: function relevel

How to specify a specific level as a reference level in GNU R while applying (generalized) linear regression?

A dataframe dt is given:

y<-rpois(40,1)
x1<-gl(4,10)
x2<-gl(4,1,40)
dt <- data.frame(cbind(y,x1,x2))

Applying poisson regression:

m1<-glm(y~factor(x1)+factor(x2),data=dt)
summary(m1)

The output shows x1=1 and x2=1 are both reference level defined by R automatically. To switch the reference level from x1=1 to x1=2, the function relevel could be used:

m2<-glm(y~relevel(factor(dt$x1),"2")+factor(x2),data=dt)
summary(m2)

No comments:

Post a Comment