Friday, July 3, 2020

Basics of R Session 22- Logistic Regression 1.1 (Caret Package)

library(caret)
library(ISLR)
data1<-read.csv("file:///C:/Users/LENOVO/Desktop/Missing/Chapter 10 Bollywood Box Office Collection Data.csv")

# data partition
data1$Success.Faliure<-as.factor(data1$Success.Faliure)
set.seed(100)
split1<-createDataPartition(data1$Success.Faliure,p=0.7, list = FALSE)

data1train <- data1[split1, ]
data1test <- data1[-split1, ]

fitControl <- trainControl(
  method = "cv",
  number = 10,
  savePredictions = TRUE
)

lreg3<-train(Success.Faliure~
               Youtube_Dislikes+Youtube_Likes+Youtube_Views,
             method= "glm",
             family= "binomial",
             trControl=fitControl,
             data = data1)

summary(lreg3)

# predicted values and residual

lreg3$finalModel$coefficients
lreg3$finalModel$fitted.values
lreg3$finalModel$residuals
lreg3$results

coef(lreg1$finalModel)
confint(lreg1$finalModel)

# model fit indices
library(blorr)
library(Rcpp)
library(tidymodels)

blr_model_fit_stats(lreg1$finalModel)
blr_test_hosmer_lemeshow(lreg1$finalModel)
blr_bivariate_analysis(lreg1$finalModel)

# walds test, for each IV variables
library(survey)
regTermTest(lreg1$finalModel, "ï..Age")


# confusionmatrix

blr_confusion_matrix(lreg3$finalModel, cutoff = 0.5)

predict1<-predict(lreg3,data1)
confusionMatrix(predict1, data1$Success.Faliure)

# for roc 
gainstable<-blr_gains_table(lreg3$finalModel)
blr_roc_curve(gainstable)

library(ROCR)
predict1<-predict(lreg3,data1, type = "prob")

predict_roc<-prediction(predict1[2], data1$Success.Faliure)
predict_roc2<-performance(predict_roc, measure = "tpr", "fpr")
plot(predict_roc2)

predict_roc2<-performance(predict_roc, measure = "auc")
predict_roc2@y.values[[1]]


# forward method, backward method, both
library(blorr)

regfor<-blr_step_aic_forward(lreg1$finalModel)
regfor
regfor1<-blr_step_aic_forward(lreg1$finalModel, details = TRUE)

regback<-blr_step_aic_backward(lreg1$finalModel)
regback

regboth<-blr_step_aic_both(reg1$finalModel)
regboth


#_will take lot of time not advisable  #
library(caret)
reg1<-train(Status~., 
            method ="glmStepAIC", 
            family= "binomial", 
            direction ="forward"
            data = datatrain)
reg1$finalmodel

#-----------------------------------------------------------------------------------------#


No comments:

Post a Comment