2024-01-30

Simple Linear Regression

-Simple linear regression displays the linear relationship between the independent (x) and dependent variables (y)

-The linear regression line can help predict the unknown y value for an x value.

-The Equation for Simple Linear Regression:

\(\hat{y}\)= b0 + b1x

\(\hat{y}\) = predicted value of y for a given value of x

b0 = y-intercept

b1 = slope

x = x-coordinate

R Code for First Plot

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
g<-ggplot(mtcars,aes(hp,qsec))+
  geom_point()+
  geom_smooth(method="lm")+
  ggtitle("Plot of 1/4 Mile Time by Horsepower")+
  xlab("Horsepower(hp)")+ylab("1/4 Mile Time(qsec)")

Plot of 1/4 Mile Time by Horsepower

The blue line is the line of simple linear regression.

The grey around the blue line represents the values within the 95% confidence interval.

Equation of the Line of Linear Regression

lm(qsec~hp,mtcars)
## 
## Call:
## lm(formula = qsec ~ hp, data = mtcars)
## 
## Coefficients:
## (Intercept)           hp  
##    20.55635     -0.01846
#This gives the y-intercept and slope.

The equation for the simple linear regression for the plot is given by the formula y~x or in this case (qsec~hp). The equation is:

\(\hat{y}\)= 20.55635+ (-0.01846)x

Using the Linear Regression Line to Solve for \(\hat{y}\)

Using the equation from last slide \(\hat{y}\)= 20.55635+ (-0.01846)x , we will find the predicted value for \(\hat{y}\) given a value of x.

If x=200

\(\hat{y}\) = 20.55635+ (-0.01846)(200)

\(\hat{y}\) = 20.55635 + (-3.692)

\(\hat{y}\) = 16.86435

Multiple Linear Regression

-Multiple linear regression is used to predict a dependent variable (y) based on a set of independent variables (x1,x2,,xi).

-Equation for Multiple Linear Regression:

\(\hat{y}\)= b0 + b1x1 + b2x2 + + bixi

y is the dependent variable

x1 , x2 , , xi are independent variables

Plot with Multiple Linear Regression Lines

This graph shows multiple linear regresson lines based on the independent variables, Sepal Length and species.The multiple linear regression equation would be:

\(\hat{y}\)= 1.6765 + (0.3499)x1 + (-0.9834)x2 + (-1.0075)x3

Example of a 3D Plot with Plane of Linear Regression

This graph represents the 3D plane of linear regression between the independent variables (carats and depths) and the independent variable (prices).