Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In your initial post, address the following items: Check to be sure your scatterplots of miles per gallon against horsepower and weight of the car

In your initial post, address the following items:

  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?

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 theRunbutton above.

In[1]:

import pandas as pd

from IPython.display import display, HTML

?

# read data from mtcars.csv data set.

cars_df_orig = pd.read_csv("https://s3-us-west-2.amazonaws.com/data-analytics.zybooks.com/mtcars.csv")

?

# 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

24

Pontiac Firebird

19.2

8

400.0

175

3.08

3.845

17.05

0

0

3

2

7

Merc 240D

24.4

4

146.7

62

3.69

3.190

20.00

1

0

4

2

27

Lotus Europa

30.4

4

95.1

113

3.77

1.513

16.90

1

1

5

2

9

Merc 280

19.2

6

167.6

123

3.92

3.440

18.30

1

0

4

4

25

Fiat X1-9

27.3

4

79.0

66

4.08

1.935

18.90

1

1

4

1

Step 2: Scatterplot of miles per gallon against weight

The block of code below will develop 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 theRunbutton above.

NOTE: If the plot is not created, click the code section and hit theRunbutton again.

In[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 (1000s lbs)')

plt.ylabel('MPG')

?

# show the plot.

plt.show()

Step 3: Scatterplot of miles per gallon against horsepower

The block of code below will develop 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 theRunbutton above.

NOTE: If the plot is not created, click the code section and hit theRunbutton again.

In[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.

plt.show()

Step 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". Thecorrmethod 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 theRunbutton above.

In[5]:

# create correlation matrix for mpg, wt, and hp.

# The correlation coefficient between mpg and wt is contained in the cell for mpg row and wt column (or wt row and mpg column).

# The correlation coefficient between mpg and hp is contained in the cell for mpg row and hp column (or hp row and mpg column).

mpg_wt_corr = cars_df[['mpg','wt','hp']].corr()

print(mpg_wt_corr)

mpgwthp

mpg1.000000 -0.875322 -0.778370

wt-0.8753221.0000000.692993

hp-0.7783700.6929931.000000

Step 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. Theolsmethod in statsmodels.formula.api submodule returns all statistics for this multiple regression model.

Click the block of code below and hit theRunbutton above.

In[6]:

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 ~ wt+hp', data=cars_df).fit()

print(model.summary())

OLS Regression Results

==============================================================================

Dep. Variable:mpgR-squared:0.823

Model:OLSAdj. R-squared:0.810

Method:Least SquaresF-statistic:62.75

Date:Tue, 08 Jun 2021Prob (F-statistic):7.06e-11

Time:17:48:34Log-Likelihood:-70.363

No. Observations:30AIC:146.7

Df Residuals:27BIC:150.9

Df Model:2

Covariance Type:nonrobust

==============================================================================

coefstd errtP>|t|[0.0250.975]

------------------------------------------------------------------------------

Intercept37.21511.64722.5950.00033.83640.595

wt-3.92200.682-5.7540.000-5.321-2.523

hp-0.03010.010-2.9420.007-0.051-0.009

==============================================================================

Omnibus:4.248Durbin-Watson:2.211

Prob(Omnibus):0.120Jarque-Bera (JB):3.137

Skew:0.787Prob(JB):0.208

Kurtosis:3.183Cond. No.554.

==============================================================================

Warnings:

[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

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. csv data set. cars_df_orig = pd. read_csv( "https: //s3-us-west-2 . amazonaws . com/data-analytics . zybooks.com/mtcars. csv") # 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 24 Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 7 Merc 240D 24.4 146.7 62 3.69 3.190 20.00 1 0 4 2 27 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 9 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 A 25 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4Step 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. In [3]: import matplotlib. pyplot as plt # create scatterplot of variables mpg against wt. pit. plot (cars_df[ "wt"], cars_df[ "mpg" ], 'o', color='red' ) # set a title for the plot, x-axis, and y-axis. pit . title( 'MPG against Weight' ) pit. xlabel( 'Weight (1000s lbs) ' ) pit . ylabel ( 'MPG' ) # show the plot. pit . show( ) MPG against Weight 35 30 25 MPG . 20 15 10 15 20 25 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. In [4]: import matplotlib. pyplot as plt # create scatterplot of variables mpg against hp. pit. 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' ) pit. xlabel ( 'Horsepower' ) plt. ylabel( 'MPG' ) # show the plot. pit . show ( ) MPG against Horsepower 35 30 25 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. In [5]: # create correlation matrix for mpg, Wt, and hp. # The correlation coefficient between mpg and wt is contained in the cell for mpg row and wt column (or wt row and mpg # The correlation coefficient between mpg and hp is contained in the cell for mpg row and hp column (or hp row and mpg mpg_wt_corr = cars_df[[ 'mpg' , 'wt' , 'hp' ]].corr() print (mpg_wt_corr ) mpg wt hp mpg 1.000000 -0.875322 -0.778370 wt 0.875322 1.000000 0.692993 hp 0.778370 0.692993 1.000000 Step 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. In [6]: 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 ~ wtthp', data=cars_df) .fit( ) print (model . summary ( ) ) OLS Regression Results Dep. Variable: mpg R-squared: 0. 823 Model : OLS Adj . R-squared: 0 . 810 Method: Least Squares F-statistic: 62 .75 Date : Tue, 08 Jun 2021 Prob (F-statistic) : 7. 06e-11 Time : 17 : 48 :34 Log-Likelihood: -70.363 No. Observations : 30 AIC : 146.7 Df Residuals: 27 BIC : 150 .9 Df Model: 2 Covariance Type: nonrobust coef std err t P> | t [0 . 025 0 . 975] Intercept 37 . 2151 1 . 647 22.595 0. 000 33 . 836 40 . 595 wt -3.9220 0 . 682 -5. 754 0 . 000 -5 . 321 -2. 523 hp -0 . 0301 0 . 010 -2. 942 0. 007 -0 . 051 -0 . 009 Omnibus : 4. 248 Durbin-Watson : 2. 211 Prob ( Omnibus ) : 0 . 120 Jarque-Bera (JB) : 3. 137 Skew: 0. 787 Prob ( JB ) : 0 . 208 Kurtosis : 3. 183 Cond. No. 554

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

Intermediate Algebra A Text/Workbook

Authors: Charles P McKeague

2nd Edition

1483214176, 9781483214177

More Books

Students also viewed these Mathematics questions