1

Rがカテゴリ値からダミー変数を自動的に作成することは知っていますが、参照値も自動的に選択します(アルファベット順だと思いますか?)。値の名前を変更せずに別の値を参照に指定するにはどうすればよいですか? おそらく、因子 a、b、c を好きな順序でラベル付けし直すことができると思いますが、それはちょっと面倒に思えます。

わかりやすくするために、例を作ります。要因が色で、値がredbluegreen、およびyellowであるとしましょう。

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

この場合の切片はcolor = blueの場合になりますが、 yellowにしたいと思います。どうすればそれを行うことができますか?

4

1 に答える 1

3

使用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)

そして、このモデルは、ref カテゴリ ' yellow'として使用する推定値を提供します。

于 2012-07-30T18:33:04.370 に答える