2024-03-16

## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ ggplot2::%+%()   masks psych::%+%()
## ✖ ggplot2::alpha() masks psych::alpha()
## ✖ dplyr::filter()  masks stats::filter()
## ✖ dplyr::lag()     masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Thank you for using fastDummies!
## 
## To acknowledge our work, please cite the package:
## 
## Kaplan, J. & Schlegel, B. (2023). fastDummies: Fast Creation of Dummy (Binary) Columns and Rows from Categorical Variables. Version 1.7.1. URL: https://github.com/jacobkap/fastDummies, https://jacobkap.github.io/fastDummies/.
## 
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo 
## 
## 
## 载入程辑包:'gridExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
## 
## 
## 
## 载入程辑包:'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows

2.1 Introduction

Introduction

2.2 Core Idea in Data Mining

Classification

classification

  • Classification is perhaps the most basic form of data analysis.
  • The recipient of an offer can respond or not respond.
  • An applicant for a loan can repay on time, repay late, or declare bankruptcy.
  • A credit card transaction can be normal or fraudulent. A packet of data traveling on a network can be benign or threatening.
  • A bus in a fleet can be available for service or unavailable. The victim of an illness can be recovered, still be ill, or be deceased.

Prediction

  • trying to predict the value of a numerical variable
  • the prediction of the value of a continuous variable
  • prediction may be used for both continuous and categorical data

Association Rules and Recommendation Systems

  • Large databases of customer transactions lend themselves naturally to the analysis of associations among items purchased
  • “what goes with what.”

Predictive Analytics

  • Classification, prediction, and to some extent, association rules and collaborative filtering constitute the analytical methods employed in predictive analytics

Data Reduction and Dimension Reduction

  • This process of consolidating a large number of records (or cases) into a smaller set is termed data reduction.

Data Exploration and Visualization

  • Exploration is aimed at understanding the global landscape of the data, and detecting unusual values

Supervised and Unsupervised Learning

Supervised Learning - Goal: Predict a single “target” or “outcome” variable - Training data, where target value is known - Score to data where value is not known - Methods: Classication and Prediction

Unsupervised Learning - Goal: Segment data into meaningful segments; detect patterns - There is no target (outcome) variable to predict or classify - Methods: Association rules, collaborative fi lters, data reduction & exploration, visualization

2.3 The Steps in Data MIning

Detailed steps

  • 1.Develop an understanding of the purpose of the data mining project.
  • 2.Obtain the data set to be used in the analysis.
  • 3.Explore, clean, and preprocess the data.
  • 4.Reduce the data dimension, if necessary.
  • 5.Determine the data mining task.
  • 6.Partition the data (for supervised tasks).
  • 7.Choose the data mining techniques to be used.
  • 8.Use algorithms to perform the task.
  • 9.Interpret the results of the algorithms.
  • 10.Deploy the model.

2.4 Preliminary Steps

Content

  • Basic Steps
  • Sampling from a Database
  • Dummy Variables
  • Missing Value

Some basic steps you need to do before doing any analysis

-   Dimension of data


-   viewing all the data


-   displaying only selected rows and columns


-   computing summary statistics for variables of interest
  • Take the WEST ROXBURY HOME VALUES DATASET as example

  • Find the dimension of the dataset

Dimension of housing.df: 5802 rows and 14 columns

  • Show the first six rows
TOTAL.VALUE TAX LOT.SQFT YR.BUILT GROSS.AREA LIVING.AREA FLOORS ROOMS BEDROOMS FULL.BATH HALF.BATH KITCHEN FIREPLACE REMODEL
344.2 4330 9965 1880 2436 1352 2 6 3 1 1 1 0 None
412.6 5190 6590 1945 3108 1976 2 10 4 2 1 1 0 Recent
330.1 4152 7500 1890 2294 1371 2 8 4 1 1 1 0 None
498.6 6272 13773 1957 5032 2608 1 9 5 1 1 1 1 None
331.5 4170 5000 1910 2370 1438 2 7 3 2 0 1 0 None
337.4 4244 5142 1950 2124 1060 1 6 3 1 0 1 1 Old

  • Show the last row
TOTAL.VALUE TAX LOT.SQFT YR.BUILT GROSS.AREA LIVING.AREA FLOORS ROOMS BEDROOMS FULL.BATH HALF.BATH KITCHEN FIREPLACE REMODEL
447.6 5630 7406 1950 2510 1600 2 7 3 1 1 1 1 None
  • Find Summary statistic for each column
