4
$\begingroup$

I am doing a logistic regression in R, where I am modeling how potholes and weather correlate to accidents. When I run a logistic regression, I get the message "Algorithm does not converge"

The problem I think I have is that I have 24,000 accidents with only 350 potholes related to these accidents. Is this to small of a sample size?

The other possible issue I thought of, is that when I look at sample logistic regressions, the outcome is either zero or one, but the only outcome I have is the outcome of one, or accident in my case. I do not have any non accident data in my set, could this be what is causing the problem?

I will attach my current code and its output.

require(ggplot2)
require(sandwich)
require(msm)
mydata <- read.csv("C:\\Users\\myname\\downloads\\logreg1.csv")
## view the first few rows of the data
head(mydata)
summary(mydata)
mylogit <- glm(Accident ~  Rain + Snow, data = mydata, family = "binomial")

> head(mydata)
      Date Pothole Rain Snow Accident
1 1/1/2012       0    0    0        1
2 1/1/2012       0    0    0        1
3 1/1/2012       0    0    0        1
4 1/1/2012       0    0    0        1
5 1/1/2012       0    0    0        1
6 1/1/2012       0    0    0        1
> summary(mydata)
         Date          Pothole            Rain              Snow            Accident
 1/8/2014  :   87   Min.   :0.0000   Min.   :0.00000   Min.   : 0.0000   Min.   :1  
 1/30/2013 :   82   1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.: 0.0000   1st Qu.:1  
 3/21/2013 :   77   Median :0.0000   Median :0.00000   Median : 0.0000   Median :1  
 12/21/2012:   76   Mean   :0.0173   Mean   :0.08077   Mean   : 0.1129   Mean   :1  
 3/10/2013 :   66   3rd Qu.:0.0000   3rd Qu.:0.00000   3rd Qu.: 0.0000   3rd Qu.:1  
 12/13/2013:   59   Max.   :8.0000   Max.   :3.32000   Max.   :11.1000   Max.   :1  
 (Other)   :23606                                                                   
> mylogit <- glm(Accident ~  Rain + Snow, data = mydata, family = "binomial")
Warning message:
glm.fit: algorithm did not converge
$\endgroup$

migrated from stackoverflow.com Nov 8 '14 at 17:32

This question came from our site for professional and enthusiast programmers.

  • 5
    $\begingroup$ If all your data has an accident, you cannot use a glm to predict an accident (because everything is always an accident in your data). But the question you are asking isn't really about programming, it is about statistical modeling, and as such it better belongs on Cross Validated instead. $\endgroup$ – MrFlick Nov 8 '14 at 1:01
  • 3
    $\begingroup$ there is a large and somewhat difficult body of ecological literature about such "presence-only" data; typically people handle this by generating "pseudo-absences" in some reasonably sensible way, but it's a pretty big can of worms. $\endgroup$ – Ben Bolker Nov 8 '14 at 1:30
  • $\begingroup$ This looks like a variant of Complete Separation as described in en.wikipedia.org/wiki/…. Because all your examples have outcome one you can predict them accurately by using a very large constant in the regression equation. But the larger the constant the better the fit, because the predicted probabilities get closer and closer to one, and no finite constant gives you the best fit, because you can always increase the goodness of fit by increasing it. $\endgroup$ – mcdowella Nov 8 '14 at 5:53
3
$\begingroup$

You were correct in your third paragraph. You are attempting to fit a logistic regression model where every response is the same (accident).

Think about what you are feeding into the model: You are giving it 24,000 records with varying variable values for "Pothole", "Rain", and "Snow" that ALL result in an accident. Your model has no built-in knowledge that some weather conditions do result in NO accident, yet alone the frequency of accident to non-accident at varying levels of weather conditions. Intuitively, your model knows of no conditions that can result in NO accident.

Since your data points are not linearly separable, you cannot get convergence of the algorithm.

Ideally you would want a random sample of all trips for which you are interested along with associated weather conditions and the result of the trip (accident/no accident). Practically, this random sample is very difficult to acquire so you will likely need a workaround to get pseudo-absences, but this is a different question in and of itself.

$\endgroup$

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.