[ANSWER] MAT 243 Module Six Discussion: Multiple Regression

$ 51.00

MAT 243 Module Six Discussion: Multiple Regression

This notebook contains the step-by-step directions for your Module Six discussion. It is very important to run through the steps in order. Some steps depend on the outputs of earlier steps. Once you have completed the steps in this notebook, be sure to answer the questions about this activity in the discussion for this module.

Order Now
Order This Paper

MAT 243 Module Six Discussion: Multiple Regression

This notebook contains the step-by-step directions for your Module Six discussion. It is very important to run through the steps in order. Some steps depend on the outputs of earlier steps. Once you have completed the steps in this notebook, be sure to answer the questions about this activity in the discussion for this module.

Reminder: If you have not already reviewed the discussion prompt, please do so before beginning this activity. That will give you an idea of the questions you will need to answer with the outputs of this script.

Initial post (due Thursday)

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.

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)\n")
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
27 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
29 Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
30 Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
11 Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.4 0 0 3 3
8 Merc 230 22.8 4 140.8 95 3.92 3.150 22.9 1 0 4 2

Step 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.

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

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”. 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 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)
          mpg        wt        hp
mpg  1.000000 -0.869623 -0.775445
wt  -0.869623  1.000000  0.653494
hp  -0.775445  0.653494  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.

from statsmodels.formula.api
Note: Full answer to this question is available after purchase.
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:                    mpg   R-squared:                       0.831
Model:                            OLS   Adj. R-squared:                  0.819
Method:                 Least Squares   F-statistic:                     66.45
Date:                Wed, 09 Feb 2022   Prob (F-statistic):           3.73e-11
Time:                        02:15:44   Log-Likelihood:                -70.139
No. Observations:                  30   AIC:                             146.3
Df Residuals:                      27   BIC:                             150.5
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     37.5637      1.669     22.508      0.000      34.139      40.988
wt            -3.9402      0.650     -6.062      0.000      -5.274      -2.607
hp            -0.0321      0.009     -3.461      0.002      -0.051      -0.013
==============================================================================
Omnibus:                        4.259   Durbin-Watson:                   1.622
Prob(Omnibus):                  0.119   Jarque-Bera (JB):                3.170
Skew:                           0.792   Prob(JB):                        0.205
Kurtosis:                       3.166   Cond. No.                         593.
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified
End of initial post

Attach the HTML output to your initial post in the Module Six discussion. The HTML output can be downloaded by clickingĀ File, thenĀ Download as, thenĀ HTML. Be sure to answer all questions about this activity in the Module Six discussion.

Follow-up posts (due Sunday)

Return to the Module Six discussion to answer the follow-up questions in your response posts to other students. There are no Python scripts to run for your follow-up posts.

Related; MAT 243 Module Four Discussion: Hypothesis Testing for the Difference in Two Population Proportions.

Order This Paper

Reviews

There are no reviews yet.

Be the first to review “[ANSWER] MAT 243 Module Six Discussion: Multiple Regression”

Your email address will not be published. Required fields are marked *

error: Content is protected !!