TOTAL.VALUE TAX LOT.SQFT YR.BUILT GROSS.AREA LIVING.AREA FLOORS ROOMS BEDROOMS FULL.BATH HALF.BATH KITCHEN FIREPLACE REMODEL
Min. : 105.0 Min. : 1320 Min. : 997 Min. : 0 Min. : 821 Min. : 504 Min. :1.000 Min. : 3.000 Min. :1.00 Min. :1.000 Min. :0.0000 Min. :1.000 Min. :0.0000 Length:5802
1st Qu.: 325.1 1st Qu.: 4090 1st Qu.: 4772 1st Qu.:1920 1st Qu.:2347 1st Qu.:1308 1st Qu.:1.000 1st Qu.: 6.000 1st Qu.:3.00 1st Qu.:1.000 1st Qu.:0.0000 1st Qu.:1.000 1st Qu.:0.0000 Class :character
Median : 375.9 Median : 4728 Median : 5683 Median :1935 Median :2700 Median :1548 Median :2.000 Median : 7.000 Median :3.00 Median :1.000 Median :1.0000 Median :1.000 Median :1.0000 Mode :character
Mean : 392.7 Mean : 4939 Mean : 6278 Mean :1937 Mean :2925 Mean :1657 Mean :1.684 Mean : 6.995 Mean :3.23 Mean :1.297 Mean :0.6139 Mean :1.015 Mean :0.7399 NA
3rd Qu.: 438.8 3rd Qu.: 5520 3rd Qu.: 7022 3rd Qu.:1955 3rd Qu.:3239 3rd Qu.:1874 3rd Qu.:2.000 3rd Qu.: 8.000 3rd Qu.:4.00 3rd Qu.:2.000 3rd Qu.:1.0000 3rd Qu.:1.000 3rd Qu.:1.0000 NA
Max. :1217.8 Max. :15319 Max. :46411 Max. :2011 Max. :8154 Max. :5289 Max. :3.000 Max. :14.000 Max. :9.00 Max. :5.000 Max. :3.0000 Max. :2.000 Max. :4.0000 NA

Sampling from a Database

  • Typically, we perform data mining on less than the complete database

    • Data Mining algorithms will have varying limitations on what they can handle on the term of the numbers and variables

    • Accurate models can often built with as few as several thousand records.

Dummy Variable

  • Dummy variables are a coding method used to represent categorical data. They transform categorical variables into binary variables, where each variable represents a level (category) of the original categorical variable.

  • Nominal categorical variables

  • In many cases, only three of the dummy variables need to be used; if the values of three are known, the fourth is also known.

.data .data_None .data_Old .data_Recent
None 1 0 0
Recent 0 0 1
None 1 0 0
None 1 0 0
None 1 0 0
Old 0 1 0

Missing Value

  • Typically, some records will contain missing values. If the number of records with missing values is small, those records might be omitted. However, if we have a large number of variables, even a small proportion of missing values can affect a large number of variables

  • An alternative to omitting records with missing values is to replace the missing value with an imputed value, based on the other values for that variable across all records.

NA not removed

Min X1st.Qu. Median Mean X3rd.Qu. Max
1 3 3 3.230093 4 9

NA has been removed

Min. X1st.Qu. Median Mean X3rd.Qu. Max.
1 3 3 3.2298 4 9

2.5 Predictive Power And Overfitting

Content

  • Overfitting

  • Creation and Use of Data Partitions

    • Training Partition

    • Validation Partition

    • Test Partition

Overfitting

  • The more variable we include in a model, the greater the risk of overfitting the particular data used for modeling.

  • Somewhat surprisingly, even if we know for a fact that a higher-degree curve is the appropriate model, if the model-fitting dataset is not large enough, a lower-degree function (that is not as likely to fit the noise) is likely to perform better in terms of predicting new values.

  • Overfitting can also result from the application of many different models, from which the best performing model is selected.

Overfitting Example

We could connect up these points with a smooth but complicated function, one that interpolates all these data points perfectly and leaves no error(residuals).This can be seen if Figure2.3.However, we can see that such a curve is unlikely to be accurate, or even useful, in predicting future sales on the basis of advertising expenditures. For instance, it is hard to believe that increasing expenditures from 400$ to 500$ will actually decrease revenue.

Creation and Use of Data Partitions

  • When we use the same data both to develop the model and to assess its performance, we introduce an “optimism” basis. This is because when we choose the model that works best with the data, this model’s superior performance comes from two sources:

    • A superior Model

    • Chance aspects of the data that happen to match the chosen model better than they match their model

  • To address the overfitting problem, we simply divide(partition) our data and develop our model using only one of the partitions. After we have a model, we try it out on another partition and see how if performs, which we can measure in several ways.

Three Data Partitions And Their Role in the Data Mining Process

Training, Validation, and Test Partitions

Partition in R and Visualization

Explanation to the R code

This is done by first drawing a random sample of records into the training set, then assigning the remaining records as validation. In the case of three partitions, the validation records are chosen randomly from the data after excluding the records already sampled into the training set.

Additional - Cross-Validation

  • When the number of records in our sample is small, data partitioning might not be advisable as each partition will contain too few records for model building and performance evaluation.

  • An alternative to data partitioning is cross-validation, which is especially useful with small samples.

  • Cross-Validation is a procedure that starts with partitioning the data into “folds,” or non-overlapping subsamples.

2.6 Building a Predictive Model

Modeling Process

  • fitting a regression model to training data (WestRoxbury)
