What is Logistic regression?

Logistic regression is used to predict a class, i.e., a probability. Logistic regression can predict a binary outcome accurately.

Imagine you want to predict whether a loan is denied/accepted based on many attributes. The logistic regression is of the form 0/1. \(y = 0\) if a loan is rejected, \(y = 1\) if accepted.

A logistic regression model differs from linear regression model in two ways.

  • First of all, the logistic regression accepts only dichotomous (binary) input as a dependent variable (i.e., a vector of 0 and 1).
  • Secondly, the outcome is measured by the following probabilistic link function called sigmoid due to its S-shaped.:

\[\sigma(t)=\frac{1}{1+\exp(-t)}\] The output of the function is always between 0 and 1. Check Image below

The sigmoid function returns values from 0 to 1. For the classification task, we need a discrete output of 0 or 1.

To convert a continuous flow into discrete value, we can set a decision bound at 0.5. All values above this threshold are classified as 1.

How to create Generalized Liner Model (GLM)

Let’s use the adult data set to illustrate Logistic regression. The “adult” is a great dataset for the classification task. The objective is to predict whether the annual income in dollar of an individual will exceed 50.000. The dataset contains 46,033 observations and ten features:

  • age: age of the individual. Numeric
  • education: Educational level of the individual. Factor.
  • marital.status: Marital status of the individual. Factor i.e. Never-married, Married-civ-spouse, …
  • gender: Gender of the individual. Factor, i.e. Male or Female
  • income: Target variable. Income above or below 50K. Factor i.e. >50K, <=50K

amongst others

library(dplyr)
data_adult <- read.csv("https://raw.githubusercontent.com/guru99-edu/R-Programming/master/adult.csv", 
                       stringsAsFactors = TRUE)
glimpse(data_adult)
## Rows: 48,842
## Columns: 10
## $ x               <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16…
## $ age             <int> 25, 38, 28, 44, 18, 34, 29, 63, 24, 55, 65, 36, 26, 5…
## $ workclass       <fct> Private, Private, Local-gov, Private, ?, Private, ?, …
## $ education       <fct> 11th, HS-grad, Assoc-acdm, Some-college, Some-college…
## $ educational.num <int> 7, 9, 12, 10, 10, 6, 9, 15, 10, 4, 9, 13, 9, 9, 9, 14…
## $ marital.status  <fct> Never-married, Married-civ-spouse, Married-civ-spouse…
## $ race            <fct> Black, White, White, Black, White, White, Black, Whit…
## $ gender          <fct> Male, Male, Male, Male, Female, Male, Male, Male, Fem…
## $ hours.per.week  <int> 40, 50, 40, 40, 30, 30, 40, 32, 40, 10, 40, 40, 39, 3…
## $ income          <fct> <=50K, <=50K, >50K, >50K, <=50K, <=50K, <=50K, >50K, …

We will proceed as follow:

  • Step 1: Check continuous variables
  • Step 2: Check factor variables
  • Step 3: Feature engineering
  • Step 4: Summary statistic
  • Step 5: Train/test set
  • Step 6: Build the model
  • Step 7: Assess the performance of the model
  • Step 8: Improve the model

Your task is to predict which individual will have a revenue higher than 50K.

Step 1) Check continuous variables

In the first step, you can see the distribution of the continuous variables.

continuous <- select_if(data_adult, is.numeric)
summary(continuous)
##        x              age        educational.num hours.per.week 
##  Min.   :    1   Min.   :17.00   Min.   : 1.00   Min.   : 1.00  
##  1st Qu.:12211   1st Qu.:28.00   1st Qu.: 9.00   1st Qu.:40.00  
##  Median :24422   Median :37.00   Median :10.00   Median :40.00  
##  Mean   :24422   Mean   :38.64   Mean   :10.08   Mean   :40.42  
##  3rd Qu.:36632   3rd Qu.:48.00   3rd Qu.:12.00   3rd Qu.:45.00  
##  Max.   :48842   Max.   :90.00   Max.   :16.00   Max.   :99.00

Code Explanation

  • continuous <- select_if(data_adult, is.numeric): Use the function select_if() from the dplyr library to select only the numerical columns
  • summary(continuous): Print the summary statistic

From the above table, you can see that the data have totally different scales and hours.per.weeks has large outliers (.i.e. look at the last quartile and maximum value).

