SNHU Horsepower And Weight of The Car Are Significant Predictors Of MPG Analysis

User Generated

zrpunabvq

Engineering

Southern New Hampshire University

Description

  • 6-3 Jupyter Notebook (Discussion Prep)External Learning ToolYou have viewed this topicThis activity will take you to the Jupyter Notebook containing the Python scripts for your Module One discussion. It is highly recommended that you read through the discussion prompt before completing your work in this notebook. When you are finished completing and running the Python scripts, begin work on your initial discussion post.Note: This task is not graded, but you will be required to attach your completed Jupyter notebook to your discussion post in the next activity.
  • 6-4 Discussion: Creating a Multiple Regression ModelDiscussion TopicTask: Reply to this topic




    Starts Jun 5, 2021 8:59 PM
    Use the link in the Jupyter Notebook activity to access your Python script. Once you have made your calculations, complete this discussion. The script will output answers to the questions given below. You must attach your Python script output as an HTML file and respond to the questions below.In this discussion, you will apply the statistical concepts and techniques covered in this week's reading about multiple regression. Last week’s discussion involved a car rental company that wanted to evaluate the premise that heavier cars are less fuel efficient than lighter cars. The company expected fuel efficiency (miles per gallon) and weight of the car (often measured in thousands of pounds) to be correlated. The company also expects cars with higher horsepower to be less fuel efficient than cars with lower horsepower. They would like you to consider this new variable in your analysis.In this discussion, you will work with a cars data set that includes the three variables used in this discussion:
    • Miles per gallon (coded as mpg in the data set)
    • Weight of the car (coded as wt in the data set)
    • Horsepower (coded as hp in the data set)
    The random sample will be drawn from a CSV file. This data will be unique to you, and therefore your answers will be unique as well. Run Step 1 in the Python script to generate your unique sample data.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?
    In your follow-up posts to other students, review your peers' results and provide some analysis and interpretation:
    1. Review your peer’s multiple regression model (#3 in their initial post). What is the predicted value of miles per gallon for a car that has 2.78 (2,780 lbs) weight and 225 horsepower? Suppose that this car achieves 18 miles per gallon, what is the residual based on this actual value and the value that is predicted using the regression equation?
    2. How do the plots and correlation coefficients of your peers compare with yours?
    3. Would you recommend this regression model to the car rental company? Why or why not?
    Remember to attach your Python output and respond to all questions in your initial and follow-up posts. Be sure to clearly communicate your ideas using appropriate terminology.

Unformatted Attachment Preview

Arlene Pighetti posted Jun 6, 2021 9:59 AM Subscribe Hello Everyone, Hope everyone is well, you can find my python script attached below. 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. The above scatterplots show a negative trend(correlation) by the downward (negative) slope and are very similar. This is what I would expect when a car weighs less it gets better gas mileage on the flip side if it had more horsepower would equal poor gas mileage. It all goes with the concept of energy being used whether a heavy car or one with high horsepower. 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. The coefficients of correlation between mpg and horsepower: R = -0.766, since R < .40 and < .80 it has a moderate negative correlation with a downward slope The coefficients of correlation between mpg and weight of the car: R = -0.869, here R falls between .80 and 1.00 giving it a strong negative correlation with a downward slope 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? R^2 = 0.823 Y= 37.291+(-3.901)MPG+(-0.031)HP The car rental company could use this to set fair rates, they could also tier there vehicles based on MPG and/or HP. Have a good week, Arlene 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. 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/dataanalytics.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 19 Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 9 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 8 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 10 Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 5 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 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. 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 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. 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. 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) mpg wt hp mpg 1.000000 -0.868572 -0.766267 wt -0.868572 1.000000 0.653875 hp -0.766267 0.653875 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 ~ wt+hp', 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.83 Date: Fri, 04 Jun 2021 Prob (F-statistic): 6.97e11 Time: 19:46:15 Log-Likelihood: 70.307 No. Observations: 30 AIC: 146.6 Df Residuals: 27 BIC: 150.8 Df Model: 2 Covariance Type: nonrobust ============================================================================= = coef std err t P>|t| [0.025 0.975] ----------------------------------------------------------------------------Intercept 37.2911 1.651 22.583 0.000 33.903 40.679 wt -3.9028 0.650 -6.002 0.000 -5.237 2.569 hp -0.0310 0.010 -3.239 0.003 -0.051 0.011 ============================================================================= = Omnibus: 4.046 Durbin-Watson: 2.126 Prob(Omnibus): 0.132 Jarque-Bera (JB): 2.972 Skew: 0.767 Prob(JB): 0.226 Kurtosis: 3.162 Cond. No. 565. ============================================================================= = 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. John Latulippe posted Jun 8, 2021 11:37 AM Subscribe 1. Scatterplots for miles per gallon(mpg) against weight(wt) and mpg against horsepower(hp). There is a trend on both graphs. The first graph shows the correlation between mpg and wt. As the wt increases the mpg output decreases. There is a similar trend on the second graph. As the hp increases the mpg decreases. It seems to make sense that the heavier the vehicle and the more hp results in less fuel efficiency. 2. Correlation of coefficient between mpg and hp,R = 1.000 and -0.761 Correlation of coefficient between mpg and wt, R = 1.000 and -0.895 The direction of the coefficient is negative as each variable moves in the opposite direction from the other. Correlation of coefficients indicates a strong correlation. 3. ŷ = 37.674 - 4.305 + 0.028. The car rental company could use this model to help predict the effects of vehicle weight and horsepower on fuel efficiency(mpg) to charge specific fees based on this information. 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. 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/dataanalytics.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 25 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 18 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 13 Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 14 Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 4 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 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. 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 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. 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. 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) mpg wt hp mpg 1.000000 -0.894764 -0.760805 wt -0.894764 1.000000 0.622304 hp -0.760805 0.622304 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 ~ wt+hp', data=cars_df).fit() print(model.summary()) OLS Regression Results ============================================================================= = Dep. Variable: mpg R-squared: 0.869 Model: OLS Adj. R-squared: 0.859 Method: Least Squares F-statistic: 89.17 Date: Mon, 07 Jun 2021 Prob (F-statistic): 1.27e12 Time: 20:15:29 Log-Likelihood: 63.867 No. Observations: 30 AIC: 133.7 Df Residuals: 27 BIC: 137.9 Df Model: 2 Covariance Type: nonrobust ============================================================================= = coef std err t P>|t| [0.025 0.975] ----------------------------------------------------------------------------Intercept 37.6744 1.444 26.096 0.000 34.712 40.637 wt -4.3052 0.558 -7.713 0.000 -5.451 3.160 hp -0.0281 0.008 -3.734 0.001 -0.044 0.013 ============================================================================= = Omnibus: 3.767 Durbin-Watson: 1.003 Prob(Omnibus): 0.152 Jarque-Bera (JB): 2.364 Skew: 0.640 Prob(JB): 0.307 Kurtosis: 3.501 Cond. No. 620. ============================================================================= = 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.
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