.data .data_None .data_Old .data_Recent
None 1 0 0
Recent 0 0 1
None 1 0 0
None 1 0 0
None 1 0 0
Old 0 1 0

Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0312399 0.0180678 1.729043e+00 0.0838548
TAX 0.0794910 0.0000006 1.435480e+05 0.0000000
LOT.SQFT 0.0000002 0.0000001 1.450697e+00 0.1469185
YR.BUILT 0.0000015 0.0000090 1.685623e-01 0.8661469
GROSS.AREA 0.0000019 0.0000009 2.129272e+00 0.0332740
LIVING.AREA -0.0000031 0.0000016 -1.873092e+00 0.0611061
FLOORS 0.0012595 0.0009309 1.352959e+00 0.1761217
ROOMS -0.0004508 0.0003450 -1.306437e+00 0.1914560
BEDROOMS 0.0001158 0.0005259 2.201710e-01 0.8257458
FULL.BATH 0.0007620 0.0007194 1.059270e+00 0.2895212
HALF.BATH 0.0009229 0.0006608 1.396748e+00 0.1625431
KITCHEN 0.0040841 0.0025528 1.599883e+00 0.1096793
FIREPLACE 0.0002128 0.0005750 3.700500e-01 0.7113588
REMODELOld -0.0010327 0.0010123 -1.020187e+00 0.3076825
REMODELRecent 0.0000075 0.0008903 8.372700e-03 0.9933199

Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0324967 0.0199043 1.632646e+00 0.1026345
TAX 0.0794909 0.0000007 1.108372e+05 0.0000000
LOT.SQFT 0.0000002 0.0000002 1.137663e+00 0.2553399
YR.BUILT 0.0000025 0.0000099 2.558616e-01 0.7980729
GROSS.AREA 0.0000014 0.0000011 1.235962e+00 0.2165567
LIVING.AREA -0.0000025 0.0000021 -1.211655e+00 0.2257270
FLOORS 0.0007447 0.0012016 6.197572e-01 0.5354584
ROOMS -0.0002303 0.0004482 -5.138221e-01 0.6074092
BEDROOMS 0.0006060 0.0006881 8.807284e-01 0.3785260
FULL.BATH 0.0006059 0.0009263 6.541155e-01 0.5130809
HALF.BATH 0.0009383 0.0008627 1.087636e+00 0.2768318
KITCHEN -0.0002889 0.0031966 -9.039190e-02 0.9279810
FIREPLACE 0.0007482 0.0007503 9.971858e-01 0.3187441
REMODELOld -0.0002075 0.0013333 -1.556068e-01 0.8763520
REMODELRecent -0.0001873 0.0011586 -1.616844e-01 0.8715638

Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0358488 0.0445681 0.8043599 0.4212722
TAX 0.0794912 0.0000009 90628.6247127 0.0000000
LOT.SQFT 0.0000002 0.0000002 0.8868655 0.3752439
YR.BUILT -0.0000040 0.0000224 -0.1789326 0.8580064
GROSS.AREA 0.0000026 0.0000015 1.7489138 0.0804391
LIVING.AREA -0.0000040 0.0000026 -1.4995629 0.1338647
FLOORS 0.0019357 0.0015048 1.2863796 0.1984399
ROOMS -0.0007824 0.0005436 -1.4391187 0.1502528
BEDROOMS -0.0005172 0.0008193 -0.6312339 0.5279502
FULL.BATH 0.0009748 0.0011708 0.8325853 0.4051650
HALF.BATH 0.0009918 0.0010490 0.9455056 0.3445000
KITCHEN 0.0117885 0.0042588 2.7680392 0.0056846
FIREPLACE -0.0004929 0.0009016 -0.5466574 0.5846671
REMODELOld -0.0022984 0.0015626 -1.4708741 0.1414618
REMODELRecent 0.0002076 0.0014022 0.1480445 0.8823206

Applying the regression model to predict validation set (WestRoxbury)

x
4 498.6114
7 359.4194
16 298.2115
20 347.9726
22 330.8013
23 357.8325

validation.data.TOTAL.VALUE pred residuals
4 498.6 498.6114 -0.0113551
7 359.4 359.4194 -0.0194121
16 298.2 298.2115 -0.0114630
20 348.0 347.9726 0.0273781
22 330.8 330.8013 -0.0012929
23 357.8 357.8325 -0.0325160
mean
-0.0005411241

computing model evaluation metrics

ME RMSE MAE MPE MAPE
Test set 16.15384 138.9491 106.3514 -1.319907 27.01707
ME RMSE MAE MPE MAPE
Test set -0.0005411 0.0225935 0.0194162 -0.0001376 0.0052403

2.7 Using R for Data Mining on a Local Machine

  • The data mining process require a huge number of records.

  • The use of a sample of 20,000 is likely to yield as accurate

  • The number of records required in each partition (training, validation, and test) can be accommodated within the memory limit allowed in R (memory.limit())

2.8 Automating Data Mining Solutions

Automating Data Mining Solutions

  • once the computational environment is set and functioning, the data miner’s work is not done.

  • The environment in which a model operates is typically dynamic

  • Predictive models often have a short shelf life—one leading consultant fi nds they rarely continue to function effectively for more than a year.

  • So, even in a fully deployed state, models must be periodically checked and re-evaluated.

  • Once performance fl ags, it is time to return to prototype mode and see if a new model can be developed.