1
$\begingroup$

I am trying to implement logistic regression in my app. I know this question is software based but the concept is mathematical.

I have a table. The X axis represents minutes in a day and starts from 1 all the way to 1440. 1 represents 12am, 3 represents 12:03am etc. The y axis represents occupied / vacant, 0 and 1 respectively:

1   1
2   1
3   1
4   1
5   1
6   1
7   1
8   1
9   1
10  1
11  1
12  1
13  1
14  1
15  1
16  1
17  1
18  1
19  1
20  1
21  1
22  1
23  1
24  1
25  1
26  1
27  1
28  1
29  1
30  1
31  1
32  1
33  1
34  1
35  1
36  1
37  1
38  1
39  1
40  1
41  1
42  1
43  1
44  1
45  1
46  1
47  1
48  1
49  1
50  1
51  1
52  1
53  1
54  1
55  1
56  1
57  1
58  1
59  1
60  1
61  1
62  1
63  1
64  1
65  1
66  1
67  1
68  1
69  1
70  1
71  1
72  1
73  1
74  1
75  1
76  1
77  1
78  1
79  1
80  1
81  1
82  1
83  1
84  1
85  1
86  1
87  1
88  1
89  1
90  1
91  1
92  1
93  1
94  1
95  1
96  1
97  1
98  1
99  1
100 1
101 1
102 1
103 1
104 1
105 1
106 1
107 1
108 1
109 1
110 1
111 1
112 1
113 1
114 1
115 1
116 1
117 1
118 1
119 1
120 1
121 1
122 1
123 1
124 1
125 1
126 1
127 1
128 1
129 1
130 1
131 1
132 1
133 1
134 1
135 1
136 1
137 1
138 1
139 1
140 1
141 1
142 1
143 1
144 1
145 1
146 1
147 1
148 1
149 1
150 1
151 1
152 1
153 1
154 1
155 1
156 1
157 1
158 1
159 1
160 1
161 1
162 1
163 1
164 1
165 1
166 1
167 1
168 1
169 1
170 1
171 1
172 1
173 1
174 1
175 1
176 1
177 1
178 1
179 1
180 1
181 1
182 1
183 1
184 1
185 1
186 1
187 1
188 1
189 1
190 1
191 1
192 1
193 1
194 1
195 1
196 1
197 1
198 1
199 1
200 1
201 1
202 1
203 1
204 1
205 1
206 1
207 1
208 1
209 1
210 1
211 1
212 1
213 1
214 1
215 1
216 1
217 1
218 1
219 1
220 1
221 1
222 1
223 1
224 1
225 1
226 1
227 1
228 1
229 1
230 1
231 1
232 1
233 1
234 1
235 1
236 1
237 1
238 1
239 1
240 1
241 1
242 1
243 1
244 1
245 1
246 1
247 1
248 1
249 1
250 1
251 1
252 1
253 1
254 1
255 1
256 1
257 1
258 1
259 1
260 1
261 1
262 1
263 1
264 1
265 1
266 1
267 1
268 1
269 1
270 1
271 1
272 1
273 1
274 1
275 1
276 1
277 1
278 1
279 1
280 1
281 1
282 1
283 1
284 1
285 1
286 1
287 1
288 1
289 1
290 1
291 1
292 1
293 1
294 1
295 1
296 1
297 1
298 1
299 1
300 1
301 1
302 1
303 1
304 1
305 1
306 1
307 1
308 1
309 1
310 1
311 1
312 1
313 1
314 1
315 1
316 1
317 1
318 1
319 1
320 1
321 1
322 1
323 1
324 1
325 1
326 1
327 1
328 1
329 1
330 1
331 1
332 1
333 1
334 1
335 1
336 1
337 1
338 1
339 1
340 1
341 1
342 1
343 1
344 1
345 1
346 1
347 1
348 1
349 1
350 1
351 1
352 1
353 1
354 1
355 1
356 1
357 1
358 1
359 1
360 1
361 1
362 1
363 1
364 1
365 1
366 1
367 1
368 1
369 1
370 1
371 1
372 1
373 1
374 1
375 1
376 1
377 1
378 1
379 1
380 1
381 1
382 1
383 1
384 1
385 1
386 1
387 1
388 1
389 1
390 1
391 1
392 1
393 1
394 1
395 1
396 1
397 1
398 1
399 1
400 1
401 1
402 1
403 1
404 1
405 1
406 1
407 1
408 1
409 1
410 1
411 1
412 1
413 1
414 1
415 1
416 1
417 1
418 1
419 1
420 1
421 1
422 1
423 1
424 1
425 1
426 1
427 1
428 1
429 1
430 1
431 1
432 1
433 1
434 1
435 1
436 1
437 1
438 1
439 1
440 1
441 1
442 1
443 1
444 1
445 1
446 1
447 1
448 1
449 1
450 1
451 1
452 1
453 1
454 1
455 1
456 1
457 1
458 1
459 1
460 1
461 1
462 1
463 1
464 1
465 1
466 1
467 1
468 1
469 1
470 1
471 1
472 1
473 1
474 1
475 1
476 1
477 1
478 1
479 1
480 1
481 0
482 0
483 0
484 0
485 0
486 0
487 0
488 0
489 0
490 0
491 0
492 0
493 0
494 0
495 0
496 0
497 0
498 0
499 0
500 0
501 0
502 0
503 0
504 0
505 0
506 0
507 0
508 0
509 0
510 0
511 0
512 0
513 0
514 0
515 0
516 0
517 0
518 0
519 0
520 0
521 0
522 0
523 0
524 0
525 0
526 0
527 0
528 0
529 0
530 0
531 0
532 0
533 0
534 0
535 0
536 0
537 0
538 0
539 0
540 0
541 0
542 0
543 0
544 0
545 0
546 0
547 0
548 0
549 0
550 0
551 0
552 0
553 0
554 0
555 0
556 0
557 0
558 0
559 0
560 0
561 0
562 0
563 0
564 0
565 0
566 0
567 0
568 0
569 0
570 0
571 0
572 0
573 0
574 0
575 0
576 0
577 0
578 0
579 0
580 0
581 0
582 0
583 0
584 0
585 0
586 0
587 0
588 0
589 0
590 0
591 0
592 0
593 0
594 0
595 0
596 0
597 0
598 0
599 0
600 0
601 0
602 0
603 0
604 0
605 0
606 0
607 0
608 0
609 0
610 0
611 0
612 0
613 0
614 0
615 0
616 0
617 0
618 0
619 0
620 0
621 0
622 0
623 0
624 0
625 0
626 0
627 0
628 0
629 0
630 0
631 0
632 0
633 0
634 0
635 0
636 0
637 0
638 0
639 0
640 0
641 0
642 0
643 0
644 0
645 0
646 0
647 0
648 0
649 0
650 0
651 0
652 0
653 0
654 0
655 0
656 0
657 0
658 0
659 0
660 0
661 0
662 0
663 0
664 0
665 0
666 0
667 0
668 0
669 0
670 0
671 0
672 0
673 0
674 0
675 0
676 0
677 0
678 0
679 0
680 0
681 0
682 0
683 0
684 0
685 0
686 0
687 0
688 0
689 0
690 0
691 0
692 0
693 0
694 0
695 0
696 0
697 0
698 0
699 0
700 0
701 0
702 0
703 0
704 0
705 0
706 0
707 0
708 0
709 0
710 0
711 0
712 0
713 0
714 0
715 0
716 0
717 0
718 0
719 0
720 0
721 0
722 0
723 0
724 0
725 0
726 0
727 0
728 0
729 0
730 0
731 0
732 0
733 0
734 0
735 0
736 0
737 0
738 0
739 0
740 0
741 0
742 0
743 0
744 0
745 0
746 0
747 0
748 0
749 0
750 0
751 0
752 0
753 0
754 0
755 0
756 0
757 0
758 0
759 0
760 0
761 0
762 0
763 0
764 0
765 0
766 0
767 0
768 0
769 0
770 0
771 0
772 0
773 0
774 0
775 0
776 0
777 0
778 0
779 0
780 0
781 0
782 0
783 0
784 0
785 0
786 0
787 0
788 0
789 0
790 0
791 0
792 0
793 0
794 0
795 0
796 0
797 0
798 0
799 0
800 0
801 0
802 0
803 0
804 0
805 0
806 0
807 0
808 0
809 0
810 0
811 0
812 0
813 0
814 0
815 0
816 0
817 0
818 0
819 0
820 0
821 0
822 0
823 0
824 0
825 0
826 0
827 0
828 0
829 0
830 0
831 0
832 0
833 0
834 0
835 0
836 0
837 0
838 0
839 0
840 0
841 0
842 0
843 0
844 0
845 0
846 0
847 0
848 0
849 0
850 0
851 0
852 0
853 0
854 0
855 0
856 0
857 0
858 0
859 0
860 0
861 0
862 0
863 0
864 0
865 0
866 0
867 0
868 0
869 0
870 0
871 0
872 0
873 0
874 0
875 0
876 0
877 0
878 0
879 0
880 0
881 0
882 0
883 0
884 0
885 0
886 0
887 0
888 0
889 0
890 0
891 0
892 0
893 0
894 0
895 0
896 0
897 0
898 0
899 0
900 0
901 0
902 0
903 0
904 0
905 0
906 0
907 0
908 0
909 0
910 0
911 0
912 0
913 0
914 0
915 0
916 0
917 0
918 0
919 0
920 0
921 0
922 0
923 0
924 0
925 0
926 0
927 0
928 0
929 0
930 0
931 0
932 0
933 0
934 0
935 0
936 0
937 0
938 0
939 0
940 0
941 0
942 0
943 0
944 0
945 0
946 0
947 0
948 0
949 0
950 0
951 0
952 0
953 0
954 0
955 0
956 0
957 0
958 0
959 0
960 0
961 0
962 0
963 0
964 0
965 0
966 0
967 0
968 0
969 0
970 0
971 0
972 0
973 0
974 0
975 0
976 0
977 0
978 0
979 0
980 0
981 0
982 0
983 0
984 0
985 0
986 0
987 0
988 0
989 0
990 0
991 0
992 0
993 0
994 0
995 0
996 0
997 0
998 0
999 0
1000    0
1001    0
1002    0
1003    0
1004    0
1005    0
1006    0
1007    0
1008    0
1009    0
1010    0
1011    0
1012    0
1013    0
1014    0
1015    0
1016    0
1017    0
1018    0
1019    0
1020    0
1021    0
1022    0
1023    0
1024    0
1025    0
1026    0
1027    0
1028    0
1029    0
1030    0
1031    0
1032    0
1033    0
1034    0
1035    0
1036    0
1037    0
1038    0
1039    0
1040    0
1041    0
1042    0
1043    0
1044    0
1045    0
1046    0
1047    0
1048    0
1049    0
1050    0
1051    0
1052    0
1053    0
1054    0
1055    0
1056    0
1057    0
1058    0
1059    0
1060    0
1061    0
1062    0
1063    0
1064    0
1065    0
1066    0
1067    0
1068    0
1069    0
1070    0
1071    0
1072    0
1073    0
1074    0
1075    0
1076    0
1077    0
1078    0
1079    0
1080    0
1081    0
1082    0
1083    0
1084    0
1085    0
1086    0
1087    0
1088    0
1089    1
1090    1
1091    1
1092    1
1093    1
1094    1
1095    1
1096    1
1097    1
1098    1
1099    1
1100    1
1101    1
1102    1
1103    1
1104    1
1105    1
1106    1
1107    1
1108    1
1109    1
1110    1
1111    1
1112    1
1113    1
1114    1
1115    1
1116    1
1117    1
1118    1
1119    1
1120    1
1121    1
1122    1
1123    1
1124    1
1125    1
1126    1
1127    1
1128    1
1129    1
1130    1
1131    1
1132    1
1133    1
1134    1
1135    1
1136    1
1137    1
1138    1
1139    1
1140    1
1141    1
1142    1
1143    1
1144    1
1145    1
1146    1
1147    1
1148    1
1149    1
1150    1
1151    1
1152    1
1153    1
1154    1
1155    1
1156    1
1157    1
1158    1
1159    1
1160    1
1161    1
1162    1
1163    1
1164    1
1165    1
1166    1
1167    1
1168    1
1169    1
1170    1
1171    1
1172    1
1173    1
1174    1
1175    1
1176    1
1177    1
1178    1
1179    1
1180    1
1181    1
1182    1
1183    1
1184    1
1185    1
1186    1
1187    1
1188    1
1189    1
1190    1
1191    1
1192    1
1193    1
1194    1
1195    1
1196    1
1197    1
1198    1
1199    1
1200    1
1201    1
1202    1
1203    1
1204    1
1205    1
1206    1
1207    1
1208    1
1209    1
1210    1
1211    1
1212    1
1213    1
1214    1
1215    1
1216    1
1217    1
1218    1
1219    1
1220    1
1221    1
1222    1
1223    1
1224    1
1225    1
1226    1
1227    1
1228    1
1229    1
1230    1
1231    1
1232    1
1233    1
1234    1
1235    1
1236    1
1237    1
1238    1
1239    1
1240    1
1241    1
1242    1
1243    1
1244    1
1245    1
1246    1
1247    1
1248    1
1249    1
1250    1
1251    1
1252    1
1253    1
1254    1
1255    1
1256    1
1257    1
1258    1
1259    1
1260    1
1261    1
1262    1
1263    1
1264    1
1265    1
1266    1
1267    1
1268    1
1269    1
1270    1
1271    1
1272    1
1273    1
1274    1
1275    1
1276    1
1277    1
1278    1
1279    1
1280    1
1281    1
1282    1
1283    1
1284    1
1285    1
1286    1
1287    1
1288    1
1289    1
1290    1
1291    1
1292    1
1293    1
1294    1
1295    1
1296    1
1297    1
1298    1
1299    1
1300    1
1301    1
1302    1
1303    1
1304    1
1305    1
1306    1
1307    1
1308    1
1309    1
1310    1
1311    1
1312    1
1313    1
1314    1
1315    1
1316    1
1317    1
1318    1
1319    1
1320    1
1321    1
1322    1
1323    1
1324    1
1325    1
1326    1
1327    1
1328    1
1329    1
1330    1
1331    1
1332    1
1333    1
1334    1
1335    1
1336    1
1337    1
1338    1
1339    1
1340    1
1341    1
1342    1
1343    1
1344    1
1345    1
1346    1
1347    1
1348    1
1349    1
1350    1
1351    1
1352    1
1353    1
1354    1
1355    1
1356    1
1357    1
1358    1
1359    1
1360    1
1361    1
1362    1
1363    1
1364    1
1365    1
1366    1
1367    1
1368    1
1369    1
1370    1
1371    1
1372    1
1373    1
1374    1
1375    1
1376    1
1377    1
1378    1
1379    1
1380    1
1381    1
1382    1
1383    1
1384    1
1385    1
1386    1
1387    1
1388    1
1389    1
1390    1
1391    1
1392    1
1393    1
1394    1
1395    1
1396    1
1397    1
1398    1
1399    1
1400    1
1401    1
1402    1
1403    1
1404    1
1405    1
1406    1
1407    1
1408    1
1409    1
1410    1
1411    1
1412    1
1413    1
1414    1
1415    1
1416    1
1417    1
1418    1
1419    1
1420    1
1421    1
1422    1
1423    1
1424    1
1425    1
1426    1
1427    1
1428    1
1429    1
1430    1
1431    1
1432    1
1433    1
1434    1
1435    1
1436    1
1437    1
1438    1
1439    1
1440    1

