lm - R - interaction with only one factor level in regression -
in regression model possible include interaction 1 dummy variable of factor? example, suppose have:
x: numerical vector of 3 variables (1,2 , 3) y: response variable z: numerical vector is possible build model like:
y ~ factor(x) + factor(x) : z but include interaction 1 level of x? realize create separate dummy variable each level of x, simplify things if possible.
really appreciate input!!
one key point you're missing when see important effect x2:z, doesn't mean x interacts z when x == 2, means the difference between x == 2 , x == 1 (or whatever reference level is) interacts z. it's not level of x interacting z, it's 1 of contrasts has been set x.
so 3 level factor default treatment contrasts:
df <- data.frame(x = sample(1:3, 10, true), y = rnorm(10), z = rnorm(10)) df$x <- factor(df$x) contrasts(df$x) 2 3 1 0 0 2 1 0 3 0 1 if think first contrast important, can create new variable compares x == 2 x == 1, , ignores x == 3:
df$x_1vs2 <- na df$x_1vs2[df$x == 1] <- 0 df$x_1vs2[df$x == 2] <- 1 df$x_1vs2[df$x == 3] <- na and run regression using that:
lm(y ~ x_1vs2 + x_1vs2:z) r lm
No comments:
Post a Comment