Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Check to be sure your scatterplots of miles per gallon against horsepower and weight of the car were included in your attachment. Do the plots

  1. Check to be sure your scatterplots of miles per gallon against horsepower and weight of the car were included in your attachment. Do the plots show any trend? If yes, is the trend what you expected? Why or why not? See Steps 2 and 3 in the Python script.
  2. What are the coefficients of correlation between miles per gallon and horsepower? Between miles per gallon and the weight of the car? What are the directions and strengths of these coefficients? Do the coefficients of correlation indicate a strong correlation, weak correlation, or no correlation between these variables? See Step 4 in the Python script.
  3. Write the multiple regression equation for miles per gallon as the response variable. Use weight and horsepower as predictor variables. See Step 5 in the Python script. How might the car rental company use this model?
image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Step 1: Generating cars dataset This block of Python code will generate the sample data for you. You will not be generating the data set using numpy module this week. Instead, the data set will be imported from a CSV file. To make the data unique to you, a random sample of size 30, without replacement, will be drawn from the data in the CSV file. The data set will be saved in a Python dataframe that will be used in later calculations. Click the block of code below and hit the Run button above. In [1]: import pandas as pd from IPython. display import display, HTML # read data from mtcars. cav data set. cars_df_orig = pd. read_cav("https://53-us-west-2. amazonaws. com/data-analytics. zybooks. com/mtcars. cav") # randomly pick 30 observations from the data set to make the data set unique to you. cars_df = cars_df_orig. sample(n=30, replace=False) # print only the first five observations in the dataset. print("Cars data frame (showing only the first five observations) \\") display (HTML(cars_df . head() . to_html( ) ) ) Cars data frame ( showing only the first five observations) Unnamed: 0 mpg cyl disp hp drat wt qsec vs am gear carb 26 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 9 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 29 Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 10 Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 A 16 Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 AStep 2: Scatterplot of miles per gallon against weight The block of code below will create a scatterplot of the variables "miles per gallon" (coded as mpg in the data set) and "weight" of the car (coded as wt). Click the block of code below and hit the Run button above. NOTE: If the plot is not created, click the code section and hit the Run button again. n [3]: import matplotlib. pyplot as plt # create scatterplot of variables mpg against wt. plt. plot(cars_df["wt"], cars_df ["mpg"], 'o', color="red' ) # set a title for the plot, x-axis, and y-axis. plt. title('MPG against Weight" ) plt. xlabel( 'Weight (10005 1bs) ' ) pit . ylabel( 'MPG' ) # show the plot. plt . show() MPG against Weight 35 25 MPG 20 15 10 15 20 2.5 3.0 3.5 4.0 4.5 5.0 5.5 Weight (1000s lbs)Step 3: Scatterplot of miles per gallon against horsepower The block of code below will create a scatterplot of the variables "miles per gallon" (coded as mpg in the data set) and "horsepower" of the car (coded as hp). Click the block of code below and hit the Run button above. NOTE: If the plot is not created, click the code section and hit the Run button again. 4]: import matplotlib. pyplot as plt # create scatterplot of variables mpg against hp. plt. plot(cars_df["hp"], cars_df["mpg"], 'o', color='blue' ) # set a title for the plot, x-axis, and y-axis. plt . title('MPG against Horsepower' ) plt. xlabel( 'Horsepower' ) plt. ylabel( 'MPG' ) # show the plot. pit . show ( ) MPG against Horsepower 35 MPG 20 15 10 50 100 150 200 250 300 HorsepowerStep 4: Correlation matrix for miles per gallon, weight and horsepower Now you will calculate the correlation coefficient between the variables "miles per gallon" and "weight". You will also calculate the correlation coefficient between the variables "miles per gallon" and "horsepower". The corr method of a dataframe returns the correlation matrix with the correlation coefficients between all variables in the dataframe. You will specify to only return the matrix for the three variables. Click the block of code below and hit the Run button above. # create correlation matrix for mpg, wt, and hp. # The correlation coefficient between mpg and wt is contained in the cell for mog row and wt column (or wt row and mpg column). # The correlation coefficient between mpg and hp is contained in the cell for mog row and hp column (or hp row and mpg column). mpg_wt_corr = cars_df[ ['mpg' , 'wt', "hp' ]]. corr() print(mpg_wt_corr) mpg wt hp mpg 1.000000 -0. 871615 -0.778620 wt -0. 871615 1. 909090 0. 655289 hp -0.778620 0.655289 1. 000900Step 5: Multiple regression model to predict miles per gallon using weight and horsepower This block of code produces a multiple regression model with "miles per gallon" as the response variable, and "weight" and "horsepower" as predictor variables. The ols method in statsmodels. formula.api submodule returns all statistics for this multiple regression model. Click the block of code below and hit the Run button above. from statsmodels . formula. api import ols # create the multiple regression model with mpg as the response variable; weight and horsepower as predictor variables. model = ols ('mpg ~ withp', data=cars_df) . fit() print (model . summary () ) OLS Regression Results Dep. Variable: mpg R- squared: 0. 835 Model : OLS Adj. R-squared: 0. 823 Method: Least Squares F-statistic: 68.39 Date: Fri, 10 Jun 2022 Prob (F-statistic) : 2.70e-11 Time: 02:14:02 Log-Likelihood: -69. 855 No. Observations : 30 AIC: 145.7 Of Residuals: 27 BIC: 149.9 of Model: 2 Covariance Type: nonrobust coef std err t P>/ t| [0.025 0.975] Intercept 37 .5934 1. 644 22. 860 0. 000 34.219 40.968 wt -3.9334 0.642 -6.123 0.000 -5.252 - 2. 615 hp -0. 0324 0.009 -3.515 0. 002 -0.051 -0. 013 Omnibus : 4.567 Durbin-Watson: 2. 063 Prob (Omnibus ) : 0.102 Jarque-Bera (JB): 3.422 Skew: 0. 821 Prob ( JB) : 0. 181 Kurtosis: 3.201 Cond. No. 590. Warnings: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probability, Random Variables, And Random Processes

Authors: Hwei P Hsu

3rd Edition

0071824774, 9780071824774

More Books

Students also viewed these Mathematics questions

Question

Which VPN protocol should you use?

Answered: 1 week ago

Question

5. Give examples of binary thinking.

Answered: 1 week ago