Domanda

Is there any way to explicitly specify which group to take as reference group for dummy coding when modeling with lm function in R using categorical variables??

È stato utile?

Soluzione

The simplest way I know of, is to recode the factor up front, so that the level you want to use as a reference is the first one.

You could do this with a function like this:

recodeFactor<-function(f, ref=levels(f)[1])
{
  lvls<-levels(f)
  if(ref== lvls[1]) return(f)

  lvls<-c(ref, setdiff(lvls, ref))
  f<-factor(as.character(f), levels=lvls)
  return(f)
}

Altri suggerimenti

You can just do this in the lm call:

y <- rnorm(100, 0, 1)
x <- c(rbinom(50,1,.5),(2*rbinom(50,1,.5)))
lm(y ~ factor(x,c(1,0,2))) # one way
lm(y ~ factor(x,c(0,1,2))) # another way, etc.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top