Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Baptiste Nusaibah validation #11

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: grstat
Type: Package
Title: Clinical Research Tools
Version: 0.1.0.9004
Version: 0.1.0.9006
Authors@R: c(
person("Dan", "Chaltiel", role = c("aut", "cre"),
email = "[email protected]",
Expand Down
24 changes: 24 additions & 0 deletions R/validation fonctions AE_table_grade/compair_grade.R
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ce seront des fonctions de testing, elles ne doivent pas aller dans R/
Utilise usethis::use_test_helper()
Tu peux aussi aller voir la doc de testthat: https://cran.r-project.org/web/packages/testthat/vignettes/special-files.html

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

compair_grade <- function(tabR,tabSAS){
DanChaltiel marked this conversation as resolved.
Show resolved Hide resolved

if (nrow(tabR)!=nrow(tabSAS)){stop("Different number of grade levels")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

soit sur une ligne, sans les {},
soit sur 3 lignes
jamais sur 2


if (ncol(tabR)!=ncol(tabSAS)){stop("Different number of arm")}
if (all(dim(tabR)==dim(tabSAS))){
print("Check: same dimension of tables")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

programmation défensive: on ne print pas si tout va bien, on warn s'il y a un problème

df=tabR%>%arrange(grade)%>%full_join(tabSAS,by="grade",suffix = c(".r",".sas"))
indice=which(df[,paste0(tabR%>%select(-grade)%>%colnames(),paste=".r")]!=df[,paste0(tabSAS%>%select(-grade)%>%colnames(),paste=".sas")],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je n'aime vraiment pas les indices, je trouve que c'est à risque d'erreur
Cf mon commentaire sur Teams

arr.ind=TRUE)
indice[,"col"]=indice[,"col"]+1 # parce qu'on avait retiré le grade
}
if (nrow(indice)!=0){
print(indice)
warning(paste0("Comparison result: Warning! Different outputs.\n",
nrow(indice)," mismatching between the two tables. Above, the indices."))

}else{
warning("Comparison result: same outputs")
}

}
23 changes: 23 additions & 0 deletions R/validation fonctions AE_table_grade/group_grades_zeroNA.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#fonction grouping figures of missing grades and grades 0 for R function ae_table_grade
group_grades_zeroNA <- function(data, round = 0){

#suming N of missing grades and grades 0
data <- data %>%
mutate(grade = replace_na(grade, 0)) %>%
group_by(grade) %>%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutate(.by=grade), plus concis et ne nécessite pas de ungroup()

mutate(across(starts_with("N"), ~sum(., na.rm = T))) %>%
distinct(grade, .keep_all = T) %>%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On ne peut pas remplacer mutate+distinct par summarise ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je crois que c'est parce que je n'arrivais pas à keep toutes les variables dans la base avec summarize(.by=) quand il y avait plusieurs bras

ungroup()

#recalculating pct
ngroups <- (ncol(data) - 1) / 2
for(i in 1:ngroups){
npatients <- sum(data[, paste0("N", i)])
data[data$grade == 0, paste0("pct", i)] <- round(data[data$grade == 0, paste0("N", i)] * 100 / npatients, round)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je vais avoir besoin de lancer le code pour trouver comment appliquer purrr


data <- data %>% arrange(grade)

return(data)

}
20 changes: 20 additions & 0 deletions R/validation fonctions AE_table_grade/separate_n_pct.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#fonction separating each column N(pct) into 2 columns N and pct
separate_n_pct <- function(data){

#separation of 1 character column into 2 character columns
data <- colnames(data) %>%
imap(
~data %>% select(all_of(.x)) %>%
separate(.x, into = c(paste0("N", .y), paste0("pct", .y)), sep = "\\(")
) %>%
bind_cols()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://tidyr.tidyverse.org/reference/separate_wider_delim.html
On doit pouvoir s'en sortir sans boucle avec separate_wider_regex(), pas évident mais l'exemple aide beaucoup.


#extraction of figures into numeric columns
data <- data %>%
mutate(
across(everything(), ~as.numeric(str_extract(.x, "\\d+\\.?\\d*")))
)

return(data)

}
31 changes: 31 additions & 0 deletions R/validation fonctions AE_table_grade/validate_ae_table_grade.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#fonction comparing R and SAS outputs
validate_ae_table_grade <- function(R_output,
SAS_output,
round = 0 #rounding of recalculated percentages in group_grades_zeroNA()
){

#formatting grade from character to numeric
R_output <- R_output %>%
mutate(grade = ifelse(variable == "No declared AE", 0, as.numeric(str_extract(variable, "\\d"))))
SAS_output <- SAS_output %>%
mutate(grade = as.numeric(str_extract(max_aegr1, "\\d")))

#separating each column N(pct) into 2 columns N and pct
R_separated <- R_output %>% select(grade) %>%
bind_cols(
separate_n_pct(R_output %>% select(-c(.id, label, variable, grade)))
)
SAS_separated <- SAS_output %>% select(grade) %>%
bind_cols(
separate_n_pct(SAS_output %>% select(-c(max_aegr1, grade)))
)

#missing figure with macro SAS <=> 0 with R package
SAS_separated <- SAS_separated %>%
replace(is.na(.), 0)

#grouping figures of missing grades and grades 0 for R function
R_separated <- group_grades_zeroNA(R_separated, round = round)

return(list(R = R_separated, SAS = SAS_separated))
}
Loading