Lab 6

.pdf

School

Western University *

*We aren’t endorsed by this school

Course

1000A

Subject

Statistics

Date

Jan 9, 2024

Type

pdf

Pages

8

Uploaded by SargentStingrayPerson1024

Report
Lab 6 October 3, 2022 1 Lab 6 In this lab we discuss statistical modelling and least squares regression. 1.1 Simple Linear Regression [1]: import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from scipy import stats [2]: # Read .csv data possum = pd . read_csv( "possum.csv" ) # The possum dataset consists of morphometric measurements on 46 possums. possum . head() [2]: sex age headL skullW totalL tailL 0 m 8 94.1 60.4 89.0 36.0 1 f 6 92.5 57.6 91.5 36.5 2 f 6 94.0 60.0 95.5 39.0 3 f 6 93.2 57.1 92.0 38.0 4 f 2 91.5 56.3 85.5 36.0 stats.linregress: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html [3]: # Building a simple model with only one term # Simple linear regression is a linear regression model with a single , explanatory variable. model = stats . linregress(x = possum[ ' age ' ], y = possum[ ' headL ' ]) [4]: # Finding the slope of the regression line model . slope [4]: 0.5631158455392805 [5]: # Finding the intercept of the regression line model . intercept [5]: 90.08288948069242 1
[6]: # Finding the correlation coefficient (R) model . rvalue [6]: 0.4011016610119052 [7]: # Calculating the R-squared (model . rvalue ** 2 ) [7]: 0.16088254246650932 [8]: # Plotting the fitted line X = pd . DataFrame({ "age" :np . linspace( 0 , 9 , 45 )}) y_pred = model . intercept + model . slope * X plt . scatter(possum[ ' age ' ], possum[ ' headL ' ], label = ' original data ' ) plt . plot(X, y_pred, color = ' red ' , label = ' fitted line ' ) plt . xlabel( "age" ) plt . ylabel( "headL" ) plt . legend() plt . show() sns.lmplot: https://seaborn.pydata.org/generated/seaborn.lmplot.html [9]: # Alternative way to plot the fitted line sns . lmplot(x = "age" , y = "headL" , data = possum, ci = None ) plt . xlim( 0 , 10 ) plt . show() 2
[10]: # Plotting two fitted lines, one for female and another for male possums sns . lmplot(x = "age" , y = "headL" , data = possum, hue = "sex" , ci = None ); plt . xlim( 0 , 10 ) plt . show() 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help