I have been trying to fit a thin-plate spline to a brain scan dataset using the popular CVXR package in R, but unfortunately the main function of that package returns an error I have been unable to decipher. Here is the example I've been working on.
require(gamair)
require(CVXR)
require(npreg)
data(brain)
x = brain[, c(1, 2)]
x <- as.matrix(x)
z = brain$medFPQ
z <- as.vector(z)
m = 2
b.tp <- basis.tps(x, knots = x, m = m, rk = TRUE, intercept = TRUE)
pen.tp <- penalty.tps(x, m = m, rk = TRUE)
mstar <- choose(m+dim(x)[2]-1, dim(x)[2])
pen.tp <- rbind(matrix(0, ncol = dim(pen.tp)[2]+mstar, nrow = mstar), cbind( matrix(0, nrow = dim(pen.tp)[1], ncol = mstar ), pen.tp ) )
theta <- Variable(dim(b.tp)[2])
obj <- sum((z-b.tp%*%theta)^2) + 1e-01*quad_form(theta, pen.tp)
prob <- Problem(Minimize(obj))
result <- solve(prob, solver = "SCS")
And the error is
Error in (function (cl, name, valueClass) :
assignment of an object of class “complex” is not valid for @‘eigvals’ in an object of class “Constant”; is(value, "numeric") is not TRUE
I have been wondering what causes this, as I have not found any relevant information. I have, however, noticed that this error is much less likely to appear with smaller datasets. For example, if we use only a couple of hundred randomly sampled observations out of the 1567 available ones.
If anyone has additional information on how to resolve this problem, could I please ask for their help? Thank you.
I cannot reproduce the error (R 4.2.0 on GNU/Linux). But to venture a wild guess: apparently a complex value was computed, which would for instance happen when a square-root is computed from a negative number. Such a negative number might be only "numeric noise" (i.e. caused by rounding error and actually zero). Such a close-to-zero negative number might be the result of a computation that required a full-rank matrix, but did receive a rank-deficient matrix.
My session info: