For given X find basis of kernel/nullspace of sum(X*gamma)

Job ID: 37446693

Budget: €8 – €30 EUR

For identification of a latent-variable model, I need to impose a sum-to-zero constraint on mu = X * gamma. X is a J * K full column rank design matrix and gamma is a K-length parameter vector to be estimated. To impose this constraint, a K*(K-1) matrix B is required as basis, so that gamma = B * gamma_raw, where gamma_raw is a K-1-length parameter vector, and the constraint then holds by construction.

Here is what I tried so far in R:

set.seed(123)

# simulate X
num_rows <- 20
num_columns <- 5
data <- data.frame(intercept = rep(1, num_rows))
for (i in 1:(num_columns - 1)) {
col_name <- paste0("indicator", i)
data[, col_name] <- sample(0:1, num_rows, replace = TRUE)
}
X <- model.matrix(~ . - 1, data)
print(X)
rankMatrix(X)

# use QR decomposition
QR <- qr(t(X))
B <- qr.Q(QR)[,1:K-1]

# use SVD
X_svd <- svd(X)
B <- X_svd$v[,1:K-1]

Using either of these B as basis does not impose the sum-to-zero constraint as intended.

Do not bother with chatGPT, it doesn't know how to do this. Script can also be delivered in Python code.