• 0
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 212

Backtrace:

File: /home/mycocrkc/statisticsanswered.com/application/views/question.php
Line: 212
Function: _error_handler

File: /home/mycocrkc/statisticsanswered.com/application/controllers/Questions.php
Line: 416
Function: view

File: /home/mycocrkc/statisticsanswered.com/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to run Logistic regression in R

I have a large set of data and I would like to learn how to run logistic regression using the dataset. 

First, I would like to subset the data so that I will have training dataset and a test dataset. 

#### LOGISTIC REGRESSION using R

The importance of splitting the dataset into a test data and a Training data is for purposes of testing the accuracy of the prediction model. 

bankloan <- read.csv("bankloan.csv")
bankloan <- bankloan[complete.cases(bankloan), ]

# train- and test sets creation
set.seed(12345); Use set.seed () so that you can obain similar results everytime the dataset is selected. 
train.prop <- 0.8  # 80% of the data is set as the test data
train.cases <- sample(nrow(bankloan), 
                      nrow(bankloan) * train.prop)

bankloan.train <- bankloan[train.cases, ]
bankloan.test <- bankloan[-train.cases, ]

# LR model fit
glm.fit <- glm(default ~ employ + address + debtinc
               + creddebt, data = bankloan.train, family = "binomial")

# posterior probabilities    # The posterior proababilities are assigned to a variable so that the table is constructed. 
glm.prob <- predict(glm.fit, bankloan.test, type = "response")

# a class variable
glm.class <- glm.prob
glm.class[glm.class > 0.5] <- "Yes"
glm.class[glm.class <= 0.5] <- "No"

  ####  Test the accuracy of the Model. 
# confusion matrix
table(glm.class, bankloan.test$default) 

# prediction accuracy
mean(glm.class == bankloan.test$default)

# adjusted rand index
adjustedRandIndex(glm.class, bankloan.test$default)

  • 2
Reply Report