# I have a dataset containing family information of married couples, which have around 10 variables & # 600+ observations. 
# Independent variables are ~ gender, age, years married, children, religion etc.
# I have one response variable which is number of extra marital affairs.
#  Now, I want to know what all factor influence the chances of extra marital affair.
# Since extra marital affair is a binary variable (either a person will have or not), 
# so we can fit logistic regression model here to predict the probability of extra marital affair.

# install.packages('AER')
library('AER')
## Warning: package 'AER' was built under R version 3.5.1
## Loading required package: car
## Warning: package 'car' was built under R version 3.5.1
## Loading required package: carData
## Warning: package 'carData' was built under R version 3.5.1
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 3.5.1
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.5.1
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 3.5.1
## Loading required package: survival
library(plyr)
## Warning: package 'plyr' was built under R version 3.5.1
affairs <- data("Affairs")
View(Affairs)

affairs1 <- Affairs
summary(affairs1)
##     affairs          gender         age         yearsmarried    children 
##  Min.   : 0.000   female:315   Min.   :17.50   Min.   : 0.125   no :171  
##  1st Qu.: 0.000   male  :286   1st Qu.:27.00   1st Qu.: 4.000   yes:430  
##  Median : 0.000                Median :32.00   Median : 7.000            
##  Mean   : 1.456                Mean   :32.49   Mean   : 8.178            
##  3rd Qu.: 0.000                3rd Qu.:37.00   3rd Qu.:15.000            
##  Max.   :12.000                Max.   :57.00   Max.   :15.000            
##  religiousness     education       occupation        rating     
##  Min.   :1.000   Min.   : 9.00   Min.   :1.000   Min.   :1.000  
##  1st Qu.:2.000   1st Qu.:14.00   1st Qu.:3.000   1st Qu.:3.000  
##  Median :3.000   Median :16.00   Median :5.000   Median :4.000  
##  Mean   :3.116   Mean   :16.17   Mean   :4.195   Mean   :3.932  
##  3rd Qu.:4.000   3rd Qu.:18.00   3rd Qu.:6.000   3rd Qu.:5.000  
##  Max.   :5.000   Max.   :20.00   Max.   :7.000   Max.   :5.000
table(affairs1$affairs)
## 
##   0   1   2   3   7  12 
## 451  34  17  19  42  38
affairs1$ynaffairs[affairs1$affairs > 0] <- 1
affairs1$ynaffairs[affairs1$affairs == 0] <- 0
affairs1$gender <- as.factor(revalue(Affairs$gender,c("male"=1, "female"=0)))
affairs1$children <- as.factor(revalue(Affairs$children,c("yes"=1, "no"=0)))
# sum(is.na(claimants))
# claimants <- na.omit(claimants) # Omitting NA values from the Data 
# na.omit => will omit the rows which has atleast 1 NA value
View(affairs1)



colnames(affairs1)
##  [1] "affairs"       "gender"        "age"           "yearsmarried" 
##  [5] "children"      "religiousness" "education"     "occupation"   
##  [9] "rating"        "ynaffairs"
class(affairs1)
## [1] "data.frame"
attach(affairs1)
## The following object is masked _by_ .GlobalEnv:
## 
##     affairs
# Preparing a linear regression 
mod_lm <- lm(ynaffairs ~ factor(gender) + age+ yearsmarried+ factor(children) + religiousness+
               education+occupation+rating, data = affairs1)
