week4

## ── 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.0317519 0.0180552 1.758600e+00 0.0786983
TAX 0.0794910 0.0000006 1.436426e+05 0.0000000
LOT.SQFT 0.0000002 0.0000001 1.536780e+00 0.1244020
YR.BUILT 0.0000012 0.0000090 1.363010e-01 0.8915881
GROSS.AREA 0.0000019 0.0000009 2.106041e+00 0.0352438
LIVING.AREA -0.0000031 0.0000016 -1.925758e+00 0.0541836
FLOORS 0.0012587 0.0009310 1.351872e+00 0.1764693
ROOMS -0.0004422 0.0003451 -1.281427e+00 0.2000951
BEDROOMS 0.0001419 0.0005259 2.697768e-01 0.7873416
FULL.BATH 0.0006892 0.0007191 9.584116e-01 0.3378954
HALF.BATH 0.0009125 0.0006603 1.381936e+00 0.1670448
KITCHEN 0.0041170 0.0025523 1.613045e+00 0.1067894
FIREPLACE 0.0002080 0.0005751 3.616622e-01 0.7176177
REMODELOld -0.0009779 0.0010112 -9.670658e-01 0.3335516
REMODELRecent 0.0000035 0.0008902 3.970700e-03 0.9968320

Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0402314 0.0201059 2.000973e+00 0.0454732
TAX 0.0794912 0.0000007 1.105569e+05 0.0000000
LOT.SQFT 0.0000002 0.0000002 1.281253e+00 0.2001906
YR.BUILT -0.0000040 0.0000100 -3.996836e-01 0.6894142
GROSS.AREA 0.0000018 0.0000012 1.548079e+00 0.1216948
LIVING.AREA -0.0000035 0.0000021 -1.643748e+00 0.1003190
FLOORS 0.0013704 0.0012153 1.127612e+00 0.2595621
ROOMS -0.0004872 0.0004465 -1.091232e+00 0.2752469
BEDROOMS 0.0001935 0.0006844 2.827980e-01 0.7773486
FULL.BATH 0.0020569 0.0009525 2.159404e+00 0.0308873
HALF.BATH 0.0010831 0.0008590 1.261005e+00 0.2073920
KITCHEN 0.0048583 0.0032042 1.516237e+00 0.1295508
FIREPLACE -0.0002265 0.0007467 -3.033878e-01 0.7616126
REMODELOld -0.0023384 0.0013015 -1.796763e+00 0.0724604
REMODELRecent -0.0005905 0.0011680 -5.055182e-01 0.6132271

Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.0204095 0.0432862 -0.4715012 0.6373276
TAX 0.0794908 0.0000009 91630.5708184 0.0000000
LOT.SQFT 0.0000002 0.0000002 0.8636661 0.3878614
YR.BUILT 0.0000292 0.0000217 1.3432975 0.1793082
GROSS.AREA 0.0000022 0.0000014 1.5868058 0.1126941
LIVING.AREA -0.0000031 0.0000026 -1.1982818 0.2309308
FLOORS 0.0015100 0.0014738 1.0245946 0.3056623
ROOMS -0.0003423 0.0005471 -0.6256034 0.5316372
BEDROOMS 0.0000933 0.0008227 0.1133563 0.9097580
FULL.BATH -0.0013767 0.0011274 -1.2211112 0.2221691
HALF.BATH 0.0005247 0.0010529 0.4983540 0.6182822
KITCHEN 0.0027012 0.0042651 0.6333269 0.5265832
FIREPLACE 0.0006500 0.0009113 0.7133434 0.4757057
REMODELOld 0.0012515 0.0016104 0.7771237 0.4371657
REMODELRecent 0.0009451 0.0013810 0.6843430 0.4938275

Applying the regression model to predict validation set (WestRoxbury)

x
3 330.0887
5 331.5201
7 359.4202
8 320.3899
9 333.5059
10 409.4202

validation.data.TOTAL.VALUE pred residuals
3 330.1 330.0887 0.0112615
5 331.5 331.5201 -0.0200945
7 359.4 359.4202 -0.0202076
8 320.4 320.3899 0.0100808
9 333.5 333.5059 -0.0059150
10 409.4 409.4202 -0.0201944
mean
-0.001374095

computing model evaluation metrics

ME RMSE MAE MPE MAPE
Test set 17.01647 140.15 105.7833 -1.126518 26.59953
ME RMSE MAE MPE MAPE
Test set -0.0013741 0.0223389 0.019249 -0.0003439 0.0051972

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.