BAR-Classification Modelling

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

###Classification Modelling

#logistic regression
y<-ifelse(concrete$comp_strength>50,1,0)
concrete<-cbind(concrete,y)
logreg_fit<-glm(y~.,concrete,family = "binomial")
summary(logreg_fit)
predict <- predict(logreg_fit, type = 'response')
pred<-ifelse(predict > 0.5, 1,0)
#confusion matrix
table(y, pred)
#ROCR Curve
library(ROCR)
ROCRpred <- prediction(pred, y)
ROCRperf <- performance(ROCRpred, 'tpr','fpr')
plot(ROCRperf, colorize = TRUE, text.adj = c(-0.2,1.7))

# train test and validation


train<-concrete[1:800,-9]
test<-concrete[801:1030,-9]

logreg_fit_2<-glm(y~.,train,family = "binomial")
summary(logreg_fit_2)
predict <- predict(logreg_fit_2, test[,-9],type = 'response')
pred<-ifelse(predict > 0.5, 1,0)
#confusion matrix
table(test$y, pred)
ROCRpred <- prediction(pred, test$y)
#ROCRperf <- performance(ROCRpred, 'prec','rec','f','auc')

#########Regularized logistic regression

library(glmnet)
x<-as.matrix(concrete[,1:8])
y<-ifelse(concrete$comp_strength>50,1,0)

model_logreg<-glmnet(x, y, family = "binomial", alpha=1,lambda =0.1)

coef(model_logreg)
#x: matrix of predictor variables
#y: the response or outcome variable, which is a binary variable.

#family: the response type. Use �binomial� for a binary outcome variable
#alpha: the elasticnet mixing parameter. Allowed values include:
#�1�: for lasso regression; 0�: for ridge regression
#a value between 0 and 1 (say 0.3) for elastic net regression.
#lamba: a numeric value defining the amount of shrinkage. Should be specify by
analyst.

#In penalized regression, you need to specify a constant lambda to adjust the
amount of the coefficient shrinkage. The best lambda for your data, can be defined
as the lambda that minimize the cross-validation prediction error rate. This can be
determined automatically using the function cv.glmnet().

# Fit the lasso penalized regression model & predict on test data:

# Display regression coefficients


coef(model)
#R^2 model:
library(DescTools)
PseudoR2(lr_model, c(all))

# Make predictions on the test data


x.test <- model(diabetes ~., test.data)
probabilities <- model %>% predict(newx = x.test)
predicted.classes <- ifelse(probabilities > 0.5, "pos", "neg")
# Model accuracy
observed.classes <- test.data$diabetes
mean(predicted.classes == observed.classes)

##OLR
library(MASS)
z<-ifelse(concrete$comp_strength<50,1,ifelse(concrete$comp_strength<70,2,3))
concrete<-cbind(concrete,z)
train<-concrete[1:800,-(9:10)]
test<-concrete[801:1030,-(9:10)]

olr_model<-polr(as.factor(z)~., data = train, Hess=TRUE,


method = "logistic")
summary(olr_model)

#predicting the class directly


predictedClass <- predict(olr_model, test[,-9]) # predict the classes directly
head(predictedClass)

## Confusion matrix

table(test$z, predictedClass)

#predicting probabilities
predictedScores <- predict(olr_model, test[,-9], type="p") # predict the
probabilites
head(predictedScores)

You might also like