# [R] how to plot a logarithmic regression line

David Winsemius dwinsemius at comcast.net
Sun Feb 23 01:57:31 CET 2014

```On Feb 22, 2014, at 1:06 PM, arun wrote:

> HI,
> Try ?curve
>
> fit <- lm(Mean_Percent_of_Range~log(No.ofPoints))
>  coef(fit)
>  #    (Intercept) log(No.ofPoints)
>   #     -74.52645         46.14392
>
>
>
>  plot(Mean_Percent_of_Range ~ No.ofPoints)
>
>
> A.K.
>
>
>
> I realize this is a stupid question, and I have honestly tried to find
> the answer online, but nothing I have tried has worked. I have two
> vectors of data:
>
> "Mean_percent_of_range"
> 10.90000  17.50000  21.86667  25.00000  25.40000  26.76667  29.53333
>  32.36667  43.13333  41.80000 50.56667  49.26667  50.36667  51.93333
>  59.70000  63.96667  62.53333  60.80000  64.23333  66.00000 74.03333
>  70.40000  77.06667  76.46667  78.13333  89.46667  88.90000  90.03333
>  91.60000  94.30000 95.50000  96.20000  96.50000  91.40000  98.20000
>  96.60000  97.40000  99.00000 100.00000
>
> and
> "No.ofPoints"
> 5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
> 39 40 41 42 43
>
> When I plot these, I get a logarithmic curve (as I should for this type of data)
>> plot(Mean_Percent_of_Range ~ No.ofPoints)
>
> All that I want to do is plot best fit regression line for that
> curve. From what I have read online, it seems like the code to do that
> should be
>> abline(lm(log(Mean_Percent_of_Range) ~ log(No.ofPoints)))
> but that gives me a straight line that isn't even close to fitting the data
>
> How do I plot the line and get the equation of that line and a correlation coefficient?

The 'abline' function is not what you want. Use 'lines' to plot multiple points.

Perhaps:

mod <- lm(log(Mean_percent_of_range) ~ log(No.ofPoints))
plot(log(Mean_percent_of_range), log(No.ofPoints))
lines( log(No.ofPoints), predict(mod))
#------------
> summary(mod)

Call:
lm(formula = log(Mean_percent_of_range) ~ log(No.ofPoints))

Residuals:
Min       1Q   Median       3Q      Max
-0.32617 -0.04839  0.00962  0.05316  0.17316

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)       1.19840    0.08060   14.87   <2e-16 ***
log(No.ofPoints)  0.94228    0.02609   36.12   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.09455 on 37 degrees of freedom
Multiple R-squared:  0.9724,	Adjusted R-squared:  0.9717
F-statistic:  1305 on 1 and 37 DF,  p-value: < 2.2e-16

David Winsemius
Alameda, CA, USA

```