1313# ' @param progress Logical; if \code{TRUE}, will display variable selection progress.
1414# ' @param details Logical; if \code{TRUE}, will print the regression result at
1515# ' each step.
16+ # ' @param steps Number of steps after which the stepwise procedures should stop.
1617# '
1718# ' @examples
19+ # ' # forward hierarchical selection
1820# ' model <- lm(y ~ ., data = surgical)
21+ # ' ols_step_hierarchical(model)
22+ # '
23+ # ' # backward hierarchical selection
1924# ' model <- lm(y ~ bcs + alc_heavy + pindex + enzyme_test + liver_test + alc_mod + age + gender, data = surgical)
25+ # ' ols_step_hierarchical(model, forward = FALSE)
26+ # '
27+ # ' # steps
2028# ' model <- lm(y ~ bcs + alc_heavy + pindex + enzyme_test + liver_test + age + gender + alc_mod, data = surgical)
29+ # ' ols_step_hierarchical(model, steps = 2)
2130# '
2231# ' @keywords internal
2332# '
2433# ' @noRd
2534# '
26- ols_step_hierarchical <- function (model , p_value = 0.1 , forward = TRUE , progress = FALSE , details = FALSE ) {
35+ ols_step_hierarchical <- function (model , p_value = 0.1 , forward = TRUE , progress = FALSE , details = FALSE , steps = NULL ) {
2736
2837 if (forward ) {
29- ols_step_hierarchical_forward(model , p_value , progress , details )
38+ ols_step_hierarchical_forward(model , p_value , progress , details , steps )
3039 } else {
31- ols_step_hierarchical_backward(model , p_value , progress , details )
40+ ols_step_hierarchical_backward(model , p_value , progress , details , steps )
3241 }
3342
3443}
3544
36- ols_step_hierarchical_forward <- function (model , p_value = 0.1 , progress = FALSE , details = FALSE ) {
45+ ols_step_hierarchical_forward <- function (model , p_value = 0.1 , progress = FALSE , details = FALSE , steps = NULL ) {
3746
3847 if (details ) {
3948 progress <- FALSE
@@ -56,7 +65,7 @@ ols_step_hierarchical_forward <- function(model, p_value = 0.1, progress = FALSE
5665 if (progress || details ) {
5766 ols_candidate_terms(nam , " forward" )
5867 }
59-
68+
6069 step <- 0
6170 rsq <- c()
6271 adjrsq <- c()
@@ -68,7 +77,7 @@ ols_step_hierarchical_forward <- function(model, p_value = 0.1, progress = FALSE
6877
6978 base_model <- lm(paste(response , " ~" , 1 ), data = l )
7079 rsq_base <- summary(base_model )$ r.squared
71-
80+
7281 if (details ) {
7382 ols_rsquared_init(NULL , " r2" , response , rsq_base )
7483 }
@@ -77,6 +86,10 @@ ols_step_hierarchical_forward <- function(model, p_value = 0.1, progress = FALSE
7786 ols_progress_init(" forward" )
7887 }
7988
89+ if (! is.null(steps )) {
90+ mlen_p <- steps
91+ }
92+
8093 for (i in seq_len(mlen_p )) {
8194 predictors <- c(preds , all_pred [i ])
8295 m <- lm(paste(response , " ~" , paste(predictors , collapse = " + " )), l )
@@ -151,7 +164,7 @@ ols_step_hierarchical_forward <- function(model, p_value = 0.1, progress = FALSE
151164
152165}
153166
154- ols_step_hierarchical_backward <- function (model , p_value = 0.1 , progress = FALSE , details = FALSE ) {
167+ ols_step_hierarchical_backward <- function (model , p_value = 0.1 , progress = FALSE , details = FALSE , steps = NULL ) {
155168
156169 if (details ) {
157170 progress <- FALSE
@@ -166,9 +179,9 @@ ols_step_hierarchical_backward <- function(model, p_value = 0.1, progress = FALS
166179 cterms <- nam
167180
168181 if (progress || details ) {
169- ols_candidate_terms(nam , " backward" )
182+ ols_candidate_terms(nam , " backward" )
170183 }
171-
184+
172185 step <- 0
173186 rsq <- c()
174187 adjrsq <- c()
@@ -179,7 +192,7 @@ ols_step_hierarchical_backward <- function(model, p_value = 0.1, progress = FALS
179192 rmse <- c()
180193
181194 rsq_base <- summary(model )$ r.squared
182-
195+
183196 if (details ) {
184197 ols_rsquared_init(NULL , " r2" , response , rsq_base )
185198 }
@@ -194,7 +207,7 @@ ols_step_hierarchical_backward <- function(model, p_value = 0.1, progress = FALS
194207 m_sum <- Anova(m )
195208 pvals <- m_sum $ `Pr(>F)` [1 : i ]
196209 p_vals <- pvals [i ]
197-
210+
198211 if (details ) {
199212 d <- data.frame (predictors = predictors , p_val = pvals )
200213 ols_stepwise_table_p(d , predictors , pvals )
@@ -222,6 +235,12 @@ ols_step_hierarchical_backward <- function(model, p_value = 0.1, progress = FALS
222235 ols_stepwise_details(step , preds , rpred , response , rsq1 , " removed" , " rsq" )
223236 }
224237
238+ if (! is.null(steps )) {
239+ if (step == steps ) {
240+ break
241+ }
242+ }
243+
225244 } else {
226245
227246 if (progress || details ) {
0 commit comments