I am using the XLMiner analysis tool (it is a Excel add on as well as a google drive add on) to create the coefficients and it generated the following coefficients: 0.7861840684, -0.0006480269158.

I've plugged in the relevant variables in the following equation which is used in logistic regression: prediction = (Math.exp(betaZero + betaOne * currentTime)) / (1 + Math.exp(betaZero + betaOne * currentTime)); where betaZero = 0.7287119626, betaOne = -0.0006480269158 and currentTime is the current time on the phone represented in the amount of minutes that has passed since 00:00am.

When I compute the value of the prediction, is always predicts >0.5, even during the time where the data specifies that it should be <0.5. I'm not sure if I've calculated this right. This is my first exposure to machine learning and in this type of mathematics.

I am using Java.

EDIT = After calculating the probability for all time (1 - 1440), I can see that the probability NEVER drops below 0.5... infact, it starts at 0.6744390764013958 and as time increases, the probability drops only till 0.5451722142011737.

$\endgroup$
6
  • 3
    $\begingroup$ Your data has a striking pattern. What question are you trying to address with your regression? You may have a situation where logistic regression doesn't help much given the obvious pattern in your data. $\endgroup$
    – Todd D
    Commented Jan 17, 2018 at 18:30
  • 2
    $\begingroup$ You don't have 1440 observations: you have three, of which two are incomplete! They consist of an interval ending at 480, the interval [481,1088], and an interval beginning at 1089. There's not a whole lot anyone can legitimately infer from such a small amount of data. What is the purpose of your app? Do you in practice have much more information? $\endgroup$
    – whuber
    Commented Jan 17, 2018 at 18:49
  • 1
    $\begingroup$ What is the trouble? You have a balanced quadratic trend in risk, so the prediction using a linear term is a flat response. The implementation was a success. The model choice was poor. $\endgroup$
    – AdamO
    Commented Jan 17, 2018 at 19:43
  • 1
    $\begingroup$ @AdamO what model you do you reckon I should go with? $\endgroup$ Commented Jan 17, 2018 at 20:02
  • 2
    $\begingroup$ I disagree with the vote to close here. This is essentially about what logistic regression does and doesn't do and what to do instead. It's not focused on code or programming. The use of Stata in my answer is just to confirm the OP's calculations (their code being invisible here) . $\endgroup$
    – Nick Cox
    Commented Jan 18, 2018 at 15:36

