Flo*_*isk 7 plot regression r prediction
给定两个变量,x并且y,我对变量运行dynlm回归,并希望根据其中一个变量绘制拟合模型,并在底部绘制残差,显示实际数据线与预测线的差异.我之前已经看过它并且之前已经完成了,但是对于我的生活,我不记得该怎么做或找到任何解释它的东西.
这让我进入了我有一个模型和两个变量的球场,但我无法得到我想要的图形类型.
library(dynlm)
x <- rnorm(100)
y <- rnorm(100)
model <- dynlm(x ~ y)
plot(x, type="l", col="red")
lines(y, type="l", col="blue")
Run Code Online (Sandbox Code Playgroud)
我想生成一个看起来像这样的图形,您可以看到模型和实际数据相互重叠,残差绘制为底部的单独图形,显示实际数据和模型的偏差. 
这应该做的伎俩:
library(dynlm)
set.seed(771104)
x <- 5 + seq(1, 10, len=100) + rnorm(100)
y <- x + rnorm(100)
model <- dynlm(x ~ y)
par(oma=c(1,1,1,2))
plotModel(x, model) # works with models which accept 'predict' and 'residuals'
Run Code Online (Sandbox Code Playgroud)
这是代码plotModel,
plotModel = function(x, model) {
ymodel1 = range(x, fitted(model), na.rm=TRUE)
ymodel2 = c(2*ymodel1[1]-ymodel1[2], ymodel1[2])
yres1 = range(residuals(model), na.rm=TRUE)
yres2 = c(yres1[1], 2*yres1[2]-yres1[1])
plot(x, type="l", col="red", lwd=2, ylim=ymodel2, axes=FALSE,
ylab="", xlab="")
axis(1)
mtext("residuals", 1, adj=0.5, line=2.5)
axis(2, at=pretty(ymodel1))
mtext("observed/modeled", 2, adj=0.75, line=2.5)
lines(fitted(model), col="green", lwd=2)
par(new=TRUE)
plot(residuals(model), col="blue", type="l", ylim=yres2, axes=FALSE,
ylab="", xlab="")
axis(4, at=pretty(yres1))
mtext("residuals", 4, adj=0.25, line=2.5)
abline(h=quantile(residuals(model), probs=c(0.1,0.9)), lty=2, col="gray")
abline(h=0)
box()
}
Run Code Online (Sandbox Code Playgroud)

你在寻找什么resid(model).试试这个:
library(dynlm)
x <- 10+rnorm(100)
y <- 10+rnorm(100)
model <- dynlm(x ~ y)
plot(x, type="l", col="red", ylim=c(min(c(x,y,resid(model))), max(c(x,y,resid(model)))))
lines(y, type="l", col="green")
lines(resid(model), type="l", col="blue")
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
2395 次 |
| 最近记录: |