Linear Regression Examples

R makes linear regression analysis very easy.

Example 1: Statistics Midterm grades.

The file below contains the grades for all of my Math 121 students from Spring 2013 to Fall 2015.

midtermData = read.csv('http://people.hsc.edu/faculty-staff/blins/StatsExamples/midtermRegressionS13_F15.csv')
head(midtermData)
##   Midterm1 Midterm2
## 1       52       54
## 2       70       73
## 3       86       89
## 4       73       62
## 5       92       85
## 6       47       72
  • Make a scatterplot showing the relationship between Midterm 1 and Midterm 2 grades using the plot() command.

  • Use the cor() function to find the correlation between exam grades. Is it about what you expected?

  • Calculate the slope of the least squares regression line for predicting Midterm 2 grades based on Midterm 1 grades.

  • Now use the lm() function to find the slope and intercept of the least squares regression line.

  • Add the least squares regression line to the scatterplot.

  • Estimate how well a student who got a 60 on Midterm 1 will do on Midterm 2. What about a student who got a 90?

Example 2: Marriage Ages

The next file contains the ages of husbands and wives who got married during one month in one county.

marriages = read.csv('http://people.hsc.edu/faculty-staff/blins/StatsExamples/marriageAges.txt')
head(marriages)
##   Husband_Age Wife_Age
## 1          25       22
## 2          25       32
## 3          51       50
## 4          25       25
## 5          38       33
## 6          30       27

Example 3: Lightning Deaths in USA

The last file contains a record of all lightning strike fatalaties in the USA from 1960 to 2010.

lightning = read.csv('http://people.hsc.edu/faculty-staff/blins/StatsExamples/LightningDeaths.csv')
head(lightning)
##   year deaths
## 1 1960    129
## 2 1961    149
## 3 1962    153
## 4 1963    165
## 5 1964    129
## 6 1965    149