9
9
# ' @param id_var name of id variable in df
10
10
# ' @param age_var name of age variable in df
11
11
# ' @param conns connection object for DataSHIELD backends
12
- # '
12
+ # '
13
13
# ' @return a tibble containing the following columns:
14
14
# '
15
15
# ' min_age: 5th percentile of age
23
23
# ' @export
24
24
dh.getRmStats <- function (df = NULL , outcome = NULL , id_var = NULL , age_var = NULL , conns = NULL ) {
25
25
variable <- perc_5 <- perc_95 <- cohort <- min_age <- max_age <- valid_n <- NULL
26
-
26
+
27
27
if (is.null(df )) {
28
28
stop(" Please provide the name of a datashield dataframe" )
29
29
}
@@ -47,7 +47,7 @@ dh.getRmStats <- function(df = NULL, outcome = NULL, id_var = NULL, age_var = NU
47
47
# # ---- First get overall stats for some of the easy ones -------------------------------------------
48
48
stats <- dh.getStats(
49
49
df = df ,
50
- vars = c(outcome , age_var ),
50
+ vars = c(outcome , age_var ),
51
51
conns = conns
52
52
)
53
53
@@ -77,7 +77,7 @@ dh.getRmStats <- function(df = NULL, outcome = NULL, id_var = NULL, age_var = NU
77
77
X.name = " data$weight" ,
78
78
INDEX.names = " id_fact" ,
79
79
FUN.name = " N" ,
80
- newobj = " id_summary" ,
80
+ newobj = " id_summary" ,
81
81
datasources = conns
82
82
)
83
83
@@ -87,49 +87,49 @@ dh.getRmStats <- function(df = NULL, outcome = NULL, id_var = NULL, age_var = NU
87
87
mutate(combined = rowSums(. )) %> %
88
88
pivot_longer(
89
89
cols = everything(),
90
- names_to = " cohort" ,
90
+ names_to = " cohort" ,
91
91
values_to = " n_participants"
92
92
)
93
-
94
- # # ---- Median number of weight measurements per child ----------------------------------------
95
93
96
- # We can use the ds.quantileMean function with the object we created above to get the
94
+ # # ---- Median number of weight measurements per child ----------------------------------------
95
+
96
+ # We can use the ds.quantileMean function with the object we created above to get the
97
97
# median number of measurements per child.
98
98
99
99
ds.asNumeric(" id_summary$N" , " id_summary_num" , datasources = conns )
100
100
101
- quants <- DSI :: datashield.aggregate(conns , as.symbol(" quantileMeanDS(id_summary_num)" ))
101
+ quants <- DSI :: datashield.aggregate(conns , as.symbol(" quantileMeanDS(id_summary_num)" ))
102
102
103
103
weight_med_iqr <- quants %> %
104
- bind_rows(.id = " cohort" ) %> %
105
- select(cohort , " 5%" , " 50%" , " 95%" ) %> %
106
- rename(n_meas_med = " 50%" , n_meas_5 = " 5%" , n_meas_95 = " 95%" )
104
+ bind_rows(.id = " cohort" ) %> %
105
+ select(cohort , " 5%" , " 50%" , " 95%" ) %> %
106
+ rename(n_meas_med = " 50%" , n_meas_5 = " 5%" , n_meas_95 = " 95%" )
107
107
108
108
# # Get the combined version using weighted sum
109
- lengths <- DSI :: datashield.aggregate(conns , call(' lengthDS' , " id_summary_num" ))
109
+ lengths <- DSI :: datashield.aggregate(conns , call(" lengthDS" , " id_summary_num" ))
110
110
numNAs <- DSI :: datashield.aggregate(conns , " numNaDS(id_summary_num)" )
111
-
112
- valid_n <- list (lengths , numNAs ) %> % pmap(~ .x - .y )
113
111
114
- weights <- unlist( valid_n ) / sum(unlist( valid_n ) )
112
+ valid_n <- list ( lengths , numNAs ) % > % pmap( ~ .x - .y )
115
113
116
- weighted_quant <- list ( quants , weights ) % > % pmap( ~ .x * .y )
114
+ weights <- unlist( valid_n ) / sum(unlist( valid_n ))
117
115
118
- sum_quant <- weighted_quant %> %
119
- pmap(function (... ){
120
- sum(c(... ))
121
- }) %> %
122
- bind_rows %> %
123
- rename(n_meas_med = " 50%" , n_meas_5 = " 5%" , n_meas_95 = " 95%" ) %> %
124
- mutate(cohort = " combined" ) %> %
125
- select(cohort , n_meas_med , n_meas_5 , n_meas_95 )
116
+ weighted_quant <- list (quants , weights ) %> % pmap(~ .x * .y )
117
+
118
+ sum_quant <- weighted_quant %> %
119
+ pmap(function (... ) {
120
+ sum(c(... ))
121
+ }) %> %
122
+ bind_rows() %> %
123
+ rename(n_meas_med = " 50%" , n_meas_5 = " 5%" , n_meas_95 = " 95%" ) %> %
124
+ mutate(cohort = " combined" ) %> %
125
+ select(cohort , n_meas_med , n_meas_5 , n_meas_95 )
126
126
127
- quant_out <- bind_rows(weight_med_iqr , sum_quant )
127
+ quant_out <- bind_rows(weight_med_iqr , sum_quant )
128
128
129
- # # ---- Create final output -------------------------------------------------------------------
130
- out <- left_join(age_ranges , outcome_n , by = " cohort" ) %> %
131
- left_join(. , n_subjects , by = " cohort" ) %> %
132
- left_join(. , quant_out , by = " cohort" )
129
+ # # ---- Create final output -------------------------------------------------------------------
130
+ out <- left_join(age_ranges , outcome_n , by = " cohort" ) %> %
131
+ left_join(. , n_subjects , by = " cohort" ) %> %
132
+ left_join(. , quant_out , by = " cohort" )
133
133
134
- return (out )
134
+ return (out )
135
135
}
0 commit comments