You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

  1. Floating-point length mismatches: Calculations like 0.3*n can produce decimals, and rep() truncates these to integers. This can make a or beta shorter than 100+n, leading to NA values in y2 that break the function's input checks.
  2. Undefined Nrofreps: If this variable wasn't set, it could create unexpected dimensions in g or X, messing up the length of g[,1].
  3. Outdated package version: An older genlasso version might have a bug in this validation check.

Fixes to Apply

  1. Enforce integer vector lengths: As shown above, use as.integer() to ensure a and beta are exactly 100+n elements long. Add checks to confirm:
    stopifnot(length(a) == 100 + n, length(beta) == 100 + n)
    
  2. Check for missing values in y2: After generating y2, verify there are no NAs:
    sum(is.na(y2))  # Should return 0
    
  3. Update the genlasso package: Run install.packages("genlasso") to get the latest version, which may resolve bugs in input validation.
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:50:39