Graphing perpendicular offsets in a least squares regression plot in R

Posted by D W on Stack Overflow See other posts from Stack Overflow or by D W
Published on 2010-04-14T17:07:45Z Indexed on 2010/04/14 18:43 UTC
Read the original article Hit count: 691

I'm interested in making a plot with a least squares regression line and line segments connecting the datapoints to the regression line as illustrated here in the graphic called perpendicular offsets: http://mathworld.wolfram.com/LeastSquaresFitting.html

I have the plot and regression line done here:

## Dataset from http://www.apsnet.org/education/advancedplantpath/topics/RModules/doc1/04_Linear_regression.html

## Disease severity as a function of temperature

# Response variable, disease severity
diseasesev<-c(1.9,3.1,3.3,4.8,5.3,6.1,6.4,7.6,9.8,12.4)

# Predictor variable, (Centigrade)
temperature<-c(2,1,5,5,20,20,23,10,30,25)


## Fit a linear model for the data and summarize the output from function lm()
severity.lm <- lm(diseasesev~temperature,data=severity)

# Take a look at the data
plot(
 diseasesev~temperature,
        data=severity,
        xlab="Temperature",
        ylab="% Disease Severity",
        pch=16
)
abline(severity.lm,lty=1)
title(main="Graph of % Disease Severity vs Temperature")

Should I use some kind of for loop and segments http://www.iiap.res.in/astrostat/School07/R/html/graphics/html/segments.html to do the perpendicular offsets? Is there a more efficient way? Please provide an example if possible.

© Stack Overflow or respective owner

Related posts about statistics

Related posts about r