6464# ' abline(lm(V2 ~ V1, data = adjusted_icpt), col = "red")
6565# '
6666# ' @export
67- adjust <- function (data ,
68- effect = NULL ,
69- select = is.numeric ,
70- exclude = NULL ,
71- multilevel = FALSE ,
72- additive = FALSE ,
73- bayesian = FALSE ,
74- keep_intercept = FALSE ,
75- ignore_case = FALSE ,
76- regex = FALSE ,
77- verbose = FALSE ) {
67+ adjust <- function (
68+ data ,
69+ effect = NULL ,
70+ select = is.numeric ,
71+ exclude = NULL ,
72+ multilevel = FALSE ,
73+ additive = FALSE ,
74+ bayesian = FALSE ,
75+ keep_intercept = FALSE ,
76+ ignore_case = FALSE ,
77+ regex = FALSE ,
78+ verbose = FALSE
79+ ) {
7880 # make sure column names are syntactically valid
7981 .check_dataframe_names(data , action = " error" )
8082
@@ -92,7 +94,8 @@ adjust <- function(data,
9294 select <- is.numeric
9395 }
9496
95- select <- .select_nse(select ,
97+ select <- .select_nse(
98+ select ,
9699 data ,
97100 exclude ,
98101 ignore_case ,
@@ -105,9 +108,15 @@ adjust <- function(data,
105108 facs <- names(data [effect ][! vapply(data [effect ], is.numeric , logical (1L ))])
106109 if (length(facs ) > = 1 && multilevel ) {
107110 if (additive ) {
108- formula_random <- stats :: as.formula(paste(" ~" , paste(paste0(" (1|" , facs , " )" ), collapse = " + " )))
111+ formula_random <- stats :: as.formula(paste(
112+ " ~" ,
113+ paste(paste0(" (1|" , facs , " )" ), collapse = " + " )
114+ ))
109115 } else {
110- formula_random <- paste(" +" , paste(paste0(" (1|" , facs , " )" ), collapse = " + " ))
116+ formula_random <- paste(
117+ " +" ,
118+ paste(paste0(" (1|" , facs , " )" ), collapse = " + " )
119+ )
111120 }
112121 effect <- effect [! effect %in% facs ]
113122 }
@@ -117,8 +126,16 @@ adjust <- function(data,
117126 for (var in select ) {
118127 predictors <- effect [effect != var ]
119128 if (additive ) {
120- predictors_num <- names(data [predictors ][vapply(data [predictors ], is.numeric , logical (1L ))])
121- predictors [predictors == predictors_num ] <- paste0(" s(" , predictors_num , " )" )
129+ predictors_num <- names(data [predictors ][vapply(
130+ data [predictors ],
131+ is.numeric ,
132+ logical (1L )
133+ )])
134+ predictors [predictors == predictors_num ] <- paste0(
135+ " s(" ,
136+ predictors_num ,
137+ " )"
138+ )
122139 }
123140 formula_predictors <- paste(c(" 1" , predictors ), collapse = " + " )
124141 model_formula <- paste(var , " ~" , formula_predictors )
@@ -134,7 +151,9 @@ adjust <- function(data,
134151 )
135152 out [var ] <- x
136153 }
137- out [names(data )[! names(data ) %in% names(out )]] <- data [names(data )[! names(data ) %in% names(out )]]
154+ out [names(data )[! names(data ) %in% names(out )]] <- data [names(data )[
155+ ! names(data ) %in% names(out )
156+ ]]
138157 out [names(data )]
139158}
140159
@@ -144,31 +163,46 @@ data_adjust <- adjust
144163
145164
146165# ' @keywords internal
147- .model_adjust_for <- function (data ,
148- model_formula ,
149- multilevel = FALSE ,
150- additive = FALSE ,
151- bayesian = FALSE ,
152- formula_random = NULL ,
153- keep_intercept = FALSE ) {
166+ .model_adjust_for <- function (
167+ data ,
168+ model_formula ,
169+ multilevel = FALSE ,
170+ additive = FALSE ,
171+ bayesian = FALSE ,
172+ formula_random = NULL ,
173+ keep_intercept = FALSE
174+ ) {
154175 # Additive -----------------------
155176 if (additive ) {
156177 # Bayesian
157178 if (bayesian ) {
158179 insight :: check_if_installed(" rstanarm" )
159- model <- rstanarm :: stan_gamm4(stats :: as.formula(model_formula ), random = formula_random , data = data , refresh = 0 )
180+ model <- rstanarm :: stan_gamm4(
181+ stats :: as.formula(model_formula ),
182+ random = formula_random ,
183+ data = data ,
184+ refresh = 0
185+ )
160186 # Frequentist
161187 } else {
162188 insight :: check_if_installed(" gamm4" )
163- model <- gamm4 :: gamm4(stats :: as.formula(model_formula ), random = formula_random , data = data )
189+ model <- gamm4 :: gamm4(
190+ stats :: as.formula(model_formula ),
191+ random = formula_random ,
192+ data = data
193+ )
164194 }
165195
166196 # Linear -------------------------
167197 } else if (bayesian ) {
168198 # Bayesian
169199 insight :: check_if_installed(" rstanarm" )
170200 if (multilevel ) {
171- model <- rstanarm :: stan_lmer(paste(model_formula , formula_random ), data = data , refresh = 0 )
201+ model <- rstanarm :: stan_lmer(
202+ paste(model_formula , formula_random ),
203+ data = data ,
204+ refresh = 0
205+ )
172206 } else {
173207 model <- rstanarm :: stan_glm(model_formula , data = data , refresh = 0 )
174208 }
@@ -185,8 +219,12 @@ data_adjust <- adjust
185219 # Re-add intercept if need be
186220 if (keep_intercept ) {
187221 intercept <- insight :: get_intercept(model )
188- if (length(intercept ) > 1 ) intercept <- stats :: median(intercept ) # For bayesian model
189- if (is.na(intercept )) intercept <- 0
222+ if (length(intercept ) > 1 ) {
223+ intercept <- stats :: median(intercept )
224+ } # For bayesian model
225+ if (is.na(intercept )) {
226+ intercept <- 0
227+ }
190228 adjusted <- adjusted + intercept
191229 }
192230
0 commit comments