Hello, I am currently trying to solve a problem with the boot package and writing a function within a function in R. I have developed several functions to perform the lasso but continue to receive an error when bootstrapping these functions within a wrapper function. When I perform these methods using tsboot outside the wrapper function, I do not get an error. However, when placed within my function I continue to get the error "Error in t[r, ] <- res[[r]] : number of items to replace is not a multiple of length."
I've attached an example of my functions as well as a data file of the data I am using. I'm sorry the file is so large, but I do not get a problem with a smaller number of observations. library(boot) library(glmnet) library(np) foo1 <- function(data,index){ #index is the bootstrap sample index x <- data[index, -1] %>% as.matrix() %>% unname() y <- data[index, 1] %>% scale(center = TRUE, scale = FALSE) %>% as.matrix() %>% unname() ols <- lm(y ~ x) # The intercept estimate should be dropped. ols.coef <- as.numeric(coef(ols))[-1] ols.coef[is.na(ols.coef)] <- 0 ## The intercept estimate should be dropped. lasso <- cv.glmnet(x, y, alpha = 1, penalty.factor = 1 / abs(ols.coef)) # Select nonzero coefficients from bic.out coef <- as.vector(coef(lasso, s = lasso$lambda.min))[-1] return(coef) } foo2 <- function(data, index){ #index is the bootstrap sample index x <- data[index, -1] %>% as.matrix() %>% unname() y <- data[index, 1] %>% scale(center = TRUE, scale = FALSE) %>% as.matrix() %>% unname() # ic.glmnet provides coefficients with lowest BIC ols <- lm(y ~ x) # The intercept estimate should be dropped. ols.coef <- as.numeric(coef(ols))[-1] ols.coef[is.na(ols.coef)] <- 0 lasso <- cv.glmnet(x, y, alpha = 1, penalty.factor = 1 / abs(ols.coef)) # Select nonzero coefficients from bic.out coef <- as.vector(coef(lasso, s = lasso$lambda.min))[-1] coef_nonzero <- coef != 0 if(sum(coef_nonzero) > 0) { ls_obj <- lm(y ~ x[, coef_nonzero, drop = FALSE]) ls_coef <- as.vector(coef(ls_obj))[-1] coef[coef_nonzero] <- ls_coef } return(coef) } foo3 <- function(data, num_samples) { bstar <- b.star(data[, 1], round = TRUE) # Select Block Length of circular block result blocklength <- bstar[, 2] init_boot_ts <- tsboot(tseries = data, statistic = foo1, R = num_samples, l = blocklength, sim = "fixed") final_boot_ts <- tsboot(tseries = data, statistic = foo2, R = num_samples, l = blocklength, sim = "fixed") # point estimates final_boot_t0 <- final_boot_ts$t0 return(list(point_estimates = final_boot_t0)) } num_samples <- 50 test.foo3 <- foo3(data, num_samples = num_samples) Which *sometimes *works, however, sometimes I get the error: “Error in t[r, ] <- res[[r]] : number of items to replace is not a multiple of length". I've also gotten the error "Error in x[, coef_nonzero, drop = FALSE] :(subscript) logical subscript too long" at times, when running foo2 within foo3. Which seems to be unclear as there should never be an index which is greater than the number of columns in x. As I stated, this error particularly occurs when I increase the number of bootstrap samples I try to run, and only when I run foo3. When I run foo1 and foo2 separately, I don’t get an error at all. I’m wondering if there is something I need to add to my function “foo3” to ensure that an error like this doesn’t occur since I am calling several other functions within this function, ie., tsboot, foo1, and foo2. Lastly, although I have not provided an example here, I get an error for this code when running with lapply for several dataframes similar to the attached. Again, I apologise for attaching such a large data frame but the function seems to work with fewer observations. Perhaps it is a data issue? Thanks ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.