2012-07-30 60 views
0

我知道R自动从分类值创建虚拟变量,但它也自动选择参考值(我认为按字母顺序?)。如何在不更改值的名称的情况下指定不同的值作为参考?我意识到我可能会按照我喜欢的顺序重新加上因素a,b,c ...,但对我来说这似乎有点奇怪。如何在R中指定任意的虚拟变量对比度?

只是要清楚,我会做一个例子。比方说,因素是颜色和值红色蓝色绿色黄色

mod.lm <- lm(preference ~ color, data = flowers) 

在这种情况下拦截行动将是情况颜色 = 蓝色,但我想让它黄色。我会怎么做呢?

回答

2

使用relevel

# In this case, the reference category is setosa 
model <- lm(Sepal.Length ~ Species, data=iris) 
summary(model) 

# Now I want Virginica to be the reference category 
iris$Species <- relevel(iris$Species, ref='virginica') 
model <- lm(Sepal.Length ~ Species, data=iris) 
summary(model) 

你的情况可能是

flowers$color <- relevel(flowers$color, ref='yellow') 
lm(preference ~ color, data = flowers) 

而且这一模式将使用与裁判类别 'yellow'

+0

感谢给你的估计,即”会做到的! – 2012-07-30 18:47:40