hamilthj
hamilthj

Reputation: 153

Fitting a regression line to graph with log axes in R

I'm trying to make a simple body size/weight regression graph with log axes but I can't seem to fit a straight regression line to the graph.

I've tried using the untf function but no line is being generated.

Here's a simplified sample of what I'm trying:

plot(Average.BL~Wet.weight, log="xy")
reg=lm(log(Average.BL)~log(Wet.weight))
abline(reg, untf=FALSE)

I've looked at previous questions about the same problem but can't get any solutions to work for me.

Example of graph generated

Upvotes: 2

Views: 9236

Answers (4)

Federico Rotolo
Federico Rotolo

Reputation: 57

In addition to previous answers, if you want to preserve the original scale for the axes tick labels, you can use the following code:

plot(log(Average.BL) ~ log(Wet.weight), axes = FALSE)

axis(1, at = axTicks(1),
     labels = round(exp(axTicks(1)), 1))
axis(2, at = axTicks(2),
     labels = round(exp(axTicks(2)), 1))
box()

reg=lm(log(Average.BL)~log(Wet.weight))
abline(reg)

Upvotes: 0

tikysal
tikysal

Reputation: 21

You can still plot the regression line directly to the log-log plot. You just need to use base 10 for the log (since that is what is used for the log-log plot).

plot(mtcars$mpg~mtcars$wt, log="xy") 
reg=lm(log(mtcars$mpg,base=10)~log(mtcars$wt,base=10))
abline(reg, untf=F)

resulting figure

Upvotes: 0

hamilthj
hamilthj

Reputation: 153

I have found that if I generate a linear regression of the log transformed data as follows:

plot(logWet.weight~logAverageBL, data=mtrosslog)
reg<-lm(logWet.weight~logAverageBL, data=mtrosslog)

Call:
lm(formula = logWet.weight ~ logAverageBL, data = mtrosslog)

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -3.73798    0.11997  -31.16   <2e-16 ***
logAverageBL  2.86705    0.09002   31.85   <2e-16 ***

And then generating a plot with log-log axes, I can fit the regression generated from the log transformed data (reg) to this plot of untransformed data:

plot(Wet.weight~Average.BL, data = mtross, log = 'xy')
ablineclip(reg)

Regression line generated from log transformed data plotted on graph of untransformed data with log-log axes

Upvotes: 0

Sandipan Dey
Sandipan Dey

Reputation: 23101

Plotting the original variables in the log scale is different from plotting (the fitted regression line with) the log-transformed variables in the original scale, we should do the latter to get the desired result (plot the fitted regression line with the log-transformed variables in the original scale), examples with mtcars dataset:

plot(log(mtcars$mpg)~log(mtcars$wt)) # plot the log-transformed variables, not in log-scale  
reg=lm(log(mtcars$mpg)~log(mtcars$wt))
abline(reg, untf=F)

enter image description here

Another option is to plot everything in log scale but fit the regression line in the original scale.

plot(mtcars$mpg~mtcars$wt, log="xy") 
reg=lm(mtcars$mpg~mtcars$wt)
abline(reg, untf=T)

enter image description here

When we want to combine the log-scale plot with regression fit with log-tranformed, the intercept computed with OLS however is not in log-scale, so I guess we need to do some log transformation with it, but it produces the belowe plot, with the correct slope of the line but the wrong offset.

plot(mtcars$mpg~mtcars$wt, log="xy") 
reg=lm(log(mtcars$mpg)~log(mtcars$wt))
abline(log(reg$coefficients[1]), reg$coefficients[2], untf=F)

enter image description here

Upvotes: 4

Related Questions