Get startedGet started for free

Finding the Slope and Intercept Using lm()

So you conducted your experiment when you gave people money and measured how much they like you. You calculated the slope and the intercept manually, so that you have the equation \(liking = 1.501 + 0.778 * money\).

Another way of calculating the intercept and slope is through the R function lm(). When you tell lm() your regression model, it produces your intercept and slope coefficients. You give lm() your model by first specifying the y (or response) variable, followed by a ~ symbol, then your x (or predictor variable). For example: lm(y~x). Try it for yourself in your script!

This exercise is part of the course

Inferential Statistics

View Course

Exercise instructions

  • In your script, add a line of code that calculates the intercept and slope using lm()
  • Hit 'submit' and look at the answer!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Vector containing the amount of money you gave participants
money  <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

# Vector containing the amount the participants liked you
liking <- c(2.2, 2.8, 4.5, 3.1, 8.7, 5.0, 4.5, 8.8, 9.0, 9.2)

# Calculate the intercept and slope using lm()
Edit and Run Code