# Sensitivity/specificity of a test

Hi,
I am trying to extract sensitivity, specificity, TP, TN, PPV, NPV. I have a database of around 500 patients and I am looking at a test and how well it can predict the outcome in each age group (I have created 4 age groups).

I used the below but unfortunately I can't get the results for each age group. I have put group_by (age_group).
This is the code:

library(finalfit)
library(yardstick)
library(tidyverse)
prognos <- function(.data, estimate, truth){
.estimate = sym(estimate)
.truth = sym(truth)
.data %>%
drop_na(!! .estimate, !! .truth) %>%
summarise(
N = n(),
TP = sum(!! .truth == 1 & !! .estimate == 1),
TN = sum(!! .truth == 0 & !! .estimate == 0),
FP = sum(!! .truth == 0 & !! .estimate == 1),
FN = sum(!! .truth == 1 & !! .estimate == 0),
Sens = sens(., !! .truth, !! .estimate).estimate, Spec = spec(., !! .truth, !! .estimate).estimate,
PPV = ppv(., !! .truth, !! .estimate).estimate, NPV = npv(., !! .truth, !! .estimate).estimate,
above = paste0(TP + FP, " (", ((100*(TP + FP))/N) %>% round_tidy(1), ")"),
below = paste0(TN + FN, " (", ((100*(TN + FN))/N) %>% round_tidy(1), ")")
) %>%
mutate_at(vars("Sens", "PPV", "NPV"), ~ prod(., 100)) %>%
mutate(across(c(everything(), -above, -below), round, digits = 1))
}

NAFLD_new %>%
prognos(estimate = "estimate", truth = "cirrhosis")

This is the result:
prognos(estimate = "estimate", truth = "cirrhosis")
`summarise()` has grouped output by 'age_group'. You can override using the `.groups` argument.

# Groups: age_group [4]

age_group N TP TN FP FN Sens Spec PPV NPV above below

1 40-49 61 11 31 4 15 26.1 12.6 30.8 15.2 15 (24.6) 46 (75.4)
2 40-49 61 11 31 4 15 26.1 12.6 30.8 15.2 15 (24.6) 46 (75.4)
3 40-49 61 11 31 4 15 26.1 12.6 30.8 15.2 15 (24.6) 46 (75.4)
4 40-49 61 11 31 4 15 26.1 12.6 30.8 15.2 15 (24.6) 46 (75.4)
5 50-59 111 33 36 27 15 26.1 12.6 30.8 15.2 60 (54.1) 51 (45.9)
6 50-59 111 33 36 27 15 26.1 12.6 30.8 15.2 60 (54.1) 51 (45.9)
7 50-59 111 33 36 27 15 26.1 12.6 30.8 15.2 60 (54.1) 51 (45.9)
8 50-59 111 33 36 27 15 26.1 12.6 30.8 15.2 60 (54.1) 51 (45.9)
9 <40 52 6 39 3 4 26.1 12.6 30.8 15.2 9 (17.3) 43 (82.7)
10 <40 52 6 39 3 4 26.1 12.6 30.8 15.2 9 (17.3) 43 (82.7)
11 <40 52 6 39 3 4 26.1 12.6 30.8 15.2 9 (17.3) 43 (82.7)
12 <40 52 6 39 3 4 26.1 12.6 30.8 15.2 9 (17.3) 43 (82.7)
13 >60 81 26 25 20 10 26.1 12.6 30.8 15.2 46 (56.8) 35 (43.2)
14 >60 81 26 25 20 10 26.1 12.6 30.8 15.2 46 (56.8) 35 (43.2)
15 >60 81 26 25 20 10 26.1 12.6 30.8 15.2 46 (56.8) 35 (43.2)
16 >60 81 26 25 20 10 26.1 12.6 30.8 15.2 46 (56.8) 35 (43.2)
Warning message:
Returning more (or less) than 1 row per `summarise()` group was deprecated in dplyr 1.1.0.
Please use `reframe()` instead.
When switching from `summarise()` to `reframe()`, remember that `reframe()` always returns an ungrouped data frame and adjust
accordingly.

I am not sure why it gives me each age group 4 times and why sens/spec is the same everywhere.

Does anyone can suggest a solution this problem please?

Thanks you.

This topic was automatically closed 42 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.