```{r setup, include=FALSE} knitr::opts_chunk$set(echo=TRUE, fig.align="center") options(width=110) ``` #### 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) 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) ``` Plot the data, and add the least squares fit line. ```{r} par(las=1) 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 ``` The coefficient of determination. ```{r} lm.sum$r.squared ``` To see how to derive these statistics from scratch, please see the advanced code. It is simple arithmetic following the lecture notes and therefore not really advanced code, I just tucked it away there. Also, 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) ``` The linear regression model. ```{r} lm.out <- lm(od ~ conc, data=dat) summary(lm.out) ``` Plot of the data with least squares regression line.. ```{r} par(las=1) plot(dat$conc, dat$od, xlab="H2O2 concentration", ylab="OD") abline(lm.out, col="red", lty=2) ``` ##### Confidence intervals for the regression parameters ```{r} confint(lm.out) confint(lm.out,level=0.99) ``` To see how to derive these from scratch, also see the advanced code. ##### Checking model assumptions ```{r} plot(lm.out,which=1) plot(lm.out,which=2) ```