1
votes

Background: Species richness scales to the negative -0.75 of body weight. However, when I fit my data, I get a value of 0.57. A friend told me that the summary(lm) results prints the 'best fit' slope of the data. Nevertheless, I'm wondering if I can create a regression plot wherein I force the slope to be -0.75 like the literature.

My code is:

y value

log.nspecies.dec = c(3.05, 2.95, 2.97, 2.98, 2.84, 2.85, 2.83, 2.71, 2.64, 2.62, 2.58, 2.37, 2.26, 2.17, 2.00, 1.88, 1.75, 1.62, 1.36, 1.30, 1.08, 1.20, 0.90, 0.30, 0.70, 0.30, 0.48, 0.00, 0.30, 0.00)

x value

logbio.dec = c(2.1, 2.3, 2.5, 2.7, 2.9, 3.1, 3.3, 3.7, 3.9, 4.1, 4.3, 4.5, 4.7, 4.9, 5.1, 5.3, 5.5, 5.7, 5.9, 6.1, 6.3, 6.5, 6.7, 6.9, 7.1, 7.3, 7.5, 7.7, 7.9)

plot a barplot and superimpose a regression line

name the y variables with the x

names(log.nspecies.dec) = logbio.dec

order the y variables

log.nspecies.dec = log.nspecies.dec[order (as.numeric(names(log.nspecies.dec)))]

do the barplot

xpos = barplot(log.nspecies.dec, las = 2, space = 0)

lm.fit = lm(log.nspecies.dec ~ as.numeric(names(log.nspecies.dec))) summary(lm.fit)

y.init = lm.fit$coefficients[2] * as.numeric(names(log.nspecies.dec))1 + lm.fit$coefficients1

y.end = lm.fit$coefficients[2] * as.numeric(names(log.nspecies.dec))[length(log.nspecies.dec)] + lm.fit$coefficients1

segments(xpos1, y.init, xpos [length(xpos)], y.end, lwd = 2, col = 'red')

title(main = 'ln Number of species ~ lm Weight')

coef(lm.fit)

gives a result wherein the slope is 0.57. How do I force the slope to -0.75? enter image description here

1

1 Answers

0
votes

You can use offset to fix the y-intercept at a negative value. For example

## Example data
x = 1:10
y = -2 + 2* x

# Fit the model
(m = lm(y ~ 0 + x, offset = rep(-2, length(y))))


#Call:
#lm(formula = y ~ 0 + x, offset = rep(-2, length(y)))

#Coefficients:
#x  
#2  

The output correctly identifies the gradient as 2.

The reason your code doesn't work is that you are using abline(). Looking at ?abline, it states that to draw the line it will in turn call coef(MODEL). When you use offset, the coef function doesn't return the y-intercept.

R> coef(m)
x 
2 

Hence abline draws the wrong line.


If the intercept is changed, the code still works

x = 1:10
y = 2 + 2*x
lm(y ~ 0 + x, offset = rep(2, length(y)))