1 Answer 1

4
$\begingroup$

This is what I get with Stata for your data.

. logit occ time

Iteration 0:   log likelihood = -980.63877  
Iteration 1:   log likelihood = -968.10592  
Iteration 2:   log likelihood = -968.09925  
Iteration 3:   log likelihood = -968.09925  

Logistic regression                             Number of obs     =      1,440
                                                LR chi2(1)        =      25.08
                                                Prob > chi2       =     0.0000
Log likelihood = -968.09925                     Pseudo R2         =     0.0128

------------------------------------------------------------------------------
    occupied |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        time |   -.000648   .0001305    -4.97   0.000    -.0009037   -.0003923
       _cons |   .7861841   .1102782     7.13   0.000     .5700429    1.002325
------------------------------------------------------------------------------

We agree on the coefficients to the precision shown above, so your code looks good. I didn't ask Stata for more decimal places.

I don't see where 0.7287119626 comes from, however.

The real question is whether your results are surprising. A graph of the data resolves the puzzlement.

enter image description here

Logit regression can't be smarter than what you ask it to do. Here with just time as a predictor the best it can do is fit a monotonic curve, which for these data doesn't vary that much, as you report. In broad terms you have three blocks of points that vote in different ways. The 1s pull up the curve; the 0s pull it down. The first 480 points are 1s and the last 352 are 1s, so that asymmetry drives a discernible downward slope.

