Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am running a logistic regression in R and I keep receiving errors, which I believe are related to how I am fitting the model,

I am running a logistic regression in R and I keep receiving errors, which I believe are related to how I am fitting the model, but I'm not really sure.

This is the code I have been using:

library(plyr)

library(randomForest)

library(rpart)

library(rpart.plot)

library(caret)

library(ggplot2)

library(gridExtra)

dat <- read.csv("C:/Users/Gabe Cedeno/Desktop/Personal/Masters/Projects/C744/WA_Fn-UseC_-Telco-Customer-Churn (1) - Copy.csv")

head(dat)

Data Preprocessing

sapply(dat, function(x) sum(is.na(x)))

dat[is.na(dat$TotalCharges),]

sum(is.na(dat$TotalCharges))/nrow(dat)

datc <- dat[complete.cases(dat), ]

datc$SeniorCitizen <- as.factor(mapvalues(datc$SeniorCitizen,

from=c("0","1"),

to=c("No", "Yes")))

datc$MultipleLines <- as.factor(mapvalues(datc$MultipleLines,

from=c("No phone service"),

to=c("No")))

for(i in 10:15){

datc[,i] <- as.factor(mapvalues(datc[,i],

from= c("No internet service"), to= c("No")))

}

datc$customerID <- NULL

set.seed(56)

split_train_test <- createDataPartition(datc$Churn,p=0.7,list=FALSE)

dtrain<- datc[split_train_test,]

dtest<- datc[-split_train_test,]

lr_fit <- glm(Churn ~., data = dtrain,

family=binomial(link='logit'))

summary(lr_fit)

lr_prob1 <- predict(lr_fit, dtest, type="response")

lr_pred1 <- ifelse(lr_prob1 > 0.5,"Yes","No")

table(Predicted = lr_pred1, Actual = dtest$Churn)

lr_prob2 <- predict(lr_fit, dtrain, type="response")

lr_pred2 <- ifelse(lr_prob2 > 0.5,"Yes","No")

lr_tab1 <- table(Predicted = lr_pred2, Actual = dtrain$Churn)

lr_tab2 <- table(Predicted = lr_pred1, Actual = dtest$Churn)

lr_acc <- sum(diag(lr_tab2))/sum(lr_tab2)

lr_acc

Is there an error in my code I am missing?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Business Analytics

Authors: Jeffrey D. Camm, James J. Cochran, Michael J. Fry, Jeffrey W. Ohlmann, David R. Anderson

2nd edition

1305627733, 978-1305861817, 1305861817, 978-0357688960, 978-1305627734

More Books

Students also viewed these Mathematics questions