【问题标题】:Fit Multiple Linear Regression line and calculate residuals from another data in R拟合多元线性回归线并从 R 中的另一个数据计算残差
【发布时间】:2014-01-27 11:10:02
【问题描述】:

我有想要拟合回归线的 3d 数据(我使用 lm(x1 ~ x2+x3))。 为了计算每个点到直线的距离,我使用residuals(fit)

问题是如何计算另一组点与之前拟合线的距离(残差)?

【问题讨论】:

  • 你试过什么吗??那么请把你的代码放在这里

标签: r


【解决方案1】:

拟合回归平面并打印残差:

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]])))

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    相关资源
    最近更新 更多