summary(mod_lm)
## 
## Call:
## lm(formula = ynaffairs ~ factor(gender) + age + yearsmarried + 
##     factor(children) + religiousness + education + occupation + 
##     rating, data = affairs1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.6336 -0.2691 -0.1632  0.1151  1.0659 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        0.736107   0.151502   4.859 1.51e-06 ***
## factor(gender)1    0.045201   0.040022   1.129 0.259180    
## age               -0.007420   0.003013  -2.463 0.014057 *  
## yearsmarried       0.015981   0.005491   2.911 0.003743 ** 
## factor(children)1  0.054487   0.046642   1.168 0.243198    
## religiousness     -0.053698   0.014881  -3.608 0.000334 ***
## education          0.003078   0.008542   0.360 0.718699    
## occupation         0.005913   0.011838   0.499 0.617643    
## rating            -0.087455   0.015984  -5.472 6.59e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4122 on 592 degrees of freedom
## Multiple R-squared:  0.1066, Adjusted R-squared:  0.09452 
## F-statistic: 8.829 on 8 and 592 DF,  p-value: 1.884e-11
pred1 <- predict(mod_lm,affairs1)
pred1
##             4             5            11            16            23 
##  0.2524486205  0.1136406616  0.4321912829  0.0376692455  0.3480920311 
##            29            44            45            47            49 
##  0.0598488898  0.2579352306  0.2620791697  0.4583221131  0.0567108409 
##            50            55            64            80            86 
##  0.6216067585  0.2256431201  0.1962503502  0.2215084253  0.1077280175 
##            93           108           114           115           116 
##  0.3466765722  0.4650551913  0.1527463202  0.1309746469  0.2543916193 
##           123           127           129           134           137 
##  0.1309746469  0.1309746469  0.2642586794  0.1635916625  0.2649915195 
##           139           147           151           153           155 
##  0.1371315087  0.0507222008  0.2425877369  0.1798161294  0.2251484823 
##           162           163           165           168           170 
##  0.3837409472  0.0772767840  0.1108171637  0.1730030818  0.3496346145 
##           172           184           187           192           194 
##  0.1984896163  0.2505913693  0.0666100566  0.2663790946  0.0416403963 
##           210           217           220           224           227 
##  0.1494311683  0.2496820346  0.0102214562  0.5502034627  0.2160492556 
##           228           239           241           245           249 
##  0.1624309141  0.1409295924  0.3464103939  0.0743402739  0.4036813253 
##           262           265           267           269           271 
## -0.0600869029  0.3500368589  0.0906467715  0.3276864727  0.2759835016 
##           277           290           292           293           295 
##  0.5143437930  0.1511124967  0.1619324074  0.0650247943  0.2905843787 
##           299           320           321           324           334 
##  0.3226369710  0.3071801475  0.1189888355  0.1792709007  0.1881538240 
##           351           355           361           362           366 
##  0.2892527528  0.2534822847  0.1709893027 -0.0301189416  0.1872405445 
##           370           374           378           381           382 
##  0.0832058255  0.0739338692  0.0966869358  0.3163429796  0.4197447991 
##           383           384           400           403           409 
##  0.1740615120  0.1317640934  0.1888072044  0.0772767840  0.4547277970 
##           412           413           416           418           422 
##  0.2224269094  0.0975239701  0.1946188949  0.1264885131  0.0421019306 
##           435           439           445           447           448 
##  0.2638114206  0.2226850778  0.3086975944  0.1019566552  0.2938503251 
##           449           478           482           486           489 
##  0.2890798445  0.4239501283  0.2352559170  0.2128241583  0.2510943890 
##           490           491           492           503           508 
##  0.0618971579  0.4887294995  0.4022621495  0.2844822722  0.2459323462 
##           509           512           515           517           532 
##  0.0932980309  0.3547211485 -0.0166214340  0.0438261073  0.2820294063 
##           533           535           537           538           543 
##  0.2504314904  0.2676737214  0.1790666736  0.4931724126  0.2122731326 
##           547           550           558           571           578 
##  0.0711858470  0.1914956279 -0.0981662678  0.1685949719  0.2155259875 
##           583           586           594           597           602 
##  0.1914450369  0.4720898711  0.1762423448 -0.0120574665  0.1862637781 
##           603           604           612           613           621 
##  0.2185178798  0.1853168054  0.3685930104  0.4749712783  0.0772767840 
##           627           630           631           632           639 
##  0.1922344834  0.0969311535  0.0316782528  0.1140413903  0.1480650534 
##           645           647           648           651           655 
##  0.3280634506  0.2026733904  0.0997851969  0.3660068839  0.3621529339 
##           667           670           671           673           701 
##  0.3669167191  0.1310583414  0.3537050497  0.1384319902  0.3324936169 
##           705           706           709           717           719 
##  0.3356901051  0.2654336375  0.2829606207  0.3174076314  0.3184658829 
##           723           724           726           734           735 
##  0.3066198764  0.2074167523  0.1564468032  0.1274641685  0.4226104480 
##           736           737           739           743           745 
##  0.2929943868  0.3270314503  0.3774794551  0.4208430412  0.3815187596 
##           747           751           752           754           760 
##  0.3837409472  0.3467496527  0.4150631573  0.3428153453  0.1964118982 
##           763           774           776           779           784 
##  0.2003820726  0.1944694285  0.1860776216  0.1297441260  0.2602823028 
##           788           794           795           798           800 
##  0.2827580337  0.4100148069  0.1048774070  0.3137749448  0.3023766965 
##           803           807           812           820           823 
##  0.0975239701 -0.0104645722  0.1098937997  0.2927125310  0.1059420587 
##           830           843           848           851           854 
##  0.4030081107  0.1675827421  0.3480038287  0.2430181112  0.2638030386 
##           856           857           859           863           865 
##  0.3630617137  0.0488192504  0.2755017358  0.0063254509  0.0964744318 
##           867           870           873           875           876 
##  0.0990526483  0.0733903043  0.2458277159  0.1789900194  0.4197447991 
##           877           880           903           904           905 
##  0.1076581851  0.1957432885  0.1686271217  0.3274750496  0.4350035354 
##           908           909           910           912           914 
##  0.4179261584  0.4056951044  0.1189051410  0.3785215213  0.0266573521 
##           915           916           920           921           925 
##  0.0173361598  0.1784297483  0.0831894281  0.2963304358  0.5295670869 
##           926           929           931           945           947 
##  0.2598758675  0.4089062955  0.1548650715  0.2247365400  0.3635721098 
##           949           950           961           965           966 
##  0.3498720050  0.2222856388  0.3765391779  0.1301665980  0.1309910442 
##           967           987           990           992           995 
## -0.1150995243  0.0777714218  0.2867221773  0.3604110092  0.1804091952 
##          1009          1021          1026          1027          1030 
##  0.3371203458  0.1995949595  0.1113787958  0.1562356943  0.2448901483 
##          1031          1034          1037          1038          1039 
##  0.3090736189  0.2302350163  0.2632787030  0.2215491167  0.0564403415 
##          1045          1046          1054          1059          1063 
##  0.2101051758  0.0948364234  0.1574719648  0.1711558107  0.0688640413 
##          1068          1070          1072          1073          1077 
##  0.2948720999 -0.0595984675 -0.0040486083  0.2324436645  0.2640472563 
##          1081          1083          1084          1086          1087 
##  0.3441242148  0.2690619547  0.4259016609  0.1859729912  0.2829229826 
##          1089          1096          1102          1103          1107 
##  0.2285402610  0.2521874791  0.1274203089  0.2123514812  0.2796898309 
##          1109          1115          1119          1124          1126 
##  0.2531594041  0.2923750433  0.1400378489  0.1957432885  0.2419434411 
##          1128          1129          1130          1133          1140 
##  0.2139725527  0.1011672087  0.4036813253  0.1274660014  0.2187620975 
##          1143          1146          1153          1156          1157 
##  0.1419093597  0.3964598117  0.3975778599  0.1248177851  0.2707104884 
##          1158          1160          1161          1166          1177 
##  0.2176385603  0.6099142305  0.2409401017  0.1970049640  0.1348589216 
##          1178          1180          1187          1191          1195 
##  0.2459323462  0.0915329774  0.2898239985  0.4854354519  0.1200417411 
##          1207          1208          1209          1211          1215 
##  0.5737634786  0.2222985108  0.2929787094  0.2046961964  0.1989287031 
##          1221          1226          1229          1231          1234 
##  0.3333876937  0.2895682799  0.1822917709  0.3442035786  0.2463627205 
##          1235          1242          1245          1260          1266 
##  0.3418295141  0.1106529214  0.4362695279  0.4089596919  0.3835124879 
##          1271          1273          1276          1280          1282 
##  0.1837396029  0.1761760724  0.2737771868  0.1984896163  0.1160506615 
##          1285          1295          1298          1299          1304 
##  0.3570158089  0.0001145478  0.1399821192  0.4087735353  0.2056970660 
##          1305          1311          1314          1319          1322 
##  0.4865523488  0.1964118982  0.2897798899  0.1227224273  0.1874921326 
##          1324          1327          1328          1330          1332 
##  0.2459929295  0.1913037430  0.0406196041  0.4056951044  0.0761572202 
##          1333          1336          1341          1344          1352 
##  0.3474917382  0.1361547423  0.0995597099  0.3687545585  0.2739099470 
##          1358          1359          1361          1364          1368 
##  0.0209688464  0.1214030345  0.3169191960  0.1947794180  0.1738033881 
##          1384          1390          1393          1394          1402 
##  0.0393971270  0.1309746469  0.2317999819  0.1897992087  0.0820472647 
##          1407          1408          1412          1413          1416 
##  0.2138488195  0.3870855565  0.1985222983  0.0798465132  0.1930289310 
##          1417          1418          1419          1420          1423 
##  0.1896714497  0.1973491472  0.2019125742  0.0871411691  0.1739034853 
##          1424          1432          1433          1437          1438 
##  0.4266442278 -0.0255089841  0.1588539765  0.2496539334  0.2605484812 
##          1439          1446          1450          1451          1452 
##  0.2451962961  0.4350035354  0.2781824221  0.1436687067  0.2267809141 
##          1453          1456          1464          1469          1473 
## -0.0051555942  0.0982350680  0.4085827140  0.2163015278  0.2526601562 
##          1481          1482          1496          1497          1504 
##  0.0938725529  0.0432531209  0.0746512000  0.0572662647  0.0449525411 
##          1513          1515          1534          1535          1536 
##  0.2095392287  0.4420583430  0.1701509556  0.1501625271  0.0816284958 
##          1540          1551          1555          1557          1566 
##  0.2568268940 -0.0594959591  0.1122504114  0.1635916625  0.0823760333 
##          1567          1576          1584          1585          1590 
##  0.1980424971  0.0711858470  0.0789485675  0.3707564593  0.1748773979 
##          1594          1595          1603          1608          1609 
##  0.3732439791  0.2941226440  0.2505120055  0.1408153768  0.0626234850 
##          1615          1616          1617          1620          1621 
##  0.1593647163  0.1218052789  0.2494535752  0.0416403963  0.1793050944 
##          1637          1638          1650          1654          1665 
##  0.2420104295  0.1163996965  0.2031290312  0.2468697821  0.4688144165 
##          1670          1671          1675          1688          1691 
##  0.0115094153  0.3345523110  0.2064441830  0.1309746469  0.2986251751 
##          1695          1698          1704          1705          1711 
##  0.0630927177  0.2721465879  0.3563169441  0.5187372958  0.4317123225 
##          1719          1723          1726          1749          1752 
##  0.0368088208  0.1905851538  0.1606171924  0.1836423469  0.3572436293 
##          1754          1758          1761          1773          1775 
##  0.0577609024  0.0449525411  0.2305182310  0.3010215913  0.3412573788 
##          1786          1793          1799          1803          1806 
##  0.0515268765  0.3143292005  0.1624928655  0.2723022812  0.1227032247 
##          1807          1808          1814          1815          1818 
##  0.3278558965  0.2254794174  0.2762585301  0.2102233289  0.0474693459 
##          1827          1834          1835          1843          1846 
##  0.3705179176 -0.0021647647  0.3905471676  0.1887148111  0.1807404953 
##          1850          1851          1854          1859          1861 
##  0.1632203606  0.2521173379  0.6335648069  0.0275289678  0.3812263088 
##          1866          1873          1875          1885          1892 
##  0.1199614048  0.1960746596  0.1865079958  0.3408194792  0.2785148728 
##          1895          1896          1897          1899          1904 
##  0.3964755701  0.0803552149  0.2107394010 -0.0123070301  0.4626575547 
##          1905          1908          1916          1918          1920 
##  0.2340489968  0.3104460939  0.2267009446  0.3309900571  0.5620496480 
##          1930          1940          1947          1949          1951 
##  0.3498720050  0.1679908413  0.2402058586  0.1189051410  0.1385647504 
##          1952          1960          9001          9012          9023 
##  0.0573747639  0.1250620028  0.1803129669  0.5296318436  0.3719376197 
##          9029             6            12            43            53 
##  0.2521173379  0.1730756809  0.1140425584  0.4484436642  0.4588435742 
##            67            79           122           126           133 
##  0.0468063593  0.1556545180  0.4839151593  0.2004478444  0.4346265575 
##           138           154           159           174           176 
##  0.4578336777  0.4328010896  0.2659835238  0.4140643258  0.5357887054 
##           181           182           186           189           204 
##  0.5058631142  0.2447940517  0.4480891401  0.0832058255  0.1906038845 
##           215           232           233           252           253 
##  0.0425329962  0.2282269132  0.1328101330  0.2303972034  0.3909819113 
##           274           275           287           288           325 
##  0.2328578064  0.3512255915  0.4075968828  0.1402105785  0.3276612062 
##           328           344           353           354           367 
##  0.1973491472  0.5138621848 -0.0207823742  0.1222075233  0.2956001406 
##           369           390           392           423           432 
##  0.4407834193  0.3964298417  0.2470969635  0.3294771373  0.4454424424 
##           436           483           513           516           518 
##  0.4169324849  0.2934733472  0.1805458243  0.6095906490  0.1859729912 
##           520           526           528           553           576 
##  0.1624928655  0.4592595491  0.1948020177  0.0920022102  0.1487701551 
##           611           625           635           646           657 
##  0.1872592752  0.4348707753  0.3670625088  0.1669571770  0.1687605268 
##           659           666           679           729           755 
##  0.2822573652  0.2406081031  0.2837810547  0.5944207515  0.4742578279 
##           758           770           786           797           811 
##  0.3164430769  0.2962855997  0.2888019747  0.3660355143  0.3687545585 
##           834           858           885           893           927 
##  0.2007641892  0.3250123252  0.4016116397  0.4899617148  0.6031142124 
##           928           933           951           968           972 
##  0.2002819753  0.3605470752  0.4884948186  0.0848382572  0.3272842283 
##           975           977           981           986          1002 
##  0.3497550032  0.4276430593  0.1848469337  0.3234449812  0.2200197472 
##          1007          1011          1035          1050          1056 
##  0.3779610633  0.3198990120  0.4061447298  0.3632232617  0.3619415109 
##          1057          1075          1080          1125          1131 
##  0.1865332623  0.5357887054  0.4468822200  0.3552618291  0.3676842307 
##          1138          1150          1163          1169          1198 
##  0.3458144245  0.3669167191  0.2743160603  0.1551857857  0.5079495407 
##          1204          1218          1230          1236          1247 
##  0.1931222010  0.5717170214  0.4791999272  0.3440708184  0.5004213401 
##          1259          1294          1353          1370          1427 
##  0.5779263072  0.0320008192  0.2591835057  0.4366999546  0.4653056113 
##          1445          1460          1480          1505          1543 
##  0.4394525892  0.4350821983  0.4225570516  0.6369094161  0.4225022963 
##          1548          1550          1561          1564          1573 
##  0.5836842304  0.0969509838  0.4811405640  0.4256354825  0.1276683956 
##          1575          1599          1622          1629          1664 
##  0.1469958937  0.3229769166 -0.0659362673  0.2301851413  0.3863467011 
##          1669          1674          1682          1685          1697 
##  0.5285976530  0.1827290176  0.2595209934  0.2127999260  0.1885410260 
##          1716          1730          1731          1732          1743 
##  0.4002869978  0.3433319630  0.2185021215  0.4890383569  0.4251250864 
##          1751          1757          1763          1766          1772 
##  0.3808240644  0.4267556853  0.3870855565  0.3269477557  0.4233064457 
##          1776          1782          1784          1791          1831 
##  0.2293365774  0.2384487211  0.2721775755  0.3011493503  0.5176884023 
##          1840          1844          1856          1876          1929 
##  0.3675755527  0.5187372958  0.4971820771  0.3719910161  0.3477438526 
##          1935          1938          1941          1954          1959 
##  0.4636530518  0.2543095887  0.3407091728  0.3012894641  0.5392970174 
##          9010 
##  0.2434970716
# plot(affairs,pred1)
# We can no way use the linear regression technique to classify the data
plot(pred1)

