How to handle Error in lm.fit (0 non-na cases) with createFolds function in R

Viewed 47

I want to run a regression model using creatFolds function and specific set.seed.

I can run the regression model without creatFolds function with no Error.

The friedman dataset can be found here

   wdbc <- read_csv("friedman.dat", col_names = FALSE, skip = 10) 
   wdbc[] <- lapply(wdbc, scale)

The regression model without createFolds Function

    lm(X6 ~ ., data = wdbc) 

Call:
lm(formula = X6 ~ ., data = wdbc)

Coefficients:
(Intercept)           X1           X2           X3           X4           X5  
  2.553e-16    3.892e-01    3.563e-01    2.834e-04    5.739e-01    2.767e-01  

The regression model with createFolds Function

 FX <- function(seed, data, k=30) {
      set.seed(seed)  
      folds <- createFolds(data[, "X6"], k=k, list=TRUE, returnTrain=TRUE) 
      lm1 <- lapply(folds, function(folds) lm(X6 ~ ., data=data[folds, ]))
    }
    seeds <- 1:30 
    res <- lapply(seeds, FX, data=wdbc) 

 Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases  
1 Answers

The createFolds function is the problematic part:

createFolds(wdbc[, "X6"], k=1, list=TRUE, returnTrain=TRUE)
$Fold1
integer(0)

This gives you an empty vector, hence you are regressing blank. So there is a difference when you do the different calls below:

typeof(wdbc[["X6"]])
[1] "double"
typeof(wdbc[,"X6"])
[1] "list"
typeof(as.matrix(wdbc)[,"X6"])
[1] "double"

Maybe someone else more well versed in tibble etc can chip in.

You can either convert your data into a data.frame or use data[["X6"]]:

FX <- function(seed, data, k=30) {
      set.seed(seed)  
      folds <- createFolds(data[["X6"]], k=k, list=TRUE, returnTrain=TRUE) 
      lm1 <- lapply(folds, function(folds) lm(X6 ~ ., data=data[folds, ]))
    }
    seeds <- 1:30 
res <- lapply(seeds, FX, data=wdbc)

res[[1]][[1]]

Call:
lm(formula = X6 ~ ., data = data[folds, ])

Coefficients:
(Intercept)           X1           X2           X3           X4           X5  
  0.0007255    0.3933912    0.3600078   -0.0008834    0.5733893    0.2729162  
Related