[R] Sensitivity and Specificity

Jin Li j|n||68 @end|ng |rom gm@||@com
Tue Oct 25 06:42:42 CEST 2022


Hi Greg,

This can be done by:
spm::pred.acc(testing$case,  predict_testing)

It will return both sensitivity and specificity, along with a few other
commonly used measures.

Hope this helps,
Jin

On Tue, Oct 25, 2022 at 6:01 AM Rui Barradas <ruipbarradas using sapo.pt> wrote:

> Às 16:50 de 24/10/2022, greg holly escreveu:
> > Hi Michael,
> >
> > I appreciate your writing. Here are what I have after;
> >
> >> predict_testing <- ifelse(predict > 0.5,1,0)
> >>
> >> head(predict)
> >           1          2          3          5          7          8
> > 0.29006984 0.28370507 0.10761993 0.02204224 0.12873872 0.08127920
> >>
> >> # Sensitivity and Specificity
> >>
> >>
> >
> sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
> > Error in predict_testing[2, 2] : incorrect number of dimensions
> >> sensitivity
> > function (data, ...)
> > {
> >      UseMethod("sensitivity")
> > }
> > <bytecode: 0x000002082a2f01d8>
> > <environment: namespace:caret>
> >>
> >>
> >
> specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
> > Error in predict_testing[1, 1] : incorrect number of dimensions
> >> specificity
> > function (data, ...)
> > {
> >      UseMethod("specificity")
> > }
> > <bytecode: 0x000002082a2fa600>
> > <environment: namespace:caret>
> >
> > On Mon, Oct 24, 2022 at 10:45 AM Michael Dewey <lists using dewey.myzen.co.uk>
> > wrote:
> >
> >> Rather hard to know without seeing what output you expected and what
> >> error message you got if any but did you mean to summarise your variable
> >> predict before doing anything with it?
> >>
> >> Michael
> >>
> >> On 24/10/2022 16:17, greg holly wrote:
> >>> Hi all R-Help ,
> >>>
> >>> After partitioning my data to testing and training (please see below),
> I
> >>> need to estimate the Sensitivity and Specificity. I failed. It would be
> >>> appropriate to get your help.
> >>>
> >>> Best regards,
> >>> Greg
> >>>
> >>>
> >>> inTrain <- createDataPartition(y=data$case,
> >>>                                  p=0.7,
> >>>                                  list=FALSE)
> >>> training <- data[ inTrain,]
> >>> testing  <- data[-inTrain,]
> >>>
> >>> attach(training)
> >>> #model training and prediction
> >>> data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
> >>> training, family = binomial(link="logit"))
> >>>
> >>> predict <- predict(data_training, data_predict = testing, type =
> >> "response")
> >>>
> >>> predict_testing <- ifelse(predict > 0.5,1,0)
> >>>
> >>> # Sensitivity and Specificity
> >>>
> >>>
> >>
>  sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
> >>>    sensitivity
> >>>
> >>>
> >>
>  specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
> >>>    specificity
> >>>
> >>>        [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>> 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.
> >>>
> >>
> >> --
> >> Michael
> >> http://www.dewey.myzen.co.uk/home.html
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> Hello,
>
> Instead of computing by hand, why not use package caret?
>
>
> tbl <- table(predict_testing, testing$case)
> caret::sensitivity(tbl)
> caret::specificity(tbl)
>
>
> Hope this helps,
>
> Rui Barradas
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>


-- 
Jin
------------------------------------------
Jin Li, PhD
Founder, Data2action, Australia
https://www.researchgate.net/profile/Jin_Li32
https://scholar.google.com/citations?user=Jeot53EAAAAJ&hl=en

	[[alternative HTML version deleted]]



More information about the R-help mailing list