From 347ca06acb0cd3238026c75c6ffd40afbb6646b0 Mon Sep 17 00:00:00 2001
From: wbonat <wbonat@gmail.com>
Date: Fri, 2 Oct 2015 14:51:21 +0200
Subject: [PATCH] Fix minor errors

---
 R/mc_fast_forward.R    | 42 +++++++++++++++++++++++-------------------
 man/mc_fast_forward.Rd |  2 ++
 2 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/R/mc_fast_forward.R b/R/mc_fast_forward.R
index d8a9245..07869e6 100644
--- a/R/mc_fast_forward.R
+++ b/R/mc_fast_forward.R
@@ -8,38 +8,42 @@
 #' @param scope a vector specyfing the covariate to be tested.
 #' @param interaction Maximum number of covariates interacting.
 #' @param penalty penalty term (default = 2).
+#' @param n_max Maximum number of models to be fitted.
 #' @return The selected model.
 #' @export
 
 mc_fast_forward <- function(object, scope, interaction = 1,
-                            penalty =2, n_max = 10) {
+                            penalty = 2, n_max = 10) {
   if (interaction > 1) {
     int_terms <- list()
     for (i in 2:interaction) {
       int_terms[[c(i-1)]] <- combn(length(scope), i)
     }
+    fun_temp <- function(int_terms) {
+      output <- c()
+      for(i in 1:dim(int_terms)[2]) {
+        output[i] <- paste(scope[int_terms[,i]],collapse = "*")
+        }
+      return(output)
+      }
+  scope <- do.call(c, lapply(int_terms, fun_temp))
   }
-  fun_temp <- function(int_terms) {
-    output <- c()
-    for(i in 1:dim(int_terms)[2]) {
-      output[i] <- paste(scope[int_terms[,i]],collapse = "*")
-    }
-    return(output)
-  }
-  scope <- c(scope, do.call(c, lapply(int_terms, fun_temp)))
   for (i in 1:n_max) {
-  sic <- mc_sic(object = object, scope = scope,
-                data = data, response = 1)
-  if (all(sic$SIC < 0)) break
-  cov_new <- as.numeric(rownames(sic[which.max(sic$SIC),]))
-  cov_enter <- scope[as.numeric(rownames(sic[which.max(sic$SIC),]))]
-  next_cov <- paste("~. +", cov_enter)
-  new_formula <- update.formula(object$linear_pred[[1]], next_cov)
-  temp_models <- mcglm(c(new_formula), matrix_pred = matrix_pred,
+    if(length(scope) == 0) break
+    sic <- mc_sic(object = object, scope = scope,
+                  data = data, response = 1)
+    if (all(sic$SIC < 0)) break
+    cov_new <- as.numeric(rownames(sic[which.max(sic$SIC),]))
+    cov_enter <- scope[as.numeric(rownames(sic[which.max(sic$SIC),]))]
+    next_cov <- paste("~. +", cov_enter)
+    new_formula <- update.formula(object$linear_pred[[1]], next_cov)
+    temp_models <- mcglm(c(new_formula),
+                       matrix_pred = object$matrix_pred,
                        link = object$link, variance = object$variance,
                        covariance = object$covariance, data = data)
-  object <- temp_models
-  scope <- scope[-cov_new]
+    object <- temp_models
+    scope <- scope[-cov_new]
+    cat(paste("Iteration", i), "\n")
   }
   return(object)
 }
diff --git a/man/mc_fast_forward.Rd b/man/mc_fast_forward.Rd
index 0e8f2cc..242ad84 100644
--- a/man/mc_fast_forward.Rd
+++ b/man/mc_fast_forward.Rd
@@ -15,6 +15,8 @@ mc_fast_forward(object, scope, interaction = 1, penalty = 2, n_max = 10)
 \item{interaction}{Maximum number of covariates interacting.}
 
 \item{penalty}{penalty term (default = 2).}
+
+\item{n_max}{Maximum number of models to be fitted.}
 }
 \value{
 The selected model.
-- 
GitLab