3232# ' # maximum subset
3333# ' model <- lm(mpg ~ disp + hp + drat + wt + qsec, data = mtcars)
3434# ' ols_step_best_subset(model, max_order = 3)
35- # '
35+ # '
3636# ' # plot
3737# ' model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
3838# ' k <- ols_step_best_subset(model)
@@ -50,8 +50,8 @@ ols_step_best_subset <- function(model, ...) UseMethod("ols_step_best_subset")
5050
5151# ' @export
5252# ' @rdname ols_step_best_subset
53- # '
54- ols_step_best_subset.default <- function (model , max_order = NULL ,
53+ # '
54+ ols_step_best_subset.default <- function (model , max_order = NULL ,
5555 include = NULL , exclude = NULL ,
5656 metric = c(" rsquare" , " adjr" , " predrsq" ,
5757 " cp" , " aic" , " sbic" , " sbc" ,
@@ -77,7 +77,7 @@ ols_step_best_subset.default <- function(model, max_order = NULL,
7777 stop(paste(paste(exclude [! npm ], collapse = " , " ), " not part of the model and hence cannot be forcibly excluded. Please verify the variable names." ), call. = FALSE )
7878 }
7979 }
80-
80+
8181 if (is.numeric(include )) {
8282 if (any(include > lenterms )) {
8383 stop(paste0(" Index of variable to be included should be between 1 and " , lenterms , " ." ), call. = FALSE )
@@ -88,7 +88,7 @@ ols_step_best_subset.default <- function(model, max_order = NULL,
8888
8989 if (is.numeric(exclude )) {
9090 if (any(exclude > lenterms )) {
91- stop(paste0(" Index of variable to be excluded should be between 1 and " , lenterms , " ." ), call. = FALSE )
91+ stop(paste0(" Index of variable to be excluded should be between 1 and " , lenterms , " ." ), call. = FALSE )
9292 } else {
9393 exclude <- indterms [exclude ]
9494 }
@@ -152,23 +152,23 @@ ols_step_best_subset.default <- function(model, max_order = NULL,
152152 lpreds <- c()
153153
154154 for (i in seq_len(len_elig )) {
155- predictors <- predicts [[i ]]
156- lp <- length(predictors )
157- out <- ols_regress(paste(response , " ~" , paste(predictors , collapse = " + " )), data = data )
158- mcount <- mcount + 1
159- lpreds [mcount ] <- lp
160- rsq [[mcount ]] <- out $ rsq
161- adjr [[mcount ]] <- out $ adjr
162- cp [[mcount ]] <- ols_mallows_cp(out $ model , model )
163- aic [[mcount ]] <- ols_aic(out $ model )
164- sbic [[mcount ]] <- ols_sbic(out $ model , model )
165- sbc [[mcount ]] <- ols_sbc(out $ model )
166- gmsep [[mcount ]] <- ols_msep(out $ model )
167- jp [[mcount ]] <- ols_fpe(out $ model )
168- pc [[mcount ]] <- ols_apc(out $ model )
169- sp [[mcount ]] <- ols_hsp(out $ model )
170- predrsq [[mcount ]] <- ols_pred_rsq(out $ model )
171- preds [[mcount ]] <- paste(predictors , collapse = " " )
155+ predictors <- predicts [[i ]]
156+ lp <- length(predictors )
157+ out <- ols_regress(paste(response , " ~" , paste(predictors , collapse = " + " )), data = data )
158+ mcount <- mcount + 1
159+ lpreds [mcount ] <- lp
160+ rsq [[mcount ]] <- out $ rsq
161+ adjr [[mcount ]] <- out $ adjr
162+ cp [[mcount ]] <- ols_mallows_cp(out $ model , model )
163+ aic [[mcount ]] <- ols_aic(out $ model )
164+ sbic [[mcount ]] <- ols_sbic(out $ model , model )
165+ sbc [[mcount ]] <- ols_sbc(out $ model )
166+ gmsep [[mcount ]] <- ols_msep(out $ model )
167+ jp [[mcount ]] <- ols_fpe(out $ model )
168+ pc [[mcount ]] <- ols_apc(out $ model )
169+ sp [[mcount ]] <- ols_hsp(out $ model )
170+ predrsq [[mcount ]] <- ols_pred_rsq(out $ model )
171+ preds [[mcount ]] <- paste(predictors , collapse = " " )
172172 }
173173
174174 ui <- data.frame (
@@ -197,13 +197,13 @@ ols_step_best_subset.default <- function(model, max_order = NULL,
197197 } else {
198198 temp <- lapply(l , function (x ) x [order(x [[metrics ]]), ][1 , ])
199199 }
200-
200+
201201 sorted <- do.call(rbind , temp )
202202 mindex <- seq_len(nrow(sorted ))
203203 sorted <- cbind(mindex , sorted )
204204 result <- list (metrics = sorted )
205205 class(result ) <- c(" ols_step_best_subset" )
206-
206+
207207 return (result )
208208
209209}
@@ -261,8 +261,8 @@ best_subset_plot <- function(d, var, title = "R-Square") {
261261 ggplot(d1 , aes(x = a , y = b )) +
262262 geom_line(color = " blue" ) +
263263 geom_point(color = " blue" , shape = 1 , size = 2 ) +
264- xlab(" " ) +
265- ylab(" " ) +
264+ xlab(" " ) +
265+ ylab(" " ) +
266266 ggtitle(title ) +
267267 theme(axis.ticks = element_blank())
268268
0 commit comments