[R] How to extract auc, specificity and sensitivity

Berend Hasselman bhh at xs4all.nl
Thu Oct 25 22:05:00 CEST 2012



Your code is still not runnable.
It gives the error message

Error in fitglm(0.05, 1) : could not find function "prediction"

Berend

On 25-10-2012, at 21:55, Adel Powell wrote:

> I think I have corrected it. Can you tell me are my spec and sens values correct
> n <- 1000; # Sample size
>  
> fitglm <- function(sigma,tau){
>     x <- rnorm(n,0,sigma)
>     intercept <- 0
>     beta <- 0
>     ystar <- intercept+beta*x
>     z <- rbinom(n,1,plogis(ystar))
>     xerr <- x + rnorm(n,0,tau)  
>     model<-glm(z ~ xerr, family=binomial(logit))
>     int<-coef(model)[1]
>     slope<-coef(model)[2]
>     pred<-predict(model)
> 
>     result<-ifelse(pred>.5,1,0)  
> 
>     accuracy<-length(which(result==z))/length(z)
>     accuracy
>     
>     rocpreds<-prediction(result,z)
>     auc<-performance(rocpreds,"auc")@y.values
>     fp<-performance(rocpreds,"sens")
>     sentiv<-slot(fp,"y.values")[[1]]
>     sentiv<-sentiv[2]
>     sentiv
>     fp2<-performance(rocpreds,"spec")
>     specs<-slot(fp2,"y.values")[[1]]
>     specs<-specs[2]
>     specs
>     output<-c(int,slope,.5,accuracy,auc,sentiv,specs)
>     names(output)<-c("Intercept","Slope","CutPoint","Accuracy","AUC","Sentivity","Specificity")
>     return(output)
> 
> }
> 
> y<-fitglm(.05,1)
> y
> 
> 
> On Thu, Oct 25, 2012 at 3:43 PM, Berend Hasselman <bhh at xs4all.nl> wrote:
> 
> On 25-10-2012, at 21:28, Adel Powell wrote:
> 
> > I am running my code in a loop and it does not work but when I run it
> > outside the loop I get the values I want.
> >
> > n <- 1000; # Sample size
> >
> > fitglm <- function(sigma,tau){
> >    x <- rnorm(n,0,sigma)
> >    intercept <- 0
> >    beta <- 0
> >    ystar <- intercept+beta*x
> >    z <- rbinom(n,1,plogis(ystar))
> >    xerr <- x + rnorm(n,0,tau)
> >    model<-glm(z ~ xerr, family=binomial(logit))
> >    int<-coef(model)[1]
> >    slope<-coef(model)[2]
> >    pred<-predict(model)
> >
> >    result<-ifelse(pred>.5,1,0)
> >
> >    accuracy<-length(which(result==z))/length(z)
> >    accuracy
> >
> >    rocpreds<-prediction(result,z)
> >    auc<-performance(rocpreds,"auc")@y.values
> >    sentiv<-performance(rocpreds,"sens")@y.values
> >    sentiv<-slot(fp,"y.values")[[1]]
> >    sentiv<-sentiv[2]
> >    sentiv
> >    specs<-performance(rocpreds,"spec")@y.values
> >    specs<-slot(fp2,"y.values")[[1]]
> >    specs<-specs[2]
> >    specs
> >    output<-c(int,slope,.5,accuracy,auc,sentiv,specs)
> >
> > names(output)<-c("Intercept","Slope","CutPoint","Accuracy","AUC","Sentivity",Specificity")
> 
> A missing " before Specificity?
> 
> >    return(output)
> >
> > }
> >
> > y<-fitglm(.05,1)
> > y
> >
> 
> Running this after correction of the missing "  one gets en error
> 
> Error in fitglm(0.05, 1) : could not find function "prediction"
> 
> How are you using a loop?
> Your example is not reproducible.
> 
> Berend
> 
> 
> >
> > The code runs without the sentiv and specs but when I remove the loop i can
> > get the sensitivity and spec. values ???
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> 
> 




More information about the R-help mailing list