使用 par() 生成回归诊断图时,abline 不添加行
`abline` does not add line when producing regression diagonstic plots with `par()`
我正在使用 par()
函数绘制多面板图,我想在第二个图上添加一条线...
par(mfrow = c(2, 2))
hist(model$residuals) # model is some predefined lm object
plot((model$residuals + model$fitted.values) ~ model$fitted.values)
# Now I want to add a line (or points or curve) to only above plot like
abline(model$coef) # but this doesn't work
qqnorm(model$residuals) # some more plots, doesn't matter which
有什么帮助吗?我不打算使用 ggplot()
并希望保持简单。
问题不是你想的那样par
;这仅仅是因为您向 abline
提供了不合适的值。您多次更改了问题,表明您不知道应该为不同的几个地块添加哪一行。我现在将澄清这一点,假设 mod
是您的拟合模型。
残差v.s。装
with(mod, plot(fitted.values, residuals))
abline(h = 0) ## residuals are centred, so we want a horizontal line
已装 v.s。回应
with(mod, plot(fitted.values + residuals, fitted.values))
abline(0, 1) ## perfect fit has `fitted = response`, so we want line `y = x`
带回归线的散点图
v <- attr(mod$terms, "term.labels") ## independent variable name
with(mod, plot(model[[v]], fitted.values + residuals)) ## scatter plot
abline(mod$coef) ## or simply `abline(mod)`, for add regression curve
可重现的例子
set.seed(0)
xx <- rnorm(100)
yy <- 1.3 * xx - 0.2 + rnorm(100, sd = 0.5)
mod <- lm(yy ~ xx)
rm(xx, yy)
par(mfrow = c(2,2))
with(mod, plot(fitted.values, residuals))
abline(h = 0)
with(mod, plot(fitted.values + residuals, fitted.values))
abline(0, 1)
v <- attr(mod$terms, "term.labels") ## independent variable name
with(mod, plot(model[[v]], fitted.values + residuals)) ## scatter plot
abline(mod$coef) ## or simply `abline(mod)`
正如@ZheyuanLi 所说,很难看到你到底想要什么。您的一些问题似乎是由于添加了与现有绘图限制不重叠的行。
model <- lm(Illiteracy~Income,data.frame(state.x77))
par(mfrow = c(2, 2))
hist(model$residuals)
plot(model$residuals ~ model$fitted.values)
plot((model$residuals+model$fitted.values) ~ model$fitted.values)
绘图后立即添加元素效果很好:
abline(a=0,b=1)
如果您想返回并向前一帧添加元素怎么办?这有点难。将绘图重置为第 1 行第 2 列:这 不会 将我们置于上一个绘图的绘图框架内,它只是让我们准备好在此子框架中绘图。
par(mfg=c(1,2))
我们想再次设置相同的绘图框架:我们将通过再次绘制相同的东西(确保相同的轴限制等)来作弊,但关闭绘图的所有方面(new=FALSE
表示我们不会将之前的情节清空):
plot(model$residuals ~ model$fitted.values,
type="n",new=FALSE,axes=FALSE,ann=FALSE)
abline(h=0,col=2)
基础图形确实不是为修改现有地块而设计的;如果你想做很多,你应该看看 grid
图形系统(lattice
和 ggplot2
图形是建立在它之上的)。
我正在使用 par()
函数绘制多面板图,我想在第二个图上添加一条线...
par(mfrow = c(2, 2))
hist(model$residuals) # model is some predefined lm object
plot((model$residuals + model$fitted.values) ~ model$fitted.values)
# Now I want to add a line (or points or curve) to only above plot like
abline(model$coef) # but this doesn't work
qqnorm(model$residuals) # some more plots, doesn't matter which
有什么帮助吗?我不打算使用 ggplot()
并希望保持简单。
问题不是你想的那样par
;这仅仅是因为您向 abline
提供了不合适的值。您多次更改了问题,表明您不知道应该为不同的几个地块添加哪一行。我现在将澄清这一点,假设 mod
是您的拟合模型。
残差v.s。装
with(mod, plot(fitted.values, residuals))
abline(h = 0) ## residuals are centred, so we want a horizontal line
已装 v.s。回应
with(mod, plot(fitted.values + residuals, fitted.values))
abline(0, 1) ## perfect fit has `fitted = response`, so we want line `y = x`
带回归线的散点图
v <- attr(mod$terms, "term.labels") ## independent variable name
with(mod, plot(model[[v]], fitted.values + residuals)) ## scatter plot
abline(mod$coef) ## or simply `abline(mod)`, for add regression curve
可重现的例子
set.seed(0)
xx <- rnorm(100)
yy <- 1.3 * xx - 0.2 + rnorm(100, sd = 0.5)
mod <- lm(yy ~ xx)
rm(xx, yy)
par(mfrow = c(2,2))
with(mod, plot(fitted.values, residuals))
abline(h = 0)
with(mod, plot(fitted.values + residuals, fitted.values))
abline(0, 1)
v <- attr(mod$terms, "term.labels") ## independent variable name
with(mod, plot(model[[v]], fitted.values + residuals)) ## scatter plot
abline(mod$coef) ## or simply `abline(mod)`
正如@ZheyuanLi 所说,很难看到你到底想要什么。您的一些问题似乎是由于添加了与现有绘图限制不重叠的行。
model <- lm(Illiteracy~Income,data.frame(state.x77))
par(mfrow = c(2, 2))
hist(model$residuals)
plot(model$residuals ~ model$fitted.values)
plot((model$residuals+model$fitted.values) ~ model$fitted.values)
绘图后立即添加元素效果很好:
abline(a=0,b=1)
如果您想返回并向前一帧添加元素怎么办?这有点难。将绘图重置为第 1 行第 2 列:这 不会 将我们置于上一个绘图的绘图框架内,它只是让我们准备好在此子框架中绘图。
par(mfg=c(1,2))
我们想再次设置相同的绘图框架:我们将通过再次绘制相同的东西(确保相同的轴限制等)来作弊,但关闭绘图的所有方面(new=FALSE
表示我们不会将之前的情节清空):
plot(model$residuals ~ model$fitted.values,
type="n",new=FALSE,axes=FALSE,ann=FALSE)
abline(h=0,col=2)
基础图形确实不是为修改现有地块而设计的;如果你想做很多,你应该看看 grid
图形系统(lattice
和 ggplot2
图形是建立在它之上的)。