本文介绍了为什么残差不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果有
dd <- structure(list(x = c(-0.27461139896373, -0.36715415967394, -0.396878987664827,
-0.46247810661517, -0.348554552166752, -0.312871287128712, -0.305359246171965,
-0.297850026219192, -0.38399462004035, -0.442901234567901, -0.436306074264866,
-0.316390041493775), y = c(0.805840995132504, 1.9359410430839,
0.820987654320988, 1.4328231292517, 0.91156462585034, 1.75308641975309,
1.15646258503401, 1.42795138888889, 1.25575279421433, 3.13271604938272,
0.825788751714678, 0.583974649162517), x2 = c(6.75, 4, 7.75,
6.5, 5.33333333333333, 5.41666666666667, 5.16666666666667, 4.16666666666667,
4.08333333333333, 2.83333333333333, 5.91666666666667, 5.75),
g = c("a1", "a3", "a5", "a6", "a2", "a1", "a2", "a1", "a2",
"a1", "a3", "a4")), .Names = c("x", "y", "x2", "g"), class = "data.frame", row.names = c(NA,
-12L))
并执行模型:
mm <- lm(y ~ x + x2 + g,
data = dd)
summary(mm)
我们可以通过两种方式获得残差:
we can get residuals in two ways:
res <- residuals(mm,type="partial")
partial.residuals <- resid(mm) + mm$coefficients["x"] * dd$x
为什么这些结果不同?
res[,1]
partial.residuals
我想在x上绘制y的部分残差
I want to plot the partial residuals of y on x
推荐答案
resid(mm, type='partial')
的列等于工作残差(即mm$residuals
)加上模型系数乘以相应的中心 >回归器.
The columns of resid(mm, type='partial')
are equivalent to the working residuals (i.e. mm$residuals
) plus the model coefficients multiplied by the respective centred regressors.
例如:
mm <- lm(y ~ x + x2 + g, data=dd)
residuals(mm,type="partial")
x x2 g
# 1 -0.18983646 -0.611273231 0.31089856
# 2 0.23116764 0.790012984 0.01595798
# 3 0.08354355 -1.069032332 0.46956893
# 4 0.24117753 -0.522368071 0.37710616
# 5 -0.02863590 -0.008204446 -0.38061524
# 6 0.17430042 0.244034093 0.58309734
# 7 0.14337349 0.340491370 -0.10480799
# 8 -0.69749887 -0.045005009 -0.25260602
# 9 -0.23111199 0.250821620 -0.66825343
# 10 0.42415724 1.311203154 0.52049360
# 11 -0.04076612 -0.486310617 -0.42214709
# 12 -0.10987052 -0.194369515 -0.44869282
# attr(,"constant")
# [1] 1.336908
resid(mm) + mm$coefficients['x'] * (dd$x - mean(dd$x))
# 1 2 3 4 5 6 7
# -0.18983646 0.23116764 0.08354355 0.24117753 -0.02863590 0.17430042 0.14337349
# 8 9 10 11 12
# -0.69749887 -0.23111199 0.42415724 -0.04076612 -0.10987052
resid(mm) + mm$coefficients['x2'] * (dd$x2 - mean(dd$x2))
# 1 2 3 4 5 6
# -0.611273231 0.790012984 -1.069032332 -0.522368071 -0.008204446 0.244034093
# 7 8 9 10 11 12
# 0.340491370 -0.045005009 0.250821620 1.311203154 -0.486310617 -0.194369515
这篇关于为什么残差不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!