Help with ROC and AUC Curves

I am trying to create an ROC plot however I am having difficulty. I am using the NHANES package. My Y variable= Depressed and X=Sexual Orientation. Can someone help me understand what I am doing wrong and what I need to do create this plot?

install.packages("NHANES")
library(NHANES)

Project<- Project[complete.cases(Project), ]
describe(Project)
Project.Final.glm<-glm(Depressed ~ Gender + Education + Poverty + BMI + Alcohol12PlusYr +
HardDrugs + SexOrientation + SexOrientation:RegularMarij +
Poverty:SexOrientation, family=binomial, data=Project)
summary(Project.Final.glm)

rocplot <- roc(Depressed~fitted(Project.Final.glm), data=Project)
rocplot <- multiclass.roc(Depressed~fitted(Project.Final.glm), data = Project)
auc(rocplot)
plot.roc(rocplot,legacy.axes=TRUE, main = "Figure 1: ROC/AUC Analysis")

plot.roc(rocplot,legacy.axes=TRUE, main = "Figure 1: ROC/AUC Analysis")
Error in roc.default(x, predictor, plot = TRUE, ...) :
No valid data provided.

This is where I can't follow the reprex

Please use require in preference to install.packages

require(NHANES)
#> Loading required package: NHANES
if (require(mosaic)) {
  nhanes <-
    NHANES %>%
    mutate(
      SmokingStatus = derivedFactor(
        Current = SmokeNow == "Yes",
        Former = SmokeNow == "No",
        Never  = Smoke100 == "No"
      )
    )
  tally( ~SmokingStatus, data = nhanes )
}
#> Loading required package: mosaic
#> Registered S3 method overwritten by 'mosaic':
#>   method                           from   
#>   fortify.SpatialPolygonsDataFrame ggplot2
#> 
#> The 'mosaic' package masks several functions from core packages in order to add 
#> additional features.  The original behavior of these functions should not be affected by this.
#> 
#> Attaching package: 'mosaic'
#> The following objects are masked from 'package:dplyr':
#> 
#>     count, do, tally
#> The following object is masked from 'package:Matrix':
#> 
#>     mean
#> The following object is masked from 'package:ggplot2':
#> 
#>     stat
#> The following objects are masked from 'package:stats':
#> 
#>     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
#>     quantile, sd, t.test, var
#> The following objects are masked from 'package:base':
#> 
#>     max, mean, min, prod, range, sample, sum
#> SmokingStatus
#> Current  Former   Never    <NA> 
#>    1466    1745    4024    2765

# Can't use complete cases; no data survives
# Project <- nhanes[complete.cases(nhanes),]
# describe(Project) FUNCTION NOT FOUND

Project <- nhanes

str(Project[,c("Depressed", "Gender", "Education", "Poverty", "BMI", "Alcohol12PlusYr", "HardDrugs", "SexOrientation")])
#> tibble [10,000 × 8] (S3: tbl_df/tbl/data.frame)
#>  $ Depressed      : Factor w/ 3 levels "None","Several",..: 2 2 2 NA 2 NA NA 1 1 1 ...
#>  $ Gender         : Factor w/ 2 levels "female","male": 2 2 2 2 1 2 2 1 1 1 ...
#>  $ Education      : Factor w/ 5 levels "8th Grade","9 - 11th Grade",..: 3 3 3 NA 4 NA NA 5 5 5 ...
#>  $ Poverty        : num [1:10000] 1.36 1.36 1.36 1.07 1.91 1.84 2.33 5 5 5 ...
#>  $ BMI            : num [1:10000] 32.2 32.2 32.2 15.3 30.6 ...
#>  $ Alcohol12PlusYr: Factor w/ 2 levels "No","Yes": 2 2 2 NA 2 NA NA 2 2 2 ...
#>  $ HardDrugs      : Factor w/ 2 levels "No","Yes": 2 2 2 NA 2 NA NA 1 1 1 ...
#>  $ SexOrientation : Factor w/ 3 levels "Bisexual","Heterosexual",..: 2 2 2 NA 2 NA NA 1 1 1 ...

# Poverty and BMI omitted because they are not factors,
# required for contrasts

Project.Final.glm<-glm(Depressed ~ Gender + 
                       Education + 
                       Alcohol12PlusYr +
                       HardDrugs + 
                       SexOrientation + 
                       SexOrientation:RegularMarij,
                       family=binomial, data=Project)

summary(Project.Final.glm)
#> 
#> Call:
#> glm(formula = Depressed ~ Gender + Education + Alcohol12PlusYr + 
#>     HardDrugs + SexOrientation + SexOrientation:RegularMarij, 
#>     family = binomial, data = Project)
#> 
#> Deviance Residuals: 
#>     Min       1Q   Median       3Q      Max  
#> -1.6224  -0.7313  -0.6041  -0.4591   2.2058  
#> 
#> Coefficients:
#>                                            Estimate Std. Error z value Pr(>|z|)
#> (Intercept)                                -0.33406    0.35592  -0.939 0.347947
#> Gendermale                                 -0.58850    0.07683  -7.660 1.86e-14
#> Education9 - 11th Grade                    -0.17472    0.19871  -0.879 0.379255
#> EducationHigh School                       -0.46345    0.18901  -2.452 0.014209
#> EducationSome College                      -0.66960    0.18467  -3.626 0.000288
#> EducationCollege Grad                      -1.09579    0.18885  -5.803 6.53e-09
#> Alcohol12PlusYrYes                          0.14379    0.10416   1.380 0.167454
#> HardDrugsYes                                0.66841    0.09637   6.936 4.03e-12
#> SexOrientationHeterosexual                 -0.32244    0.30394  -1.061 0.288749
#> SexOrientationHomosexual                    1.05673    0.42962   2.460 0.013907
#> SexOrientationBisexual:RegularMarijYes      0.98927    0.41322   2.394 0.016664
#> SexOrientationHeterosexual:RegularMarijYes  0.07394    0.09397   0.787 0.431388
#> SexOrientationHomosexual:RegularMarijYes   -0.85147    0.48911  -1.741 0.081710
#>                                               
#> (Intercept)                                   
#> Gendermale                                 ***
#> Education9 - 11th Grade                       
#> EducationHigh School                       *  
#> EducationSome College                      ***
#> EducationCollege Grad                      ***
#> Alcohol12PlusYrYes                            
#> HardDrugsYes                               ***
#> SexOrientationHeterosexual                    
#> SexOrientationHomosexual                   *  
#> SexOrientationBisexual:RegularMarijYes     *  
#> SexOrientationHeterosexual:RegularMarijYes    
#> SexOrientationHomosexual:RegularMarijYes   .  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> (Dispersion parameter for binomial family taken to be 1)
#> 
#>     Null deviance: 4867.2  on 4593  degrees of freedom
#> Residual deviance: 4619.2  on 4581  degrees of freedom
#>   (5406 observations deleted due to missingness)
#> AIC: 4645.2
#> 
#> Number of Fisher Scoring iterations: 4
 
# roc is not in namespace
rocplot <- roc(Depressed~fitted(Project.Final.glm), data=Project)
#> Error in roc(Depressed ~ fitted(Project.Final.glm), data = Project): could not find function "roc"

Created on 2020-12-05 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.