1

I'm trying to build a ROC curve with data from a survey object from surveyr. But the pROC function cannot coerce a survey object back to a data frame for the analyses and it doen't work with the survey object

if (!require("pacman")) install.packages("pacman")

pacman::p_load(tidyverse, haven, survey, sjPlot, pROC, surveydata)

roccurve1 <- roc(apoio_ag ~fitted(Model1), data = psurvey)
roccurve2 <- roc(apoio_ag ~fitted(Model2), data = psurvey)
roccurve3 <- roc(apoio_ag ~fitted(Model3), data = psurvey)

Erro message Error in as.data.frame.default(data, optional = TRUE) : cannot coerce class ‘"svyrep.design"’ to a data.frame

Those are the packages and the code that I'm currently using. Anyone has an idea on how to solve that?

  • It would help if you could give a reproducible example here... see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Calimo Jan 14 '22 at 14:29
  • would this help? https://stackoverflow.com/a/67310885/1759499 – Anthony Damico Jan 14 '22 at 16:29
  • I cannot share the data, because it came from a private company. But the psurvey object is a post-stratified data. And the model is a glm from a quasi-binomial distribution, create with the command svyglm() – João Camargos Jan 15 '22 at 12:22
  • @AnthonyDamico, it doesn't work beacuse the formula is doesn't apply for a survey object – João Camargos Jan 15 '22 at 12:30

1 Answers1

0

The pROC::roc function doesn't accept sampling weights, so it's not just a matter of data formats -- you can extract a data frame from a survey object with model.frame, but roc still won't be doing a weighted analysis.

There's a WeightedROC package that fits weighted ROC curves. You could do something like (not tested because there isn't a reproducible example)

WeightedROC(fitted(Model3), Model3$y, weights(psurvey))
Thomas Lumley
  • 1,893
  • 5
  • 8