466_Models

Association Models (Frequent Pattern Mining)

  • Purpose: To find interesting relationships between items in large datasets (e.g., market basket analysis).

  • Type: Unsupervised Model

Key Metrics:

  1. Support:

    • Definition: Proportion of transactions containing a particular item or itemset.


    • Formula:[ Support(X) = \frac{\text{Number of transactions containing item X}}{\text{Total number of transactions}} ]

    • Purpose: Indicates how frequent an itemset is; higher support suggests more frequent patterns.

  2. Confidence:

    • Definition: Probability that a consequent occurs given the antecedent.


    • Formula:[ Confidence(A \rightarrow B) = \frac{Support(A \cup B)}{Support(A)} ]

    • Purpose: Measures rule strength. High confidence (e.g., "milk → cereal") indicates that if milk is purchased, cereal is likely also purchased.

  3. Lift:

    • Definition: Measures how much more likely two items are to be bought together compared to if they were bought independently.


    • Formula:[ Lift(A \rightarrow B) = \frac{Support(A \cup B)}{Support(A) \cdot Support(B)} ]

    • Purpose: Strength of association; lift > 1 indicates items are more often bought together than expected by chance.

Functions & Methods in Association:

  • apriori(): Used for discovering frequent itemsets and generating association rules.

  • itemFrequency(): Computes the frequency of individual items.

  • inspect(): Inspects generated association rules.

  • plot(): Visualizes results, like item frequencies.

Example: Used to find relationships (e.g., "customers who buy bread also buy butter").

Key Functions for Association:


  • Library Loading:library(arules)


  • Loading Dataset:data("Groceries")


Implementation of Apriori Algorithm


  • Code Example:rules <- apriori(Groceries, parameter = list(support = 0.01, confidence = 0.5))


  • Inspect Rules:inspect(rules)

  • Item Frequency Calculation: itemFrequency(Groceries)


  • Visualization:itemFrequencyPlot() creates bar plots of item frequencies.


Clustering Models (K-means Clustering)

  • Purpose: To group similar data points into clusters based on their characteristics.

  • Type: Unsupervised Model

Key Concepts:

  • Clusters: Groups of similar data points.

  • Centroids: Center points of clusters, representing means of the data points within the cluster.

  • K: Number of clusters specified.

  • Elbow Method: Helps determine the optimal number of clusters.

Functions & Methods:

  • kmeans(): Performs k-means clustering on the dataset.

  • plot(): Visualizes the data and cluster assignments.

  • centers: Accesses cluster centroids from the k-means object.

Steps:

  1. Data Preprocessing:

    • Ensure dataset contains only numeric variables; remove non-numeric columns.

    • Example: Fruit1 = Fruit[, c(-1, -6)]

  2. Elbow Method:

    • Determine optimal clusters (K) by examining Within-Cluster Sum of Squares (WCSS).

    • Code Example:

    wcss <- vector()
    for (k in 1:10) {
        wcss[k] <- kmeans(Fruit1, k)$tot.withinss
    }
    plot(1:10, wcss, type="b", main="Application of Elbow Method", xlab="Number of clusters", ylab="WCSS")

Regression Models (Linear Regression)

  • Purpose: To predict a continuous outcome based on one or more predictor variables.

  • Type: Supervised Model

Key Concepts:

  • Predictors: Variables used to predict the target variable.

  • Response Variable: Outcome variable predicted.

  • Mean Squared Error (MSE): Measures closeness of model’s predictions to actual values (lower is better).

Functions & Methods:

  • lm(): Fits a linear regression model.

  • predict(): Generates predictions from trained model.

  • summary(): Summarizes model, including coefficients, R-squared, and p-values.

  • plot(): Visualizes data and residuals.

  • mse(): Calculates mean squared error.

  • abline(): Adds the regression line to the plot.

  • sample.split: From caTools, splits data into training/testing sets.

Steps:

  1. Prepare data and convert categorical columns to factors.

    • Example: Data1State <- factor(...)

  2. Build model using significant columns.


    • Code Example:L1 <- lm(Profit ~ ., data = Data1)

  3. Split data for training/testing setup.

  4. Test model using predicted values.

  5. Create comparison table for actual vs. predicted.


Classification Models (Decision Trees)

  • Purpose: To predict discrete categories or classes based on input features.

  • Type: Supervised Model

Key Concepts:

  • Target Variable: Categorical variable predicted (e.g., spam vs. not spam).

  • Features: Independent variables used for predictions.

  • Classes: Different categories the target variable can belong (e.g., "spam", "not spam").

Functions & Methods:

  • rpart(): Builds decision tree model.

  • predict(): Makes predictions based on the trained model.

  • confusionMatrix(): Evaluates model performance.

  • plot(): Visualizes decision boundaries/tree structure.

Steps:

  1. Prepare data by retaining output column for classification and removing irrelevant features.

    • Convert categorical columns to factors.

    • Example: Fruit2Type= factor(Fruit2$Type)

  2. Identify feature importance using attrEval().

  3. Model building using ctree().

  4. Predictions using trained model.

  5. Confusion matrix creation to evaluate accuracy.

  6. Visualization of the decision tree classifier using plot().


Summary of Models

  • Supervised Learning (Requires labeled data):

    • Classification: e.g., ctree() for decision trees (predicting categories).

    • Regression: e.g., lm() for linear regression (predicting continuous values).

  • Unsupervised Learning (Does not require labeled data):

    • Clustering: e.g., kmeans() for clustering (grouping similar data points).

    • Association: e.g., apriori() for frequent itemset mining (finding relationships between items).