我打算使用 R 对多个线性回归的残差与9个预测变量的9个图进行比较 . 我知道我可以为9个变量中的每个变量编写9行绘图函数,但我相信有一种优雅的方法可以用短循环绘制它们 . (让我们说 y 和 x1 ... x9 )
R
y
x1
x9
天真的方式:
plot(x1, residual); plot(x2, residual).....
有更好的方法吗?
以下是使用ggplot2的示例:
fit <- lm(Sepal.Length ~ Petal.Length + Petal.Width, data=iris) iris$resid <- residuals(fit) library(reshape2) plotDF <- melt(iris[, c("Petal.Length", "Petal.Width", "resid")], id="resid") library(ggplot2) ggplot(plotDF, aes(x=value, y=resid)) + geom_point() + facet_wrap(~ variable)
1 回答
以下是使用ggplot2的示例: