R语言Genlasso包Fused Lasso报错求助:(mak <- max(k)) <= m - 1 is not TRUE
Fixing the
Error: (mak <- max(k)) <= m - 1 is not TRUE in Your Fused Lasso Code Let's break down what's going wrong and how to fix it step by step.
First, Let's Reproduce Your Code with Defined Variables
I noticed you didn't define Nrofreps in your code snippet—let's add that first to make the code runnable, plus fix some potential edge cases:
n <- 150 Nrofreps <- 1 # Set your desired resampling count here g <- matrix(0, n, Nrofreps) X <- array(0, dim = c(Nrofreps, n, 1)) e <- rnorm(100 + n, 1) # Use integer calculations to avoid floating-point length mismatches part1_a <- 100 + as.integer(0.3 * n) part2_a <- as.integer(0.4 * n) part3_a <- as.integer(0.3 * n) a <- c(rep(0, part1_a), rep(5, part2_a), rep(5, part3_a)) part1_beta <- 100 + as.integer(0.7 * n) part2_beta <- as.integer(0.3 * n) beta <- c(rep(0.0, part1_beta), rep(0.0, part2_beta)) y <- rep(0, 101 + n) for (z in 1:(100 + n)){ y[z+1] <- a[z] + beta[z] * y[z] + e[z] } y2 <- y[101:(100 + n)] g[,1] <- y2 X[1,,1] <- rep(5, n) # Load the required package (assuming you're using genlasso) library(genlasso) a1 <- fusedlasso1d(g[,1], X = as.matrix(X[1,,]), minlam = 3, gamma = 0.1)
Why the Error Happens
This error comes from the fusedlasso1d function (in the genlasso package) when the maximum difference order k you're using is too large for the length of your response variable. By default, k=1 (first-order differences), which should work fine for length(y2)=150 (since 1 <= 150-1). The most likely culprits in your original code are:
- Floating-point length mismatches: Calculations like
0.3*ncan produce decimals, andrep()truncates these to integers. This can makeaorbetashorter than100+n, leading toNAvalues iny2that break the function's input checks. - Undefined
Nrofreps: If this variable wasn't set, it could create unexpected dimensions ingorX, messing up the length ofg[,1]. - Outdated package version: An older
genlassoversion might have a bug in this validation check.
Fixes to Apply
- Enforce integer vector lengths: As shown above, use
as.integer()to ensureaandbetaare exactly100+nelements long. Add checks to confirm:stopifnot(length(a) == 100 + n, length(beta) == 100 + n) - Check for missing values in
y2: After generatingy2, verify there are no NAs:sum(is.na(y2)) # Should return 0 - Update the
genlassopackage: Runinstall.packages("genlasso")to get the latest version, which may resolve bugs in input validation. - Explicitly set
k=1: While this is the default, defining it explicitly can rule out unexpected parameter issues:a1 <- fusedlasso1d(g[,1], X = as.matrix(X[1,,]), minlam = 3, gamma = 0.1, k=1)
For Your Resampling Goal
Once the base code works, wrap the fitting logic in a loop to handle multiple resamples:
results <- list() for (rep in 1:Nrofreps) { # Replace this with your resampled data generation logic y2_resampled <- sample(y2, replace = TRUE) g[,rep] <- y2_resampled X[rep,,1] <- rep(5, n) # Adjust if you need resampled covariates results[[rep]] <- fusedlasso1d(g[,rep], X = as.matrix(X[rep,,]), minlam = 3, gamma = 0.1) }
内容的提问来源于stack exchange,提问作者Bas Ramaekers
相关产品推荐
相关产品推荐

