ignore/in_progress_tests/nonPenTests.r

##
## Testing penalizing only some variables. 
##
## Conclusion: Non penalized variables not done for QICD implementation. This is of minor importance. 
library(mvtnorm)
library(grpreg)
library(devtools)
install_github("bssherwood/rqpen",force=TRUE)
library(rqPen)
library(splines)


n <- 100
p <- 8
x <- matrix( rnorm(n*p), ncol=p)

y <- 1 + x[,1] + x[,3] - x[,8] + rnorm(n)
pen_spots <- c(2,4,6,8)
pen_x <- x[,pen_spots]
non_pen_x <- x[,-pen_spots]



#these get the same answer
lp_lasso <- rq.lasso.fit(x,y,lambda=.1,penVars=pen_spots)
quick_lasso <- LASSO.fit.nonpen(y, pen_x, non_pen_x, .5, lambda=.1, TRUE, 1e-08)
rqPen:::re_order_nonpen_coefs(quick_lasso, pen_spots)

#as do these
lp_lasso_no_inter <- rq.lasso.fit(x,y,lambda=.1,penVars=pen_spots,intercept=FALSE)
quick_lasso_no_inter <- LASSO.fit.nonpen(y, pen_x, non_pen_x, .5, lambda=.1, FALSE, 1e-08)
rqPen:::re_order_nonpen_coefs(quick_lasso_no_inter, pen_spots, FALSE)

cv_fit <- cv.rq.pen(x,y,penVars=pen_spots, criteria="BIC")
all_coefs <- sapply(cv_fit$models, coefficients)
all_coefs[pen_spots,] #this actually gives the nonpenalized vars after accounting for intercept and they are all non zero


lp_cv_scad <- cv.rq.pen(x,y, penVars=pen_spots, penalty="SCAD", alg="LP", criteria="BIC")
lp_scad_all_coefs <- sapply(lp_cv_scad$models, coefficients)
lp_scad_all_coefs[pen_spots,]

qicd_scad <- rq.nc.fit(x,y,penVars=pen_spots, penalty="SCAD", alg="QICD", lambda=.01)
qicd_cv_scad <- cv.rq.pen(x,y, penVars=pen_spots, penalty="SCAD", alg="QICD", criteria="BIC")
qicd_scad_all_coefs <- sapply(qicd_cv_scad$models, coefficients)
qicd_scad_all_coefs[pen_spots,]



## Testing non penalties for group penalties
##
g <- rep(seq(1,4),each=2)
penGroups <- c(1,3)

lp_glasso <- rq.group.fit(x,y,lambda=10,groups=g,penGroups=penGroups, alg="LP")
lp_gscad <- rq.group.fit(x,y,lambda=10,group=g,penGroups=penGroups, alg="LP",penalty="SCAD")

cv_lp_glasso <- cv.rq.group.pen(x,y,groups=g,penGroups=penGroups,alg="LP",criteria="BIC")
cv_lp_gscad <- cv.rq.group.pen(x,y,groups=g,penGroups=penGroups,alg="LP",criteria="BIC",penalty="SCAD")

#non pen group penalty work for qicd is not done yet
bssherwood/rqpen documentation built on April 23, 2024, 9:50 a.m.