# We can also include NA values but where ever it finds NA value
# probability values obtained using the glm will also be NA 
# So they can be either filled using imputation technique or
# exlclude those values 


# GLM function use sigmoid curve to produce desirable results 
# The output of sigmoid function lies in between 0-1
model <- glm(ynaffairs ~ factor(gender) + age+ yearsmarried+ factor(children) + religiousness+
               education+occupation+rating, data = affairs1,family = "binomial")

# To calculate the odds ratio manually we going r going to take exp of coef(model)
exp(coef(model))
##       (Intercept)   factor(gender)1               age      yearsmarried 
##         3.9640180         1.3235091         0.9567099         1.0994093 
## factor(children)1     religiousness         education        occupation 
##         1.4883560         0.7227292         1.0212740         1.0314027 
##            rating 
##         0.6259691
# Confusion matrix table 
prob <- predict(model,affairs1,type="response")
summary(model)
## 
## Call:
## glm(formula = ynaffairs ~ factor(gender) + age + yearsmarried + 
##     factor(children) + religiousness + education + occupation + 
##     rating, family = "binomial", data = affairs1)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.5713  -0.7499  -0.5690  -0.2539   2.5191  
## 
## Coefficients:
##                   Estimate Std. Error z value Pr(>|z|)    
## (Intercept)        1.37726    0.88776   1.551 0.120807    
## factor(gender)1    0.28029    0.23909   1.172 0.241083    
## age               -0.04426    0.01825  -2.425 0.015301 *  
## yearsmarried       0.09477    0.03221   2.942 0.003262 ** 
## factor(children)1  0.39767    0.29151   1.364 0.172508    
## religiousness     -0.32472    0.08975  -3.618 0.000297 ***
## education          0.02105    0.05051   0.417 0.676851    
## occupation         0.03092    0.07178   0.431 0.666630    
## rating            -0.46845    0.09091  -5.153 2.56e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 675.38  on 600  degrees of freedom
## Residual deviance: 609.51  on 592  degrees of freedom
## AIC: 627.51
## 
## Number of Fisher Scoring iterations: 4
# We are going to use NULL and Residual Deviance to compare the between different models

