<jupyter_start><jupyter_text>Temperature<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import linear_model
import statsmodels.formula.api as smf
import seaborn as sns

# Display preferences.
%matplotlib inline
pd.options.display.float_format = '{:.3f}'.format

import warnings
warnings.filterwarnings(action="ignore")

weather_df = pd.read_csv('weatherHistory.csv')
weather_df.head(10)
weather_df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 96453 entries, 0 to 96452
Data columns (total 12 columns):
 #   Column                    Non-Null Count  Dtype  
---  ------                    --------------  -----  
 0   Formatted Date            96453 non-null  object 
 1   Summary                   96453 non-null  object 
 2   Precip Type               95936 non-null  object 
 3   Temperature (C)           96453 non-null  float64
 4   Apparent Temperature (C)  96453 non-null  float64
 5   Humidity                  96453 non-null  float64
 6   Wind Speed (km/h)         96453 non-null  float64
 7   Wind Bearing (degrees)    96453 non-null  float64
 8   Visibility (km)           96453 non-null  float64
 9   Loud Cover                96453 non-null  float64
 10  Pressure (millibars)      96453 non-null  float64
 11  Daily Summary             96453 non-null  object 
dtypes: float64(8), object(4)
memory usage: 8.8+ MB<jupyter_text>1. Assumption one: linearity of the model in the coefficients<jupyter_code>X = weather_df[['Humidity','Pressure (millibars)','Wind Bearing (degrees)','Wind Speed (km/h)']]


Y = weather_df['Temperature (C)']
lrm = linear_model.LinearRegression()
lrm.fit(X,Y)

#predictions =  lrm.predict(weather_df[['Humidity', 'Pressure (millibars)','Wind Bearing (degrees)', 'Wind Speed (km/h)']])
predictions =  lrm.predict(X)

print('\nCoefficients: \n', lrm.coef_)
print('\nIntercept: \n', lrm.intercept_)
plt.figure(figsize= (12,12))

plt.subplot(221)
plt.scatter(weather_df["Pressure (millibars)"], predictions)
plt.xlabel("Pressure")
plt.ylabel("Estimated Temp (C)")
plt.title('Pressure vs Estimated Temp(C)')

plt.subplot(222)
plt.scatter(weather_df["Humidity"], predictions)
plt.xlabel("Humidity")
plt.ylabel("Estimated Temp (C)")
plt.title('Humidity vs Estimated Temp(C)')

plt.subplot(223)
plt.scatter(weather_df["Wind Bearing (degrees)"], predictions)
plt.xlabel("Wind Bearing (degrees)")
plt.ylabel("Estimated Temp (C)")
plt.title('Wind Bearing (degrees) vs Estimated Temp(C)')

plt.subplot(224)
plt.scatter(weather_df["Wind Speed (km/h)"], predictions)
plt.xlabel("Wind Speed (km/h)")
plt.ylabel("Estimated Temp (C)")
plt.title('Wind Speed (km/h) vs Estimated Temp(C)')

plt.show()<jupyter_output><empty_output><jupyter_text>2. Assumption two: the error term should be zero on average<jupyter_code>errors = Y - predictions

print("Mean of the errors in the medical costs model is: {}".format(np.mean(errors)))<jupyter_output>Mean of the errors in the medical costs model is: 4.03891753518824e-15<jupyter_text>3. Assumption three: homoscedasticity<jupyter_code>from scipy.stats import bartlett
from scipy.stats import levene

bart_stats = bartlett(predictions, errors)
lev_stats = levene(predictions, errors)

print("Bartlett test statistic value is {0:3g} and p value is {1:.3g}".format(bart_stats[0], bart_stats[1]))
print("Levene test statistic value is {0:3g} and p value is {1:.3g}".format(lev_stats[0], lev_stats[1]))<jupyter_output>Bartlett test statistic value is 2464.84 and p value is 0
Levene test statistic value is 2321.24 and p value is 0<jupyter_text>The p-values of both tests are lower than 0.05. So, the test results reject the null hypothesis which means our errors are heteroscedastic.  4. Assumption four: low multicollinearity<jupyter_code>corr = weather_df.corr()

sns.heatmap(corr, data=weather_df)
#to see the correlation between the variables.
for index in corr.columns:
  print(corr[index].sort_values(ascending=False))
  print("---------------"*3)<jupyter_output>Temperature (C)             1.000
Apparent Temperature (C)    0.993
Visibility (km)             0.393
Wind Bearing (degrees)      0.030
Wind Speed (km/h)           0.009
Pressure (millibars)       -0.005
Humidity                   -0.632
Loud Cover                    nan
Name: Temperature (C), dtype: float64
---------------------------------------------
Apparent Temperature (C)    1.000
Temperature (C)             0.993
Visibility (km)             0.382
Wind Bearing (degrees)      0.029
Pressure (millibars)       -0.000
Wind Speed (km/h)          -0.057
Humidity                   -0.603
Loud Cover                    nan
Name: Apparent Temperature (C), dtype: float64
---------------------------------------------
Humidity                    1.000
Pressure (millibars)        0.005
Wind Bearing (degrees)      0.001
Wind Speed (km/h)          -0.225
Visibility (km)            -0.369
Apparent Temperature (C)   -0.603
Temperature (C)            -0.632
Loud Cover                    nan
Name: H[...]<jupyter_text>So our variables are in low collinearity.  5. Assumption five: error terms should be uncorrelated with one other<jupyter_code>plt.plot(errors)
plt.show()
from statsmodels.tsa.stattools import acf

acf_data = acf(errors)

plt.plot(acf_data[1:])
plt.show()<jupyter_output><empty_output><jupyter_text>So, the autocorrelation between the errors is respectively high(ranging between 0.65 & 0.98)  6. Assumption six: features shouldn't be correlated with the errors<jupyter_code>rand_nums = np.random.normal(np.mean(errors), np.std(errors), len(errors))

plt.figure(figsize=(12,5))

plt.subplot(1,2,1)
plt.scatter(rand_nums, errors)
plt.xlabel("the normally distributed random variable")
plt.ylabel("errors of the model")
plt.title("QQ plot")

plt.subplot(1,2,2)
plt.hist(errors)
plt.xlabel("errors")
plt.title("Histogram of the errors")

plt.tight_layout()
plt.show()
from scipy.stats import jarque_bera
from scipy.stats import normaltest

jb_stats = jarque_bera(errors)
norm_stats = normaltest(errors)

print("Jarque-Bera test statistics is {0} and p value is {1}".format(jb_stats[0], jb_stats[1]))
print("Normality test statistics is {0} and p value is {1}".format(norm_stats[0], norm_stats[1]))<jupyter_output>Jarque-Bera test statistics is 3793.296735252353 and p value is 0.0
Normality test statistics is 3375.4323076995734 and p value is 0.0<jupyter_text>By the p-values, we can say that our errors are normally distributed.<jupyter_code>import statsmodels.api as sm

# We need to add constant manually 
# in statsmodels' sm
X = sm.add_constant(X)

results = sm.OLS(Y, X).fit()

results.summary()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13069634531508498, 'fraction_numerical': 0.04963152353737404, 'mean_word_length': 3.548563611491108, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '288637', 'license': 'Apache-2.0', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2223, 'n_words': 614, 'path': 'Regression_Assignments/Assumptions_of_Linear_Regression_SerhanOnerAksakal.ipynb', 'repo_name': 'serhanoner/data_science', 'size': '6649'}