To get closer to the data you'd need more predictors (e.g. a quadratic would give you a turning point too), but it's hard to see why that would be interesting or useful in this case. The data are deterministic and defined by two steps at particular times. Logit regression can't get very close to that unless you tautologically use stepped predictors for the purpose (and there are subtle problems with such deterministic cases any way).

$\endgroup$
8
  • 2
    $\begingroup$ This was almost exactly the answer I was writing : ) $\endgroup$ Commented Jan 17, 2018 at 19:07
  • $\begingroup$ @NickCox Thanks for the in depthanswer. It actually helped me understand it more. I came up with this data as I am using it to predict when a parking bay is occupied. These are pretty much dummy values. In the above data, person A parks his car until 9amish and parks it back at 6pmish. I was under the assumption that logistic regression would be suitable for this approach as there are only 2 outcomes; 0 or 1.. what do you reckon? $\endgroup$ Commented Jan 17, 2018 at 19:25
  • 1
    $\begingroup$ Broadly, my view is that it's unsuitable for your data for the reasons stated. In real data you could presumably focus on times of arrival and departure, which could be expected to vary. $\endgroup$
    – Nick Cox
    Commented Jan 17, 2018 at 19:30
  • $\begingroup$ @NickCox I could add days into the data so add data for 7 days of the week as the bay will be used differently on weekdays than it is used on weekends? More data means more variety hence a better prediction...? $\endgroup$ Commented Jan 17, 2018 at 19:38
  • 2
    $\begingroup$ You need to think about what is random in your problem. Logistic regression assumes that the response is random, but governed by a probability. In your case, the boundaries seem random, while the observations between the boundaries are deterministic. $\endgroup$ Commented Jan 17, 2018 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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