# Confusion matrix and considering the threshold value as 0.5 
confusion<-table(prob>0.5,affairs1$ynaffairs)
confusion
##        
##           0   1
##   FALSE 435 125
##   TRUE   16  25
# Model Accuracy 
Accuracy<-sum(diag(confusion)/sum(confusion))
Accuracy # 76.53
## [1] 0.765391
# Creating empty vectors to store predicted classes based on threshold value
pred_values <- NULL
yes_no <- NULL

pred_values <- ifelse(prob>=0.5,1,0)
yes_no <- ifelse(prob>=0.5,"yes","no")

 # Creating new column to store the above values
affairs1[,"prob"] <- prob
affairs1[,"pred_values"] <- pred_values
affairs1[,"yes_no"] <- yes_no

View(affairs1[,c(1,9:11)])

table(affairs1$ynaffairs,affairs1$pred_values)
##    
##       0   1
##   0 435  16
##   1 125  25
# Calculate the below metrics
# precision | recall | True Positive Rate | False Positive Rate | Specificity | Sensitivity
# from the above table - 59


# ROC Curve => used to evaluate the betterness of the logistic model
# more area under ROC curve better is the model 
# We will use ROC curve for any classification technique not only for logistic
# install.packages("ROCR")
library(ROCR)
## Warning: package 'ROCR' was built under R version 3.5.1
## Loading required package: gplots
## Warning: package 'gplots' was built under R version 3.5.1
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
rocrpred<-prediction(prob,affairs1$ynaffairs)
rocrperf<-performance(rocrpred,'tpr','fpr')

