```{r setup, include=FALSE} knitr::opts_chunk$set(echo=TRUE, fig.align="center") ``` #### Statistics for Laboratory Scientists ( 140.615 ) ## Linear Regression #### Example from class: David Sullivan's heme data ```{r} h2o2 <- rep(c(0,10,25,50), each=3) h2o2 pf3d7 <- c(0.3399,0.3563,0.3538, 0.3168,0.3054,0.3174, 0.2460,0.2618,0.2848, 0.1535,0.1613,0.1525) pf3d7 ``` Plot the data, and add the least squares fit line. ```{r} plot(h2o2, pf3d7, xlab="H2O2 concentration", ylab="OD") abline(lsfit(h2o2, pf3d7), col="red", lty=2) ``` ##### The regression of optical density on hydrogen peroxide concentration, using the built-in 'lm' function ```{r} lm.out <- lm(pf3d7 ~ h2o2) lm.out lm.sum <- summary(lm.out) lm.sum attributes(lm.sum) ``` The table of coefficients. ```{r} lm.sum$coef ``` The parameter estimates. ```{r} lm.sum$coef[,1] ``` The residual standard deviation. ```{r} lm.sum$sigma ``` To avoid data glitches, it is a good habit to make a data frame. ```{r} dat <- data.frame(conc=h2o2, od=pf3d7) dat str(dat) plot(dat$conc, dat$od, xlab="H2O2 concentration", ylab="OD") abline(lsfit(dat$conc, dat$od), col="red", lty=2) lm.out <- lm(od ~ conc, data=dat) summary(lm.out) ``` ##### Confidence intervals for the regression parameters ```{r} confint(lm.out) confint(lm.out, level=0.99) ``` ##### Checking model assumptions ```{r} lm.out$fitted lm.out$residuals ``` Alternatively, there are also generic functions. ```{r} fitted(lm.out) residuals(lm.out) ``` The residual qq plot. ```{r} qqnorm(lm.out$residuals, main="") qqline(lm.out$residuals, col="blue", lty=2) ``` Fitted values versus residuals. ```{r} plot(lm.out$fitted, lm.out$residuals, pch=1, xlab="fitted values", ylab="residuals") abline(h=0, col="blue", lty=2) ```