|
38 | 38 | #' @importFrom stats .checkMFClasses .getXlevels delete.response |
39 | 39 | #' @importFrom stats model.offset model.weights na.omit na.pass |
40 | 40 | .convert_form_to_xy_fit <- function(formula, |
41 | | - data, |
42 | | - ..., |
43 | | - na.action = na.omit, |
44 | | - indicators = "traditional", |
45 | | - composition = "data.frame", |
46 | | - remove_intercept = TRUE) { |
47 | | - |
48 | | - if (!(composition %in% c("data.frame", "matrix"))) |
| 41 | + data, |
| 42 | + ..., |
| 43 | + na.action = na.omit, |
| 44 | + indicators = "traditional", |
| 45 | + composition = "data.frame", |
| 46 | + remove_intercept = TRUE) { |
| 47 | + if (!(composition %in% c("data.frame", "matrix"))) { |
49 | 48 | rlang::abort("`composition` should be either 'data.frame' or 'matrix'.") |
| 49 | + } |
50 | 50 |
|
51 | 51 | ## Assemble model.frame call from call arguments |
52 | 52 | mf_call <- quote(model.frame(formula, data)) |
53 | 53 | mf_call$na.action <- match.call()$na.action # TODO this should work better |
54 | 54 | dots <- quos(...) |
55 | 55 | check_form_dots(dots) |
56 | | - for(i in seq_along(dots)) |
57 | | - mf_call[[ names(dots)[i] ]] <- get_expr(dots[[i]]) |
| 56 | + for (i in seq_along(dots)) { |
| 57 | + mf_call[[names(dots)[i]]] <- get_expr(dots[[i]]) |
| 58 | + } |
58 | 59 |
|
59 | 60 | # setup contrasts |
60 | | - if (any(names(dots) == "contrasts")) |
| 61 | + if (any(names(dots) == "contrasts")) { |
61 | 62 | contrasts <- eval_tidy(dots[["contrasts"]]) |
62 | | - else |
| 63 | + } else { |
63 | 64 | contrasts <- NULL |
| 65 | + } |
64 | 66 |
|
65 | 67 | # For new data, save the expression to create offsets (if any) |
66 | | - if (any(names(dots) == "offset")) |
| 68 | + if (any(names(dots) == "offset")) { |
67 | 69 | offset_expr <- get_expr(dots[["offset"]]) |
68 | | - else |
| 70 | + } else { |
69 | 71 | offset_expr <- NULL |
| 72 | + } |
70 | 73 |
|
71 | 74 | mod_frame <- eval_tidy(mf_call) |
72 | 75 | mod_terms <- attr(mod_frame, "terms") |
|
78 | 81 | y <- model.response(mod_frame, type = "any") |
79 | 82 |
|
80 | 83 | # if y is a numeric vector, model.response() added names |
81 | | - if(is.atomic(y)) { |
| 84 | + if (is.atomic(y)) { |
82 | 85 | names(y) <- NULL |
83 | 86 | } |
84 | 87 |
|
85 | 88 | w <- as.vector(model.weights(mod_frame)) |
86 | | - if (!is.null(w) && !is.numeric(w)) |
| 89 | + if (!is.null(w) && !is.numeric(w)) { |
87 | 90 | rlang::abort("`weights` must be a numeric vector") |
| 91 | + } |
88 | 92 |
|
89 | 93 | offset <- as.vector(model.offset(mod_frame)) |
90 | 94 | if (!is.null(offset)) { |
91 | | - if (length(offset) != nrow(mod_frame)) |
| 95 | + if (length(offset) != nrow(mod_frame)) { |
92 | 96 | rlang::abort( |
93 | 97 | glue::glue("The offset data should have {nrow(mod_frame)} elements.") |
94 | | - ) |
| 98 | + ) |
| 99 | + } |
95 | 100 | } |
96 | 101 |
|
97 | 102 | if (indicators != "none") { |
|
103 | 108 | options(contrasts = new_contr) |
104 | 109 | } |
105 | 110 | x <- model.matrix(mod_terms, mod_frame, contrasts) |
106 | | - |
107 | 111 | } else { |
108 | 112 | # this still ignores -vars in formula |
109 | 113 | x <- model.frame(mod_terms, data) |
110 | 114 | y_cols <- attr(mod_terms, "response") |
111 | | - if (length(y_cols) > 0) |
112 | | - x <- x[,-y_cols, drop = FALSE] |
| 115 | + if (length(y_cols) > 0) { |
| 116 | + x <- x[, -y_cols, drop = FALSE] |
| 117 | + } |
113 | 118 | } |
114 | 119 |
|
115 | 120 | if (remove_intercept) { |
|
124 | 129 | ) |
125 | 130 |
|
126 | 131 | if (composition == "data.frame") { |
127 | | - if (is.matrix(y)) |
| 132 | + if (is.matrix(y)) { |
128 | 133 | y <- as.data.frame(y) |
| 134 | + } |
129 | 135 | res <- |
130 | 136 | list( |
131 | 137 | x = as.data.frame(x), |
|
140 | 146 | } else { |
141 | 147 | # Since a matrix is requested, try to convert y but check |
142 | 148 | # to see if it is possible |
143 | | - if (will_make_matrix(y)) |
| 149 | + if (will_make_matrix(y)) { |
144 | 150 | y <- as.matrix(y) |
| 151 | + } |
145 | 152 | res <- |
146 | 153 | list( |
147 | 154 | x = x, |
|
164 | 171 | #' @keywords internal |
165 | 172 | #' @export |
166 | 173 | .convert_form_to_xy_new <- function(object, |
167 | | - new_data, |
168 | | - na.action = na.pass, |
169 | | - composition = "data.frame") { |
170 | | - |
171 | | - if (!(composition %in% c("data.frame", "matrix"))) |
| 174 | + new_data, |
| 175 | + na.action = na.pass, |
| 176 | + composition = "data.frame") { |
| 177 | + if (!(composition %in% c("data.frame", "matrix"))) { |
172 | 178 | rlang::abort("`composition` should be either 'data.frame' or 'matrix'.") |
| 179 | + } |
173 | 180 |
|
174 | 181 | mod_terms <- object$terms |
175 | 182 | mod_terms <- delete.response(mod_terms) |
|
183 | 190 | # If offset was done at least once in-line |
184 | 191 | if (!is.null(offset_cols)) { |
185 | 192 | offset <- rep(0, nrow(new_data)) |
186 | | - for (i in offset_cols) |
| 193 | + for (i in offset_cols) { |
187 | 194 | offset <- offset + |
188 | | - eval_tidy(attr(mod_terms, "variables")[[i + 1]], |
189 | | - new_data) # use na.action here and below? |
190 | | - } else offset <- NULL |
| 195 | + eval_tidy( |
| 196 | + attr(mod_terms, "variables")[[i + 1]], |
| 197 | + new_data |
| 198 | + ) # use na.action here and below? |
| 199 | + } |
| 200 | + } else { |
| 201 | + offset <- NULL |
| 202 | + } |
191 | 203 |
|
192 | 204 | if (!is.null(object$offset_expr)) { |
193 | | - if (is.null(offset)) |
| 205 | + if (is.null(offset)) { |
194 | 206 | offset <- rep(0, nrow(new_data)) |
| 207 | + } |
195 | 208 | offset <- offset + eval_tidy(object$offset_expr, new_data) |
196 | 209 | } |
197 | 210 |
|
198 | 211 | new_data <- |
199 | | - model.frame(mod_terms, |
200 | | - new_data, |
201 | | - na.action = na.action, |
202 | | - xlev = object$xlevels) |
| 212 | + model.frame( |
| 213 | + mod_terms, |
| 214 | + new_data, |
| 215 | + na.action = na.action, |
| 216 | + xlev = object$xlevels |
| 217 | + ) |
203 | 218 |
|
204 | 219 | cl <- attr(mod_terms, "dataClasses") |
205 | | - if (!is.null(cl)) |
| 220 | + if (!is.null(cl)) { |
206 | 221 | .checkMFClasses(cl, new_data) |
| 222 | + } |
207 | 223 |
|
208 | | - if(object$options$indicators != "none") { |
| 224 | + if (object$options$indicators != "none") { |
209 | 225 | if (object$options$indicators == "one_hot") { |
210 | 226 | old_contr <- options("contrasts")$contrasts |
211 | 227 | on.exit(options(contrasts = old_contr)) |
|
217 | 233 | model.matrix(mod_terms, new_data, contrasts.arg = object$contrasts) |
218 | 234 | } |
219 | 235 |
|
220 | | - if(object$options$remove_intercept) { |
| 236 | + if (object$options$remove_intercept) { |
221 | 237 | new_data <- new_data[, colnames(new_data) != "(Intercept)", drop = FALSE] |
222 | 238 | } |
223 | 239 |
|
224 | | - if (composition == "data.frame") |
| 240 | + if (composition == "data.frame") { |
225 | 241 | new_data <- as.data.frame(new_data) |
226 | | - else { |
227 | | - if (will_make_matrix(new_data)) |
| 242 | + } else { |
| 243 | + if (will_make_matrix(new_data)) { |
228 | 244 | new_data <- as.matrix(new_data) |
| 245 | + } |
229 | 246 | } |
230 | 247 | list(x = new_data, offset = offset) |
231 | 248 | } |
|
247 | 264 | #' |
248 | 265 | #' @importFrom dplyr bind_cols |
249 | 266 | .convert_xy_to_form_fit <- function(x, |
250 | | - y, |
251 | | - weights = NULL, |
252 | | - y_name = "..y", |
253 | | - remove_intercept = TRUE) { |
254 | | - if (is.vector(x)) |
| 267 | + y, |
| 268 | + weights = NULL, |
| 269 | + y_name = "..y", |
| 270 | + remove_intercept = TRUE) { |
| 271 | + if (is.vector(x)) { |
255 | 272 | rlang::abort("`x` cannot be a vector.") |
| 273 | + } |
256 | 274 |
|
257 | | - if(remove_intercept) { |
| 275 | + if (remove_intercept) { |
258 | 276 | x <- x[, colnames(x) != "(Intercept)", drop = FALSE] |
259 | 277 | } |
260 | 278 |
|
261 | 279 | rn <- rownames(x) |
262 | 280 |
|
263 | | - if (!is.data.frame(x)) |
| 281 | + if (!is.data.frame(x)) { |
264 | 282 | x <- as.data.frame(x) |
| 283 | + } |
265 | 284 |
|
266 | 285 | if (is.matrix(y)) { |
267 | 286 | y <- as.data.frame(y) |
|
277 | 296 | form <- make_formula(names(x), names(y)) |
278 | 297 |
|
279 | 298 | x <- bind_cols(x, y) |
280 | | - if(!is.null(rn) & !inherits(x, "tbl_df")) |
| 299 | + if (!is.null(rn) & !inherits(x, "tbl_df")) { |
281 | 300 | rownames(x) <- rn |
| 301 | + } |
282 | 302 |
|
283 | 303 | if (!is.null(weights)) { |
284 | | - if (!is.numeric(weights)) |
| 304 | + if (!is.numeric(weights)) { |
285 | 305 | rlang::abort("`weights` must be a numeric vector") |
286 | | - if (length(weights) != nrow(x)) |
| 306 | + } |
| 307 | + if (length(weights) != nrow(x)) { |
287 | 308 | rlang::abort(glue::glue("`weights` should have {nrow(x)} elements")) |
| 309 | + } |
288 | 310 | } |
289 | 311 |
|
290 | 312 | res <- list( |
|
301 | 323 | #' @export |
302 | 324 | .convert_xy_to_form_new <- function(object, new_data) { |
303 | 325 | new_data <- new_data[, object$x_var, drop = FALSE] |
304 | | - if (!is.data.frame(new_data)) |
| 326 | + if (!is.data.frame(new_data)) { |
305 | 327 | new_data <- as.data.frame(new_data) |
| 328 | + } |
306 | 329 | new_data |
307 | 330 | } |
308 | 331 |
|
|
0 commit comments