str(rocrperf)
## Formal class 'performance' [package "ROCR"] with 6 slots
##   ..@ x.name      : chr "False positive rate"
##   ..@ y.name      : chr "True positive rate"
##   ..@ alpha.name  : chr "Cutoff"
##   ..@ x.values    :List of 1
##   .. ..$ : num [1:570] 0 0 0.00222 0.00443 0.00443 ...
##   ..@ y.values    :List of 1
##   .. ..$ : num [1:570] 0 0.00667 0.00667 0.00667 0.01333 ...
##   ..@ alpha.values:List of 1
##   .. ..$ : num [1:570] Inf 0.725 0.709 0.709 0.696 ...
plot(rocrperf,colorize=T,text.adj=c(-0.2,1.7))

# More area under the ROC Curve better is the logistic regression model obtained

## Getting cutt off or threshold value along with true positive and false positive rates in a data frame 
str(rocrperf)
## Formal class 'performance' [package "ROCR"] with 6 slots
##   ..@ x.name      : chr "False positive rate"
##   ..@ y.name      : chr "True positive rate"
##   ..@ alpha.name  : chr "Cutoff"
##   ..@ x.values    :List of 1
##   .. ..$ : num [1:570] 0 0 0.00222 0.00443 0.00443 ...
##   ..@ y.values    :List of 1
##   .. ..$ : num [1:570] 0 0.00667 0.00667 0.00667 0.01333 ...
##   ..@ alpha.values:List of 1
##   .. ..$ : num [1:570] Inf 0.725 0.709 0.709 0.696 ...
rocr_cutoff <- data.frame(cut_off = rocrperf@alpha.values[[1]],fpr=rocrperf@x.values,tpr=rocrperf@y.values)
colnames(rocr_cutoff) <- c("cut_off","FPR","TPR")
View(rocr_cutoff)

library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.1
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
rocr_cutoff$cut_off <- round(rocr_cutoff$cut_off,6)
# Sorting data frame with respect to tpr in decreasing order 
rocr_cutoff <- arrange(rocr_cutoff,desc(TPR))
## Warning: package 'bindrcpp' was built under R version 3.5.1
View(rocr_cutoff)