如何绘制线性回归到双对数R图? [英] How to plot a linear regression to a double logarithmic R plot?

查看:399
本文介绍了如何绘制线性回归到双对数R图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

someFactor = 500
x = c(1:250)
y = x^-.25 * someFactor

在双对数图中显示:

plot(x, y, log="xy")

现在,我使用线性模型找出"数据的斜率:

Now I "find out" the slope of the data using a linear model:

model = lm(log(y) ~ log(x))
model

给出:

Call:
lm(formula = log(y) ~ log(x))

Coefficients:
(Intercept)       log(x)  
      6.215       -0.250  

现在,我想将线性回归绘制为一条红线,但是abline无效:

Now I'd like to plot the linear regression as a red line, but abline does not work:

abline(model, col="red")

向图中添加回归线的最简单方法是什么?

What is the easiest way to add a regression line to my plot?

推荐答案

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

在对数刻度上绘制的x值和用作自变量的log(x)的值范围实际上是完全不同的.这将为您提供全方位的服务:

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")

这篇关于如何绘制线性回归到双对数R图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