在R中的lm()中选择因子级别作为虚拟基准


10

可以说我正在X1和X2上回归Y,其中X1是一个数字变量,X2是一个具有四个级别(A:D)的因数。有什么方法可以编写线性回归函数,lm(Y ~ X1 + as.factor(X2))以便我可以选择特定级别的X2(例如B)作为基线?

Answers:


14

您可以relevel()用来更改因子的基准水平。例如,

> g <- gl(3, 2, labels=letters[1:3])
> g
[1] a a b b c c
Levels: a b c
> relevel(g, "b")
[1] a a b b c c
Levels: b a c
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.