当我转换" am"因为,我认为它正确地转换为两个级别" 0"和" 1",但值为2和1,而不是。如何将它们变为原始数据中的0/1?
data(mtcars)
str(mtcars$am)
# num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
mtcars$am <- factor(mtcars$am)
str(mtcars$am)
# Factor w/ 2 levels "0","1": 2 2 2 1 1 1 1 1 1 1 ...
结果是,当我使用abline进行回归和绘图时,我得到了错误的绘图(将1和2视为X值,而不是0和1):
fit <- lm(mpg ~ am, mtcars)
qplot(am, mpg, data = mtcars) + geom_abline(intercept = fit$coef[1], slope = fit$coef[2])
fit
#----
Call:
lm(formula = mpg ~ am, data = mtcars)
Coefficients:
(Intercept) am1
17.147 7.245
答案 0 :(得分:1)
在这里,我将向您展示两种等效的方法来证明它们是等价的:
qplot(am, mpg, data = mtcars) +
#Method 1
geom_abline(intercept = fit$coef[1] - fit$coef[2], slope = fit$coef[2]) +
#Method 2
geom_smooth(method = "lm",se = FALSE,aes(group = 1))
因子编码没有任何问题,您只需调整绘图坐标(或只使用geom_smooth
)。