Partial preview of the text
Download WGU NBM2 Task 1 Multiple Regression | Predictive Modeling Study Guide & Analysis and more Study notes Business Taxation and Tax Management in PDF only on Docsity!
WGU NBM2 TASK 1: MULTIPLE REGRESSION Predictive Modeling & Data Analysis Study Guide * Regression Analysis * Task Review Key Topics: Multiple Linear Regression « Python * Data Preparation Lasso Regression * OLS « Predictors * R? MAE « MSE ¢ RMSE ¢ Residual Analysis | Western Governors University Multiple Regression Page 1| 28 For Predictive Modeling Part I: RESEARCH QUESTION A 1. Utilize the cable company data set to determine which services predict tenure of customers? 2. The goal of this analysis is to identify critical services that are used by customers that keep them coming back. Identifying these services would provide a basis for marketing emphasis to current customers as well as marketing for new customers. Part Il: METHOD JUSTIFICATION B. t. Multiple regression analysis is dependent on a few assumptions regarding the dataset. The most obvious assumption is that the relationship between the target and predictor variables is linear. Asecond assumption is that the residuals between the observed and predicted values should be normally distributed. A third assumption is that there is no multicollinearity. The independent variables cannot be highly correlated, if so, the highly correlated variables should be removed from the analysis. The last assumption is homoscedasticity. Homoscedasticity is focused on the variance of the independent variables. That variance needs to be finite or very similar over all the variables. (Assumptions of Multiple Linear Regression, n.d.) 2. Using Python as the primary programming tool for analysis provides significant opportunities for utilization, sharing, and frameworks. Many development environments can utilize Python from the most robust to an online text editor. Jupyter notebooks are an invaluable tool in which python programming can be used and shared readily. Google Colabs (colabs.research.google.com) is another environment that is accessible from anywhere. Almost ener Page 2|28 Another data prep issue is determining if the numeric field is continuous or discrete or if the character fields are categorical or free text. The understanding of the structure of the variable helps in exploring extremes and rectifying any issues found. Categorical fields will need enumeration for calculation and analytic purposes. Identifying all the categorical fields and the correctness of each category value will be critical. Deletion of records with missing information may occur if there are plenty of other records in the dataset. Duplicate records absolutely need deletion. There were no duplicate records found. Checking for extremes in any of the column types is an excellent way to evaluate continuity. Many extreme entries are errors and need to be addressed. These extreme values could significantly impact the data analysis, particularly true if the dataset is small. Handling these extreme values is typically a logic check. If the data clearly makes no sense or is impossible, then a decision on how to handle the issues come to the forefront. Do you delete the record? Do you average out the value if it isa number? Do you make it zero or the mode value? There are lots of options. With categorical variables, it is requisite to check for different spellings. The various spellings and misspellings could dilute the potential value and impact a certain category may have in the analysis. For analysis purposes, text-based classification variables will need to be enumerated. Encoding the categorical fields with pandas.get_dummies (pandas.get_dummies, n.d.) on the following fields, PaymentMethod, Contract, Marital, InternetService, Port_modem, Phone, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport, StreamingTV, and StreamingMovies. 2. A quick look through the table finds that numerous fields will not be part of the general analysis. These include Lat, Lng, UID, Interaction, Customer_id, CaseOrder, TimeZone, Job, Zip, County, State, City, and Area. These variables show no variation or pattern in distribution graphs and identify all customers to be in North America. A quick thought on the locations and their relevance is visible in the zip field. The zip encompasses all the state, city, area, county, lat and Ing, and TimeZone. The zip field distribution in the below chart shows even distribution of customers from the OOxxx's to the eee eee eee e eee Page 4| 28 999xx's. This distribution indicates that no one area is suffering from churn anymore than any other. The slight dip in the OOxxx's and the 88xxxs ranges reflect the lack of population in the northern New England area and the Rocky Mountain states inclusively. Zip 1000 800 600 400 200 o 50000 100000 Chart 1 — Zip Code Distribution There are 10,000 records for every field. There are no records that have blanks, NaNs, or Null values. Looking for extreme values are easily seen in the following table showing field name, min, max, average, and quartiles. Age Income Tenure | MonthlyCharge | GB/Year | Contact | EquipFail | Item s s Mea 53.0 | 39806.92 | 34.53 172.62 3392.34 | 0.99 0.39 35 n Z std 20.6 | 28199.91 | 26.44 | 42.94 2185.29 | 0.98 0.63 1.0 So min 18 348.67 1.00 79.97 155.5. 0.00 0.00 1.00 25% 35, 19224.71 | 7.92 139.97 1236.47 | 0.00 0.00 3.00 50% 53. 33170.61 | 35.43 167.48 3279.53 | 1.00 0.00 4.00 75% val 53246.17_ | 61.48 200.73 5586.14 | 2.00 1.00 4.00 max 89 258900.7 | 71.99 290.16 7158.98 | 7.00 6.00 7.00 0 Table 1: df.describe() results In the table, the Age range goes from 18 to 89, which seems very reasonable. 75% of the users are under the age of 71. Income ranges from $348 to $258,900. The low end seems odd, but it could be college-age customers who may not earn a great deal and are subsidized by parents. Page 5|28 ftem1 tem2 tem3 3000 3000 3000 2000 2000 2000 1000 1000 1000 0 o 0 2 4 6 2 4 6 2 4 6 8 nema tems een? tems 000 1000 x00 ney | won 100 | es 00 | 10 won| ; o = ot Chart 3 — Survey Question Distributions All the categorical variables have a consistent and reasonable selection of values. Marital status has 'Widowed', 'Married', 'Separated', ‘Never Married’, ‘Divorced’. PaymentMethods variable has ‘Electronic Check’, ‘Mailed Check’, ‘Bank Transfer(automatic)', 'Credit Card(automatic)' ared standard values. Gender has 'Male', ‘Female’, 'Nonbinary'. InternetService has ‘Fiber Optic’, "DSL', and 'None'. Contracts have three values of 'Month-to-month’', 'Two Year’, 'One Year’. 3. After loading the data into a pandas dataframe in Python, the steps used to prepare the data for analysis include the following steps: + Aquick review of the data to evaluate what the data looks like using the following code: a df.info — which displays the top 5 raws and the bottom five rows of the dataset loaded into the dataframe df. * Aquick statistical overview of the dataframe is produced using: o Df.describe() = This command provides a count, mean, standard deviation, minimum, 1* quartile, 2°? quartile, 3 quartile, and max values for all the numerical variables in the dataframe, as noted in Table 1 above. = Areview of these results should expose any extreme values should they exist. + Review all the column names. o Df.columns will present the list of column names in the dataframe. + Review all the variable data types. Page 7|28 ° Df.dtypes will return a list of all the variable and the data types associated with each, such as int64, object, float64. A quick review here will identify the numeric fields from the text fields. The object fields will be considered categorical and addressed by encading methods if appropriate. If you have 1,000 different categories, it would not be appropriate. + Review general contents of all variables. ° Using df.info() will display a count of each variable, the variable name, the number of Non-Null values, and the data type of the field. Info() is a quick and easy way to find missing values and locate them in the dataframe. Review for missing information o Using df.isna().any() will review each variable and check for NaNs, Nulls, NaTs for each variable. It returns a list of variables with a True if there are missing values or False if there are no missing values. Review all the object variables and values eo Df[‘Marital’].unique() will return all the unique values in the variable. o Reviewing those unique values will spot any misspellings or extreme values for the variables. Used .unique() on all categorical variables such as Marital, PaymentMethod, InternetService, Contract, Gender, TechSupport, DeviceProtection, OnlineBackup, OnlineSecurity, Multiple, Port_modem, Tablet, Phone, PaperlessBilling, StreamingTV, StreamingMovies, and Techie. o There were no extreme or irrational values in these variables. Number of Customers using Internet Service Type on am ntemetervice se00 zoo “aaa ¥ & # ¢ < Chart 6 — Internet Service Distribution Bivariate visualizations: 2°. BS e 8 Frequency of Churn 28 8 $ | 2 2 8 a a a Tenure (Months) Chart 7 — Frequency of Churn by Tenure e 5 6 8 8 —— Chum = Yes Frequency to Churn 3 — Chum =No anos ano2 0.000 x 100 150 2 20 0 Monthly Charge ($$$) Chart 8 — Churn Frequency by Monthly Charge xO 250 § 200 g i 150 100 a Fiber Optic DSL None internetService Chart 9 — Monthly Charge by Internet Service 5. A copy of the cleaned dataset is in the file named prepared_multipleregressian_churn_data.csv. Part IV: MODEL COMPARISON AND ANALYSIS D. MULTIPLE LINEAR REGRESSION WITH ALL PREDICTORS array ([-3 Coefficients = -75869018e-01, 3.99287852e-02, 2.70848624e-04, -05492876: 4, -1863527/3e-03, 3.895417/8Be- 2.641981850-01, -31750648c-01, 3.81297086e-01, -30502688e+00, 3.22921782e-08, 3.50823492e-02, 1. 02, 1.175 1 45874e-03, -3. 05, -1.92281248 j -1.8/39/418e-03, -1.82101604e-04, -2.243/1619e-03, -48981697c-04, 2.38060651c-03, -94074694e-01, 2.6081007 01, 745692e-03, 9.60570957e-C 5.75 -2.73954392e-03, 784044e-031) # Further tests for accuracy and believability —2..93290725e-04, a, -4.98454394e-04, =3,559515770-01, -7.30056412e-01, 225221¢c+00, 3.25366180e-03, 3367372e-03, 5681104e-03, -5.99460019c-01, -4 =i 4 6390161e-03, -26025017e-04, -1.1380/054e-03, 4.60276859c+00, 2.44763434e-03, print("Mean Squared Error (MSE): %.2f"% np.mean((regmodel.predict(X_test)- y_test) **2)) Mean Squared Error (MSE) = 0.01 print("Variance score: %.2f" % regmodel.score(X_test,y_test)) Variance score: 1.00 There were 43 features evaluated during the multiple linear regression. Not all those predictors were helpful. Determining which features to focus on and which to ignore will require more information. Utilizing a correlation heatmap is useful, but it is difficult to read due to the number of features. Please refer to the chart below. Correl: ft Heatmap Chart 11 - Correlation Heat Map The target of the regression analysis is the ‘MonthlyCharge’ variable. As expected, there are noted correlations to ‘MonthlyCharge’ with ‘Multiple’, ‘OnlineSecurity’, ‘OnlineBackup’, ‘DeviceProtection’, ‘TechSupport’, ‘StreamingTV’, ‘StreamingMovies’, ‘Internet_Fiber Optic’, and ‘Churn’. All of these features are actually services that require an additional fee for the subscriber, except for Churn. The relationship between ‘MonthlyCharge’ and Churn is of a different nature. Another python statistic module called statsmodel has a linear regression method called OLS that has a robust statistical analysis included. To verify the results using the scikitLearn LinearRegression model, | installed statsmodels.api and ran statsmodels OLS function on the same dataset. The summary results are noted below. Email -0.1956 0.018 -10.990 -0.231 0.161 0.00 Contacts -0.1809 0.055 -3.345 1 0.288 -0.074 . 4 0.01 Yearly_equip_failure -0.2062 0.085 —--2.420 A -0.373 -0.039 , 0.57 Techie -0.0820 0.147 -0.559 6 -0.370 0.206 0.00 Port_modem -0.3401 0.108 +3.145 2 -0.552 -0.128 0.01 Tablet -0.2984 0.118 2.529 1 -0.530 -0.067 0.00 Phone -1.8061 0.184 -9.822 0 -2.167 -1.446 Multiple 18.4431 0.135 aa ae 18.178 18.708 4 & 0.00 OnlineSecurity -12.0524 0.141 -85.599 0 -12.328 -11.776 2 0.00 OnlineBackup 4.3525 0.148 29.379 0 4.062 4.643 a a5 0.00 DeviceProtection -3.9128 0.143 -27.339 0 4.193 -3.632 TechSupport 11.4062 0.112 _ aa 11.186 11.626 es 0.00 StreamingTV 1.6223 0.260 -6.245 0 2.131 1.113 StreamingMovies 11.8156 0.243 48.609 0.00 11.339 12.292 PaperlessBilling Tenure Bandwidth_GB_Year Item1 Item2 Item3 Item4 Item5 Item6é Item7 Items Marital_Married Marital_Never Married -0.2980 -15.5542 0.1898 -0.0407 0.0118 -0.1752 -0.7546 -1.1077 -0.3944 -0.3803 -0.3246 -0.6620 -0.7377 0.110 0.083 0.001 0.078 0.072 0.066 0.058 0.059 0.063 0.060 0.057 0.171 0.170 2.711 186.47 187.42 -0.521 0.162 2.637 -12.976 -18.701 -6.229 -6.368 -5.672 -3.875 4.341 -0.514 -15.718 0.188 0.194 -0.130 -0.305 -0.869 1.224 -0.519 -0.497 -0.437 -0.997 -1.071 -0.083 -15.391 0.192 0.112 0.154 -0.045 -0.641 -0.992 -0.270 -0.263 -0.212 -0.327 -0.405 1.99e- Skew: 0.037 Prob(JB): 64 Kurtosis: 2.034 Cond.No. °48e* 05 Warnings: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. [2] The condition number is large, 6.48e+05. This might indicate that there are strong multicollinearity or other numerical problems. Chart 12 — Statsmodel OLS Summary Data Interpretation of this chart focuses on the fourth column of data or the p-value. Any feature with a p-value smaller than 0.05 is a viable candidate for selection as a feature for further investigation. Upon review, Techie, Item1, Item2, Contract_One year, and Contract_Two year do not fall below the 0.05 threshold. Nothing in this analysis lines up with the correlation heatmap above. This issue could be caused by several things one being that there is no y-intercept. The y-intercept for MonthlyCharge would be zero and there are not bills at zero. The lack of a y- intercept at times confuses the linear regression models. The high 99.999% prediction rate reflects that confusion as well. Using all the predictors is generally not a good idea for a linear regression model. 3. The Reduced Predictor Multiple Linear Regression analysis produced more realistic numbers. However, finding the right predictor variables to focus on was two fold. One is use the ones that correlate with the ‘MonthlyCharge’ target or use another technique to identify the most impactful variables. There is another regression technique calle the Lasso Regression. The Lasso Regression adjusts out variables that variances can be minimized and those variables with large or inconsequential variances are turned to zero and have no impact. (Zach, 2020) Running the Lasso regression with the following code; from sklearn.linear_model import Lasso lassoreg = Lasso(alpha=0.01, normalize = True) lassoreg.fit(X_train, y_train) # PRINT OUT THE RESULTING COEFFICIENTS print(lassoreg.coef_) fo a. QO. Oo. 66109624 -0. 0's Os: Cc. —D; -C. “0. 30.64819113 ©.83895828 20.69201036 10 .8748363 10.64463426 40.13439908 50.00559052 0. QO. QO. 0. oO. -0. Q. oO. -0. -0. oO. Q. -0. a) =. 18./27/33'152 11 Ag 9 -0. -0. Oo. 0. 0. ] # TEST THE LASSO REGRESSION y_prea = Lassoreg.predict (X_test) # Print out the test scores using; from sklearn import metrics print("R2 (R-Square Value)",r2_score(y_test,y_pred)) print("\n") print ("MAE (mean_absolute_error) :",metrics.mean_absolute_error(y_test, y_pred)) print("\n") print ("MSE (mean_squared_error) :",metrics.mean_squared_error(y_test, y_pred)) print("\n") print ("RMSE (root_mean_squared_error) : "| np.sqrt(metrics.mean_squared_error(y_test, y_pred))) # RESULTS: RZ (R-Square Value) 0.9554539407908741 MAE (mean_absolute_error) : 8.73259057926937 MSE (mcean_squared_crror) : 83.62730606806327 RMSE (root_mean_squared_error) : 9.1486231/8821131 These numbers are not great, but seem to be more reasonable than with the ‘all preditor’ linear regression model. Also, diving into the variables that the Lasso Regression identified as impactful include the following; Churn, Multiple, Online Security, OnlineBackup, DeviceProtection, TechSupport, StreamingTV, StreamingMovies, Internet_FiberOptic, and Internet_None. All these fields tightly coincide with the correlation heatmap. E. Data Analysis 1. The multiple linear regression analysis is targeting the MonthlyCharge value. The analysis will identify the required variables or predictor features that are necessary to predict