我有三维数据,我想要拟合回归线(我使用lm(x1 ~ x2+x3)
)。
要计算线上每个点的距离,我使用residuals(fit)
。
问题是如何从之前的拟合线计算另一组点的距离(残差)?
答案 0 :(得分:0)
拟合回归平面并打印残差:
data(trees)
fit <- lm(Volume ~ Girth + Height, data=trees)
(res <- with(trees, Volume - (fit$coefficients[[3]]*Height + fit$coefficients[[2]]*Girth + fit$coefficients[[1]]))) # = residuals(fit)
现在有了新数据:
trees_new <- trees * runif(nrow(trees))
(res_new <- with(trees_new, Volume - (fit$coefficients[[3]]*Height + fit$coefficients[[2]]*Girth + fit$coefficients[[1]])))