Simple Linear Regression in R

R.6.2: Simple Linear Regression

Fitting a linear regression model with one predictor and one response variable is very simple in R. The command

lm(yvar~xvar,data=dataset,pch=20)

creates a simple linear model for the data. The ~ character is used to separate the predictor variables from the response variable. Note:pch=20 is an aesthetic modification that fills in the plotted circles Using the “heights” data the command would look like:

modslr<-lm(son~father,data=heights)

Remember to store the lm(linear model) image so that you can run summaries or further tests on it. I have saved it, in the above example as “modslr.” There are many different ways to view the coefficients. Two of the simplest are included below. Note that to use either of these commands; you first must create a linear model.

coefficients(modslr)

summary(modslr)

The summary command provides much more information about the regression model. Take a minute to look for these pieces of information

  1. Coefficients (Slope and Y-intercept)
  2. Standard Errors for both the slope and y-intercept
  3. P-values
  4. T-values(and whether they are significant or not)
  5. R-squared (coefficient of determination)

If we wanted to represent this model visually we would apply this line to the plot using the abline() command.

plot(son~father,pch=20)
    abline(modslr,col="blue") 

Comment are closed.