5353# ' by = "id"
5454# ' )
5555# ' @export
56- check_group_variation <- function (x , select = NULL , by = NULL ,
57- tolerance = 1e-4 , only_balanced = TRUE ) {
56+ check_group_variation <- function (
57+ x ,
58+ select = NULL ,
59+ by = NULL ,
60+ tolerance = 1e-4 ,
61+ only_balanced = TRUE
62+ ) {
5863 insight :: check_if_installed(" datawizard" , minimum_version = " 0.12.0" )
5964
6065 if (inherits(select , " formula" )) {
@@ -69,7 +74,9 @@ check_group_variation <- function(x, select = NULL, by = NULL,
6974 insight :: format_error(" Please provide the group variable using `by`." )
7075 }
7176 if (! all(by %in% colnames(x ))) {
72- insight :: format_error(" The variable(s) speciefied in `by` were not found in the data." )
77+ insight :: format_error(
78+ " The variable(s) speciefied in `by` were not found in the data."
79+ )
7380 }
7481
7582 # select all, if not given
@@ -78,20 +85,30 @@ check_group_variation <- function(x, select = NULL, by = NULL,
7885 }
7986
8087 # create all combinations that should be checked
81- combinations <- expand.grid(variable = select , group = by , stringsAsFactors = FALSE )
88+ combinations <- expand.grid(
89+ variable = select ,
90+ group = by ,
91+ stringsAsFactors = FALSE
92+ )
8293 combinations <- combinations [combinations $ variable != combinations $ group , ]
8394 combinations $ type <- NA_character_
8495
8596 # initialize lists
8697 for (i in seq_len(nrow(combinations ))) {
87- combinations [i ," type" ] <-
88- .check_nested(x , combinations [i ," group" ], combinations [i ," variable" ],
89- tolerance = tolerance ,
90- only_balanced = only_balanced )
98+ combinations [i , " type" ] <- .check_nested(
99+ x ,
100+ combinations [i , " group" ],
101+ combinations [i , " variable" ],
102+ tolerance = tolerance ,
103+ only_balanced = only_balanced
104+ )
91105 }
92106
93-
94- combinations <- datawizard :: data_relocate(combinations , select = " group" , before = " variable" )
107+ combinations <- datawizard :: data_relocate(
108+ combinations ,
109+ select = " group" ,
110+ before = " variable"
111+ )
95112 class(combinations ) <- c(" check_group_variation" , class(combinations ))
96113 combinations
97114}
@@ -100,11 +117,18 @@ check_group_variation <- function(x, select = NULL, by = NULL,
100117# ' @export
101118print.check_group_variation <- function (x , ... ) {
102119 if (insight :: n_unique(x $ group ) > 1L ) {
103- cat(insight :: export_table(x , caption = c(" Check group variation" , " blue" ), by = " group" ))
120+ cat(insight :: export_table(
121+ x ,
122+ caption = c(" Check group variation" , " blue" ),
123+ by = " group"
124+ ))
104125 } else {
105126 x_new <- x
106127 x_new $ group <- NULL
107- cat(insight :: export_table(x_new , caption = c(sprintf(" Check %s variation" , x $ group [1 ]), " blue" )))
128+ cat(insight :: export_table(
129+ x_new ,
130+ caption = c(sprintf(" Check %s variation" , x $ group [1 ]), " blue" )
131+ ))
108132 }
109133
110134 return (invisible (x ))
@@ -117,9 +141,16 @@ print.check_group_variation <- function(x, ...) {
117141 UseMethod(" .check_nested" , data [[predictor ]])
118142}
119143
144+
120145.check_nested.numeric <- function (data , by , predictor , tolerance = 1e-05 , ... ) {
121146 # demean predictor
122- d <- datawizard :: demean(data , select = predictor , by = by , verbose = FALSE , add_attributes = FALSE )
147+ d <- datawizard :: demean(
148+ data ,
149+ select = predictor ,
150+ by = by ,
151+ verbose = FALSE ,
152+ add_attributes = FALSE
153+ )
123154
124155 # get new names
125156 within_name <- paste0(predictor , " _within" )
@@ -135,30 +166,31 @@ print.check_group_variation <- function(x, ...) {
135166 NULL
136167}
137168
169+
138170.check_nested.default <- function (data , by , predictor , only_balanced = TRUE , ... ) {
139- f <- data [[by ]]
171+ group <- data [[by ]]
140172 variable <- data [[predictor ]]
141173
142- oo <- complete.cases(f , variable )
143- f <- as.factor(f [ oo ])
144- variable <- variable [oo ]
174+ complete <- stats :: complete.cases(group , variable )
175+ group <- as.factor(group [ complete ])
176+ variable <- variable [complete ]
145177
146- n_uniques <- tapply(variable , f , insight :: n_unique )
178+ n_uniques <- tapply(variable , group , insight :: n_unique )
147179 is_between <- all(n_uniques == 1L )
148180 if (is_between ) return (" between" )
149181
150182 if (! insight :: has_single_value(is_between )) return (" both" )
151183
152184 variable_levels <- unique(variable )
153- has_all <- tapply(variable , f , function (v ) all(variable_levels %in% v ))
185+ has_all <- tapply(variable , group , function (v ) all(variable_levels %in% v ))
154186 if (! all(has_all )) return (" both" )
155187
156188 if (only_balanced ) {
157- tab <- table(variable , f )
189+ tab <- table(variable , group )
158190 is_balanced <- all(apply(tab , 2 , insight :: has_single_value ))
159191 if (! is_balanced ) {
160192 return (" both" )
161193 }
162194 }
163195 return (" within" )
164- }
196+ }
0 commit comments