1
$\begingroup$

I have achieved 68% accuracy using glm with family = 'binomial' while doing logistic regression in R. I don't have any idea on how to specify the number of iterations through my code. Any suggestions on it? Will application of stochastic gradient descent work for epoch. If so, how do I apply it in my code? I want to increase the accuracy of my model. Following is my code:

rm(list=ls())
library(dplyr)
data1 <- read.csv("~/hj.csv", head`enter preformatted text here`er=T)
train<- data1[1:116,]
VALUE<-as.numeric(rownames(train))
testset<- data1[1:116,]
mylogit <- glm(VALUE ~ POINT1 + POINT2 + POINT3 + POINT4 , data = data1, family ="binomial")
testset$predicted.value = predict(mylogit, newdata = testset, type="response")
for (i in 1: nrow(testset)){
  if(testset$predicted.value[i] <= 0.50)
    testset$outcome[i] <- 0 
  else testset$outcome[i] <- 1
}
print(testset)
tab = table(testset$VALUE, testset$outcome) %>% as.matrix.data.frame()
accuracy = sum(diag(tab))/sum(tab)
print(accuracy)
print(tab)
table(testset$VALUE, testset$outcome)

enter image description here

$\endgroup$
  • 2
    $\begingroup$ Sorry, the question is not clear. Please clarify the question further. I guess by the iterations you mean number of epochs, and by reaching gradient descent you mean convergence. Please make the suitable edits. $\endgroup$ – Himanshu Rai Jan 31 '17 at 9:17
  • $\begingroup$ Voting to reopen, now the question seems OK to me. It might also help if your show your R code, and explain how you are measuring accuracy (i.e. is this training accuracy, or from cv, or a hold-out test set?). I think StudentT's comment is part of the answer. Do you know for certain that a more accurate model is possible? $\endgroup$ – Neil Slater Feb 6 '17 at 10:25
  • $\begingroup$ I have shared my code and attached the screenshot of my csv here. Can you please check it? $\endgroup$ – Swordsman Feb 7 '17 at 7:05
  • $\begingroup$ You should message to Sean Owen. $\endgroup$ – SmallChess Feb 7 '17 at 11:13
3
$\begingroup$

Logistic regression in R uses the iterative re-weighted least squares algorithm. You can specify the maximum iterations and accuracy with:

m <- glm(..., family = "binomial", control = list(maxit = 2, epsilon=1))

Please read the documentation for glm.control here.

You can check the actual number of iterations used in fitting:

m$iter

BUT... The default settings should be enough.

> glm.control()

$epsilon
[1] 1e-08

$maxit
[1] 25

$trace
[1] FALSE

The default maximum number of iterations is 25, and I **doubt** you will get anything by changing it to anything larger. The accuracy is 1e-08, which is already very small.

You shouldn't blindly adjust the iteration number, most likely it won't help. Try this:

> glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial", control = list(trace=TRUE))

Deviance = 458.875865177 Iterations - 1
Deviance = 458.517518881 Iterations - 2
Deviance = 458.517492476 Iterations - 3
Deviance = 458.517492476 Iterations - 4

I got the dataset from http://www.ats.ucla.edu/stat/r/dae/logit.htm (because I don't have your data file).

Do you see I have trace=TRUE? I print off the deviance for each iteration. R stops because the deviance difference between iteration 3 and 4 is too small. My model only requires 4 iterations.

My recommendation:

  • Print your deviance for each iteration and convince yourself you don't need more than 25 iterations
  • Check your model properly. Do you need more independent variables? Do you need to transform your variables to improve prediction accuracy?
  • Ask yourself is 60% accuracy enough? In many fields 60% is a huge success.
  • Does your model require interaction? Note a saturated model will give you prefect accuracy, but it has all the interaction terms. Your model assumes the effects of your independent variables are additive in predicting the log-odd. Is this a fair assumption?
  • Do you want to run automatic stepwise model selection algorithm in R? You might get a better model than manually done it yourself.

Some useful links you might want to consider:

| improve this answer | |
$\endgroup$
  • $\begingroup$ Can you tell me how to find out whether a logistic regression model has reached it's maximum accuracy or not? Actually I'm pretty new to machine learning and pretty confused on what will be the best foot forward. $\endgroup$ – Swordsman Feb 14 '17 at 7:04
  • $\begingroup$ @ArindamMukherjee if you don't see a convergence warning message in R. $\endgroup$ – SmallChess Feb 14 '17 at 7:05
  • $\begingroup$ I have gone through your previous writeup and have tried to gauge what you have been trying to say. But right now, I am really confused on what should be my next step. Can you suggest me any possible measures on how to take this forward. Or maybe read or go through something (article) which will help me in gauging what will be the best approach to take from here on $\endgroup$ – Swordsman Feb 14 '17 at 16:43
  • $\begingroup$ @ArindamMukherjee I believe I've answered the question. You have a new question? Please start a new one. $\endgroup$ – SmallChess Feb 15 '17 at 1:44
  • $\begingroup$ Can you tell me how to apply stepwise regression in this code and how beneficial it would be for my model? $\endgroup$ – Swordsman Feb 15 '17 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.