# Simple Linear Regression and Analysis

I’ve been trying to get more accustomed with using Hadley Wickham’s ggplot2 so today I’ll be going over simple linear regression and some easy ways to analyze the models you’ve created. ggplot2 is a phenomenal package for data visualization and I am actually lucky enough to have taken a class with Hadley at Rice University.

First we’ll load some packages and attach the “fat” dataset:

I loaded faraway to have access to the dataset. gridExtra will be used later on for viewing multiple plots at once. Then, I created several linear models with Brozek (Brozek is an estimate of body fat % using density, click link for more information) as the response variable and other various “fat” variables as predictors.

I predict that age will not be a very good predictor of body fat percentage, while all of the other variables will be. My suspicions are confirmed after I analyze the summaries of each linear model and find the correlations of brozek with respect to each of the predictors.

We can check the R2 values of h1:h4 to check how well the regression line fits the set of data by using h1\$r.squared and cycling it from h1 to h4. The R2 values respectively are 0.3759604, 0.6621178, 0.08362132, 0.3150402. As we can see, age has a very low R2 and thus the linear model is a fairly bad predictor of the data.

The correlation tells the same story. The respective correlations are 0.6131561, 0.8137062, 0.2891735, 0.5612844. The abdom model had the largest R2 value and cor(brozek, abdom) was also highest amongst the pairs. It seems as if abdominal size is good indicator of body fat percentage.

Next we can plot the residuals. These graphs help visualize that the sum of the residuals must always be 0. There doesn’t seem to be much heteroscedasticity present in these plots so the variance of our residuals is almost constant. It is always good to check the validity of our model because when conducting a regression we assume that the variance of the residuals is constant.

Finally, we can use ggplot2 to make some very aesthetic graphs of the regressions.

The straight lines represent the linear model fit. It’s pretty easy to see which plots exhibit a good linear relationship.