How to plot a linear regression to a double logarithmic R plot?

我只是一个虾纸丫 提交于 2019-12-01 21:55:13
lines(log(x), exp(predict(model, newdata=list(x=log(x)))) ,col="red")

The range of values for x plotted on the log-scale and for log(x) being used as the independent variable are actually quite different. This will give you the full range:

lines(x, exp(predict(model, newdata=list(x=x))) ,col="red")

Your line is being plotted, you just can't see it in the window because the values are quite different. What is happening when you include the log='xy' argument is that the space underneath the plot (so to speak) is being distorted (stretched and/or compressed), nonetheless, the original numbers are still being used. (Imagine you are plotting these points by hand on graph paper; you are still marking a point where the faint blue graph lines for, say, (1,500) cross, but the graph paper has been continuously stretched such that the lines are not equally spaced anymore.) On the other hand, your model is using the transformed data.

You need to make your plot with the same transformed data as your model, and then simply re-mark your axes in a way that will be sufficiently intuitively accessible. This is a first try:

plot(log(x), log(y), axes=FALSE, xlab="X", ylab="Y")
box()
axis(side=1,     at=log(c(1,2, 10,20, 100,200)), 
             labels=c(    1,2, 10,20, 100,200))
axis(side=2,     at=log(c(125,135, 250,260, 350, 500)), 
             labels=c(    125,135, 250,260, 350, 500))
abline(model, col="red")

Instead of transforming the axes, plot the log-transformed x and y.

plot(log(x), log(y))
abline(model, col="red")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!