绘制线性,主轴和标准主轴回归的残差

时间:2019-07-23 11:47:12

标签: r model linear-regression

我一直在兜圈子,尝试从R中的Warton et al (2006)复制图形。

Warton et al Figure 4

我想强调一下如何为演示文稿绘制残差的方向,并有一个可重现的示例。除了箭头以外,我已经完成了线性模型(图4A),但一直被其他模型卡住。这是线性模型版本的代码,我也将此代码发布为GitHub Gist

## Warton, D. I., Wright, I. J., Falster, D. S., and Westoby, M. (2006). 
# Bivariate line-fitting methods for allometry. 
# Biological Reviews 81, 259–291. doi:10.1017/S1464793106007007.

#Direction in which residuals are measured. 

#Fig 4a (Standard Linear Regression)
set.seed(123)
test.x=1:25
error=rnorm(25,0,25)
test.y=(5*test.x)+200+error

par(family="serif",mar=c(0.5,0.5,0.5,0.5),oma=c(0.25,0.25,0.25,0.25));
plot(test.x,test.y,type="n",axes=F,ylab=NA,xlab=NA,ylim=c(190,325),xlim=c(0,26))
test.lm=lm(test.y~test.x)
test.lm.pred=predict(test.lm,data.frame(test.x=test.x))
lines(test.x,test.lm.pred,lty=2)
segments(test.x,test.y,test.x,test.lm.pred,col="red",lwd=1.5)
points(test.x,test.y,pch=21,bg="grey")
box(lwd=1)

以下是输出:

CodeOutput

任何帮助将有助于开发图4B和图4C的示例。还尝试添加箭头,以显示拟合方向和剩余轴。

0 个答案:

没有答案