You can deal with it following two steps:

  1. Plot the distribution of hours.per.week
  2. Standardize the continuous variables
  1. Plot the distribution: Let’s look closer at the distribution of hours.per.week
# Histogram with kernel density curve
library(ggplot2)
ggplot(continuous, aes(x = hours.per.week)) +
  geom_density(alpha = .2, fill = "#FF6666")

The variable has lots of outliers and not well-defined distribution. You can partially tackle this problem by deleting the top 0.01 percent of the hours per week.

Basic syntax of quantile:

quantile(variable, percentile)

arguments:

  • variable: Select the variable in the data frame to compute the percentile
  • percentile: Can be a single value between 0 and 1 or multiple value. If multiple, use this format: `c(A,B,C, …)
  • ‘A’, ‘B’, ‘C and’…’ are all integer from 0 to 1.

We compute the top 2 percent percentile

top_one_percent <- quantile(data_adult$hours.per.week, .99)
top_one_percent
## 99% 
##  80

Code Explanation

  • quantile(data_adult$hours.per.week, .99): Compute the value of the 99 percent of the working time

98 percent of the population works under 80 hours per week.

You can drop the observations above this threshold. You use the filter from the dplyr library.

data_adult_drop <- data_adult %>% filter(hours.per.week < top_one_percent)
dim(data_adult_drop)
## [1] 48314    10
  1. Standardize the continuous variables

You can standardize each column to improve the performance because your data do not have the same scale. You can use the function mutate_if from the dplyr library. The basic syntax is:

mutate_if(df, condition, funs(function))

arguments:

  • ‘df’: Data frame used to compute the function
  • ‘condition’: Statement used. Do not use parenthesis
  • funs(function): Return the function to apply. Do not use parenthesis for the function

You can standardize the numeric columns as follow:

data_adult_rescale <- data_adult_drop %>%
    mutate_if(is.numeric, funs(as.numeric(scale(.))))
head(data_adult_rescale)
x age workclass education educational.num marital.status race gender hours.per.week income
-1.732661 -0.9916131 Private 11th -1.1983003 Never-married Black Male 0.0082160 <=50K
-1.732590 -0.0446406 Private HS-grad -0.4191721 Married-civ-spouse White Male 0.8856429 <=50K
-1.732519 -0.7730810 Local-gov Assoc-acdm 0.7495203 Married-civ-spouse White Male 0.0082160 >50K
-1.732448 0.3924236 Private Some-college -0.0296080 Married-civ-spouse Black Male 0.0082160 >50K
-1.732377 -1.5015213 ? Some-college -0.0296080 Never-married White Female -0.8692109 <=50K
-1.732306 -0.3360167 Private 10th -1.5878644 Never-married White Male -0.8692109 <=50K

Code Explanation

  • mutate_if(is.numeric, funs(scale)): The condition is only numeric column and the function is scale

Step 2) Check factor variables

This step has two objectives:

  • Check the level in each categorical column
  • Define new levels

We will divide this step into three parts:

  • Select the categorical columns
  • Store the bar chart of each column in a list
  • Print the graphs

We can select the factor columns with the code below:

# Select categorical column
factor <- data.frame(select_if(data_adult_rescale, is.factor))
ncol(factor)
## [1] 6

The dataset contains 6 categorical variables.

The second step is more skilled. You want to plot a bar chart for each column in the data frame factor. It is more convenient to automatize the process, especially in situation there are lots of columns.

library(ggplot2)
# Create graph for each column
graph <- lapply(names(factor), function(x) ggplot(factor, aes(get(x))) + geom_bar() +
                  theme(axis.text.x = element_text(angle = 90)))

Code Explanation

  • lapply(): Use the function lapply() to pass a function in all the columns of the dataset. You store the output in a list
  • function(x): The function will be processed for each x. Here x is the columns
  • ggplot(factor, aes(get(x))) + geom_bar()+ theme(axis.text.x = element_text(angle = 90)): Create a bar char chart for each x element. Note, to return x as a column, you need to include it inside the get()

The last step is relatively easy. You want to print the 6 graphs.

# Print the graph
graph
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

## 
## [[6]]

## Step 3) Feature engineering

Recast education

From the graph above, you can see that the variable education has 16 levels. This is substantial, and some levels have a relatively low number of observations. If you want to improve the amount of information you can get from this variable, you can recast it into higher level. Namely, you create larger groups with similar level of education. For instance, low level of education will be converted in dropout. Higher levels of education will be changed to master.

recast_data <- data_adult_rescale %>% select(-x) %>%
    mutate(education = factor(ifelse(education == "Preschool" | education == "10th" | education == "11th" | education == "12th" | education == "1st-4th" | education == "5th-6th" | education == "7th-8th" | education == "9th", "dropout", ifelse(education == "HS-grad", "HighGrad", ifelse(education == "Some-college" | education == "Assoc-acdm" | education == "Assoc-voc", "Community",
    ifelse(education == "Bachelors", "Bachelors",
        ifelse(education == "Masters" | education == "Prof-school", "Master", "PhD")))))))

Code Explanation

  • We use the verb mutate from dplyr library. We change the values of education with the statement ifelse.

In the table below, you create a summary statistic to see, on average, how many years of education (z-value) it takes to reach the Bachelor, Master or PhD.

recast_data %>% group_by(education) %>% 
  dplyr::summarize(average_educ_year = mean(educational.num), count = n()) %>%
    arrange(average_educ_year)
education average_educ_year count
dropout -1.7365624 6340
HighGrad -0.4191721 15608
Community 0.1111341 14396
Bachelors 1.1390844 7968
Master 1.6192475 3427
PhD 2.3077767 575

Recast Marital-status

# Change level marry
recast_data <- recast_data %>%
    mutate(marital.status = factor(ifelse(marital.status == "Never-married" | marital.status == "Married-spouse-absent", "Not_married", ifelse(marital.status == "Married-AF-spouse" | marital.status == "Married-civ-spouse", "Married", ifelse(marital.status == "Separated" | marital.status == "Divorced", "Separated", "Widow")))))

You can check the number of individuals within each group.

table(recast_data$marital.status)
## 
##     Married Not_married   Separated       Widow 
##       22085       16638        8084        1507

Step 4) Summary Statistic

It is time to check some statistics about our target variables. In the graph below, you count the percentage of individuals earning more than 50k given their gender.

# Plot gender income
ggplot(recast_data, aes(x = gender, fill = income)) + geom_bar(position = "fill") + theme_classic()

Next, check if the origin of the individual affects their earning.

# Plot origin income
ggplot(recast_data, aes(x = race, fill = income)) +
    geom_bar(position = "fill") +
    theme_classic() +
    theme(axis.text.x = element_text(angle = 90))

The number of hours work by gender.

# box plot gender working time
ggplot(recast_data, aes(x = gender, y = hours.per.week)) +
    geom_boxplot() +
    stat_summary(fun.y = mean,
        geom = "point",
        size = 3,
        color = "steelblue") +
    theme_classic()

The box plot confirms that the distribution of working time fits different groups. In the box plot, both genders do not have homogeneous observations.

You can check the density of the weekly working time by type of education. The distributions have many distinct picks. It can probably be explained by the type of contract in the US.

# Plot distribution working time by education
ggplot(recast_data, aes(x = hours.per.week)) +
    geom_density(aes(color = education), alpha = 0.5) +
    theme_classic()

Code Explanation

  • ggplot(recast_data, aes( x= hours.per.week)): A density plot only requires one variable
  • geom_density(aes(color = education), alpha =0.5): The geometric object to control the density

To confirm your thoughts, you can perform a one-way ANOVA test:

anova <- aov(hours.per.week~education, recast_data)
summary(anova)
##                Df Sum Sq Mean Sq F value Pr(>F)    
## education       5   1637   327.5   338.9 <2e-16 ***
## Residuals   48308  46676     1.0                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The ANOVA test confirms the difference in average between groups.

Non-linearity

Before you run the model, you can see if the number of hours worked is related to age.

library(ggplot2)
ggplot(recast_data, aes(x = age, y = hours.per.week)) +
    geom_point(aes(color = income),
        size = 0.5) +
    stat_smooth(method = 'lm',
        formula = y~poly(x, 2),
        se = TRUE,
        aes(color = income)) +
    theme_classic()

Code Explanation

  • ggplot(recast_data, aes(x = age, y = hours.per.week)): Set the aesthetic of the graph
  • geom_point(aes(color= income), size =0.5): Construct the dot plot
  • stat_smooth(): Add the trend line with the following arguments:
    • method=‘lm’: Plot the fitted value if the linear regression
    • formula = y~poly(x,2): Fit a polynomial regression
    • se = TRUE: Add the standard error
    • aes(color= income): Break the model by income

In a nutshell, you can test interaction terms in the model to pick up the non-linearity effect between the weekly working time and other features. It is important to detect under which condition the working time differs.

Correlation

The next check is to visualize the correlation between the variables. You convert the factor level type to numeric so that you can plot a heat map containing the coefficient of correlation computed with the Spearman method.

library(GGally)
# Convert data to numeric
corr <- data.frame(lapply(recast_data, as.integer))
# Plot the graph
ggcorr(corr,
    method = c("pairwise", "spearman"),
    nbreaks = 6,
    hjust = 0.8,
    label = TRUE,
    label_size = 3,
    color = "grey50")

Code Explanation

  • data.frame(lapply(recast_data,as.integer)): Convert data to numeric
  • ggcorr() plot the heat map with the following arguments:
    • method: Method to compute the correlation
    • nbreaks = 6: Number of break
    • hjust = 0.8: Control position of the variable name in the plot
    • label = TRUE: Add labels in the center of the windows
    • label_size = 3: Size labels
    • color = “grey50”: Color of the label

Step 5) Train/test set

Any supervised machine learning task require to split the data between a train set and a test set. You can use the “function” you created in the other supervised learning tutorials to create a train/test set.

set.seed(1234)
create_train_test <- function(data, size = 0.8, train = TRUE) {
    n_row = nrow(data)
    total_row = size * n_row
    train_sample <- 1: total_row
    if (train == TRUE) {
        return (data[train_sample, ])
    } else {
        return (data[-train_sample, ])
    }
}
data_train <- create_train_test(recast_data, 0.8, train = TRUE)
data_test <- create_train_test(recast_data, 0.8, train = FALSE)
dim(data_train)
## [1] 38651     9
dim(data_test)
## [1] 9663    9

Step 6) Build the model

To see how the algorithm performs, you use the glm() package. The Generalized Linear Model is a collection of models. The basic syntax is:

glm(formula, data=data, family=linkfunction())

Argument:

  • formula: Equation used to fit the model- data: dataset used
  • Family: - binomial: (link = “logit”)
  • gaussian: (link = “identity”)
  • Gamma: (link = “inverse”)
  • inverse.gaussian: (link = “1/mu^2”)
  • poisson: (link = “log”)
  • quasi: (link = “identity”, variance = “constant”)
  • quasibinomial: (link = “logit”)
  • quasipoisson: (link = “log”)

You are ready to estimate the logistic model to split the income level between a set of features.

formula <- income ~ .
logit <- glm(formula, data = data_train, family = 'binomial')
summary(logit)
## 
## Call:
## glm(formula = formula, family = "binomial", data = data_train)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.6426  -0.5653  -0.2533  -0.0727   3.2517  
## 
## Coefficients:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)               -1.28482    0.22017  -5.836 5.36e-09 ***
## age                        0.42214    0.01877  22.492  < 2e-16 ***
## workclassFederal-gov       1.33598    0.11827  11.296  < 2e-16 ***
## workclassLocal-gov         0.69509    0.10545   6.592 4.35e-11 ***
## workclassNever-worked     -5.52301   72.34065  -0.076  0.93914    
## workclassPrivate           0.79941    0.09261   8.632  < 2e-16 ***
## workclassSelf-emp-inc      1.26046    0.11408  11.049  < 2e-16 ***
## workclassSelf-emp-not-inc  0.24939    0.10280   2.426  0.01527 *  
## workclassState-gov         0.52998    0.11633   4.556 5.22e-06 ***
## workclassWithout-pay       0.23184    0.86794   0.267  0.78938    
## educationCommunity        -0.42581    0.08142  -5.230 1.70e-07 ***
## educationdropout          -1.03575    0.20982  -4.936 7.96e-07 ***
## educationHighGrad         -0.65998    0.11640  -5.670 1.43e-08 ***
## educationMaster            0.34846    0.06672   5.222 1.77e-07 ***
## educationPhD               0.46896    0.15539   3.018  0.00255 ** 
## educational.num            0.57696    0.06968   8.280  < 2e-16 ***
## marital.statusNot_married -2.52272    0.05058 -49.880  < 2e-16 ***
## marital.statusSeparated   -2.17079    0.05369 -40.433  < 2e-16 ***
## marital.statusWidow       -2.26364    0.12177 -18.589  < 2e-16 ***
## raceAsian-Pac-Islander     0.10016    0.20012   0.501  0.61671    
## raceBlack                  0.08880    0.19012   0.467  0.64046    
## raceOther                  0.05968    0.27083   0.220  0.82560    
## raceWhite                  0.36501    0.18136   2.013  0.04415 *  
## genderMale                 0.05616    0.04205   1.335  0.18174    
## hours.per.week             0.43088    0.01749  24.633  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 42290  on 38650  degrees of freedom
## Residual deviance: 27938  on 38626  degrees of freedom
## AIC: 27988
## 
## Number of Fisher Scoring iterations: 10

Code Explanation

  • formula <- income ~ .: Create the model to fit
  • logit <- glm(formula, data = data_train, family = ‘binomial’): Fit a logistic model (family = ‘binomial’) with the data_train data.
  • summary(logit): Print the summary of the model

The summary of our model reveals interesting information. The performance of a logistic regression is evaluated with specific key metrics.

  • AIC (Akaike Information Criteria): This is the equivalent of R2 in logistic regression. It measures the fit when a penalty is applied to the number of parameters. Smaller AIC values indicate the model is closer to the truth.
  • Null deviance: Fits the model only with the intercept. The degree of freedom is \(n-1\). We can interpret it as a - Chi-square value (fitted value different from the actual value hypothesis testing).
  • Residual Deviance: Model with all the variables. It is also interpreted as a Chi-square hypothesis testing.
  • Number of Fisher Scoring iterations: Number of iterations before converging.

The output of the glm() function is stored in a list. The code below shows all the items available in the logit variable we constructed to evaluate the logistic regression.

# The list is very long, print only the first three elements
lapply(logit, class)[1:3]
## $coefficients
## [1] "numeric"
## 
## $residuals
## [1] "numeric"
## 
## $fitted.values
## [1] "numeric"

Each value can be extracted with the $ sign follow by the name of the metrics. For instance, you stored the model as logit. To extract the AIC criteria, you use:

logit$aic
## [1] 27987.98

Step 7) Assess the performance of the model

Confusion Matrix

The confusion matrix is a better choice to evaluate the classification performance compared with the different metrics you saw before. The general idea is to count the number of times True instances are classified are False.

To compute the confusion matrix, you first need to have a set of predictions so that they can be compared to the actual targets.

predict <- predict(logit, data_test, type = 'response')
# confusion matrix
table_mat <- table(data_test$income, predict > 0.5)
table_mat
##        
##         FALSE TRUE
##   <=50K  6796  506
##   >50K   1112 1249

Code Explanation

  • predict(logit,data_test, type = ‘response’): Compute the prediction on the test set. Set type = ‘response’ to compute the response probability.
  • table(data_test$income, predict > 0.5): Compute the confusion matrix. predict > 0.5 means it returns 1 if the predicted probabilities are above 0.5, else 0.

Each row in a confusion matrix represents an actual target, while each column represents a predicted target. The first row of this matrix considers the income lower than 50k (the False class): 6241 were correctly classified as individuals with income lower than 50k (True negative), while the remaining one was wrongly classified as above 50k (False positive). The second row considers the income above 50k, the positive class were 1229 (True positive), while the True negative was 1074.

You can calculate the model accuracy by summing the true positive + true negative over the total observation \[\text{accuracy}=\frac{TP+TN}{TP+TN+FP+FN}\]

accuracy_Test <- sum(diag(table_mat)) / sum(table_mat)
accuracy_Test
## [1] 0.8325572

Code Explanation

  • sum(diag(table_mat)): Sum of the diagonal
  • sum(table_mat): Sum of the matrix.

The model appears to suffer from one problem, it overestimates the number of false negatives. This is called the accuracy test paradox. We stated that the accuracy is the ratio of correct predictions to the total number of cases. We can have relatively high accuracy but a useless model. It happens when there is a dominant class. If you look back at the confusion matrix, you can see most of the cases are classified as true negative. Imagine now, the model classified all the classes as negative (i.e. lower than 50k). You would have an accuracy of 75 percent (6718/6718+2257). Your model performs better but struggles to distinguish the true positive with the true negative.

In such situation, it is preferable to have a more concise metric. We can look at:

  • \(\text{Precision} = TP/(TP+FP)\)
  • \(\text{Recall} = TP/(TP+FN)\)

Precision vs Recall

Precision looks at the accuracy of the positive prediction. Recall is the ratio of positive instances that are correctly detected by the classifier;

You can construct two functions to compute these two metrics

# Construct precision
precision <- function(matrix) {
    # True positive
    tp <- matrix[2, 2]
    # false positive
    fp <- matrix[1, 2]
    return (tp / (tp + fp))
}

Code Explanation

  • mat[1,1]: Return the first cell of the first column of the data frame, i.e. the true positive
  • mat[1,2]; Return the first cell of the second column of the data frame, i.e. the false positive
recall <- function(matrix) {
# true positive
    tp <- matrix[2, 2]# false positive
    fn <- matrix[2, 1]
    return (tp / (tp + fn))
}

Code Explanation

  • mat[1,1]: Return the first cell of the first column of the data frame, i.e. the true positive
  • mat[2,1]; Return the second cell of the first column of the data frame, i.e. the false negative

You can test your functions

prec <- precision(table_mat)
prec
## [1] 0.7116809
rec <- recall(table_mat)
rec
## [1] 0.5290131

When the model says it is an individual above 50k, it is correct in only 54 percent of the case, and can claim individuals above 50k in 72 percent of the case.

You can create the score based on the precision and recall. The is a harmonic mean of these two metrics, meaning it gives more weight to the lower values. \[F_1=2*\frac{precision*recall}{precision+recall}\]

f1 <- 2 * ((prec * rec) / (prec + rec))
f1
## [1] 0.6068999

Precision vs Recall tradeoff

It is impossible to have both a high precision and high recall.

If we increase the precision, the correct individual will be better predicted, but we would miss lots of them (lower recall). In some situation, we prefer higher precision than recall. There is a concave relationship between precision and recall.

  • Imagine, you need to predict if a patient has a disease. You want to be as precise as possible.
  • If you need to detect potential fraudulent people in the street through facial recognition, it would be better to catch many people labeled as fraudulent even though the precision is low. The police will be able to release the non-fraudulent individual.

The ROC curve

The Receiver Operating Characteristic curve is another common tool used with binary classification. It is very similar to the precision/recall curve, but instead of plotting precision versus recall, the ROC curve shows the true positive rate (i.e., recall) against the false positive rate. The false positive rate is the ratio of negative instances that are incorrectly classified as positive. It is equal to one minus the true negative rate. The true negative rate is also called specificity. Hence the ROC curve plots sensitivity (recall) versus 1-specificity

To plot the ROC curve, we need to install a library called RORC.

We can plot the ROC with the prediction() and performance() functions.

library(ROCR)
ROCRpred <- prediction(predict, data_test$income)
ROCRperf <- performance(ROCRpred, 'tpr', 'fpr')
plot(ROCRperf, colorize = TRUE, text.adj = c(-0.2, 1.7))

Code Explanation

  • prediction(predict, data_test$income): The ROCR library needs to create a prediction object to transform the input data
  • performance(ROCRpred, ‘tpr’,‘fpr’): Return the two combinations to produce in the graph. Here, tpr and fpr are constructed. Tot plot precision and recall together, use “prec”, “rec”.

Step 8) Improve the model

You can try to add non-linearity to the model with the interaction between

  • age and hours.per.week
  • gender and hours.per.week.

You need to use the score test to compare both model

formula_2 <- income ~ age:hours.per.week + gender:hours.per.week + .
logit_2 <- glm(formula_2, data = data_train, family = 'binomial')
predict_2 <- predict(logit_2, data_test, type = 'response')
table_mat_2 <- table(data_test$income, predict_2 > 0.5)
precision_2 <- precision(table_mat_2)
recall_2 <- recall(table_mat_2)
f1_2 <- 2 * ((precision_2 * recall_2) / (precision_2 + recall_2))
f1_2
## [1] 0.6069869

The score is slightly higher than the previous one. You can keep working on the data a try to beat the score.

Summary

Ordinary least squared regression can be summarized in the table below:

Library Objective Function Arguments
glm Train a Generalized Linear Model glm() formula, data, family
glm Summarize the model summary() fitted model
base Make prediction predict() fitted model, dataset, type = ‘response’
base Create a confusion matrix table() y, predict()
base Create accuracy score sum(diag(table())/sum(table()
ROCR Create ROC : Step 1 Create prediction prediction() predict(), y
ROCR Create ROC : Step 2 Create performance performance() prediction(), ‘tpr’, ‘fpr’
ROCR Create ROC : Step 3 Plot graph plot() performance()
 

A work by Gianluca Sottile

gianluca.sottile@unipa.it