2024-10-22

Simple Linear Regression

  • Statistical technique to estimate a linear relationship between variables
  • Can be used to predict outcomes based on trends in a data set
  • Examines how a dependent variable affects an independent variable

Positive Linear Regression Example (ggplot)

Correlation Coefficient (R code)

cor(icecream$Temperature,icecream$Revenue)
## [1] 0.9898408
  • The previous graph shows temperature vs ice cream sales revenue
  • Here, we see a strong, positive linear correlation. As temperature increases, the revenue from ice cream also increases

Regression Line Equation (Math Text)

A linear regression line takes the form of \(y=a+bx\), where \(a\) is the y-intercept and \(b\) is the slope of the line

R code to Set up the Plot

x <- c(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
y <- c(8.5, 7.1, 6.2, 5.4, 4.1, 3.3, 2.4, 1.5, 0.9, -0.2)
negative <- data.frame(x,y)

These are points that are put into a data frame to be graphed.

ggplot of x and y

Previous Regression Line (Math Text and R Code)

cor(x,y)
## [1] -0.9975846

In the previous graph, the regression line shows that x and y are negatively correlated. In this case, the \(b\) in \(y=a+bx\) is negative.

R Code to set up Plotly Plot

gym <- read.csv(file="C:/Users/mystk/Desktop/DAT301/gym_members_exercise_tracking.csv")
myX = gym$Water_Intake..liters.
myY = gym$Calories_Burned
myZ = gym$Age
  • This data will be used to generate a 3D scatter plot

Plotly Plot of Gym Data Set

Plotly Description

  • This 3D scatterplot visualizes a gym member data set, with the x-axis representing the number of liters of water a member drinks per day, the y-axis representing calories burned, and the z-axis representing the member’s age
  • Here, a simple linear regression technique cannot be used because there is more than two dimensions