View attached explanation and answer. Let me know if you have any questions.

Module 6 Discussion
For the discussion, analysis was carried out to evaluate whether horsepower and weight of the
car are significant predictors of miles per gallon (MPG). The obtained results are as discussed
below.
Scatterplot
The first analysis involved plotting scatterplots of miles per gallon against horsepower and miles
per gallon against the weight of car. The obtained plots are as shown below.

Figure 1: Scatterplot of Miles per Gallon (MPG) against the Weight of the car.

Figure 2: Scatterplot of Miles per Gallon (MPG) against Horsepower
The scatterplots show that there is a negative linear relationship between the weight of a car and
the MPG. There is also a negative linear relationship between horsepower and the MPG. MPG
decreases as horsepower increases, and also MPG decreases as the weight of car increases and
vice versa. The results were as expected because as the weight of a car increases, then the
amount of fuel consumed per mile is more, thereby resulting in a lower MPG. In terms of
horsepower, the results were also as expected. The more power a car needs, then the more fuel it
uses per mile, thereby resulting in a lower MPG.
Correlation coefficients
The correlation coefficient confirms the results observed from the scatterplots. The correlation
coefficient between the weight of a car and the MPG is equal to -0.868. The correlation
coefficient indicates that there is a strong negative linear relationship between the weight of a car

and the MPG. MPG therefore strongly linearly decreases as the weight of a car increases. The
correlation coefficient between horsepower and the MPG is equal to -0.786. The correlation
coefficient also indicates that there is a strong negative linear relationship between the
horsepower of a car and the ...


Anonymous
Goes above and beyond expectations!

Studypool
4.7
Indeed
4.5
Sitejabber
4.4

Related Tags