I am using CVXR to code a penalized linear regression. My global loss is composed of 4 elements: two differents SSE losses loss_u, loss_b on two different data sets, a ridge penalty and a specific distance D. The code works if I use the 'distance == "MM"'. However, there is an error for 'distance == "MMD"'. I use an external rcpp function from kernal "kernlab::kmmd". The problem is that "Xb %*% beta" is a MulExpression. I dont know if I should convert it into a numeric (but how?) or if it is impossible to use rcpp function.
deb_reg <- function(Xu, Yu, Xb, Yb, beta, lambda = 0, theta = 0.5, alpha = 0, distance = "MM") {
n <- nrow(Xu)
m <- nrow(Xb)
ridge <- lambda * sum(beta^2)
loss_u <- sum((Yu - Xu %*% beta)^2) * ( theta/ n )
loss_b <- sum((Yb - Xb %*% beta)^2) * ( (1-theta)/ m )
if(distance == "MM"){
D <- alpha * ( mean(Yu) - mean(Xb %*% beta) )^2
} else if(distance == "MMD"){
y <- as.numeric(Yu)
# print(beta)
x <- Xb %*% beta
# D <- alpha * EasyMMD::MMD(y, x)
MMD <- kernlab::kmmd(as.matrix(y), as.matrix(x))
D <- alpha * sum(MMD@mmdstats)
} else{
D <- 0
}
obj <- loss_u + loss_b + ridge + D
return(obj)
}
p <- ncol(X_unbiased)
beta <- Variable(p)
obj <- deb_reg(Xu = X_unbiased, Yu = Y_unbiased, Xb = X_biased, Yb = Y_biased, beta,
lambda = 0.1, theta=0.5, alpha = 10, distance = "MMD")
prob <- Problem(Minimize(obj))
result <- solve(prob)