Skip to content

Commit

Permalink
fixing CRAN check warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Taylor Arnold committed Jul 27, 2018
1 parent 087a77c commit 12b7afb
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 74 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Package: genlasso
Type: Package
Title: Path algorithm for generalized lasso problems
Version: 1.3
Date: 2014-09-14
Version: 1.4
Date: 2018-07-27
Author: Taylor B. Arnold and Ryan J. Tibshirani
Maintainer: Taylor Arnold <taylor.arnold@aya.yale.edu>
Maintainer: Taylor Arnold <taylor.arnold@acm.org>
Description: This package computes the solution path for generalized lasso problems. Important use cases are the fused lasso over an arbitrary graph, and trend fitting of any given polynomial order. Specialized implementations for the latter two subproblems are given to improve stability and speed.
License: GPL (>= 2.0)
Depends: Matrix, igraph, R (>= 3.1.0)
ByteCompile: TRUE
URL: https://github.com/statsmaths/genlasso
URL: https://github.com/statsmaths/genlasso
RoxygenNote: 6.0.1
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import("Matrix", "igraph")

importFrom("grDevices", "rainbow")
importFrom("graphics", "abline", "legend", "lines", "matplot", "par",
"plot", "points", "text")
importFrom("stats", "coef", "rnorm", "sd")

export("coef.genlasso",
"cv.trendfilter",
"fusedlasso1d",
Expand Down
4 changes: 2 additions & 2 deletions R/dualpathFused.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ dualpathFused <- function(y, D, approx=FALSE, maxsteps=2000, minlam=0,
B = c(B,I[ihit])
I = I[-ihit]
s = c(s,shit)
D2 = rBind(D2,D1[ihit,])
D2 = rbind(D2,D1[ihit,])
D1 = D1[-ihit,,drop=FALSE]

if (verbose) {
Expand Down Expand Up @@ -295,7 +295,7 @@ dualpathFused <- function(y, D, approx=FALSE, maxsteps=2000, minlam=0,
I = c(I,B[ileave])
B = B[-ileave]
s = s[-ileave]
D1 = rBind(D1,D2[ileave,])
D1 = rbind(D1,D2[ileave,])
D2 = D2[-ileave,,drop=FALSE]

if (verbose) {
Expand Down
4 changes: 2 additions & 2 deletions R/dualpathFusedL1.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ dualpathFusedL1 <- function(y, D, D0, gamma, approx=FALSE, maxsteps=2000,
B = c(B,I[ihit])
I = I[-ihit]
s = c(s,shit)
D2 = rBind(D2,D1[ihit,])
D2 = rbind(D2,D1[ihit,])
D1 = D1[-ihit,,drop=FALSE]

if (verbose) {
Expand Down Expand Up @@ -314,7 +314,7 @@ dualpathFusedL1 <- function(y, D, D0, gamma, approx=FALSE, maxsteps=2000,
I = c(I,B[ileave])
B = B[-ileave]
s = s[-ileave]
D1 = rBind(D1,D2[ileave,])
D1 = rbind(D1,D2[ileave,])
D2 = D2[-ileave,,drop=FALSE]

if (verbose) {
Expand Down
4 changes: 2 additions & 2 deletions R/dualpathFusedL1X.R
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ dualpathFusedL1X <- function(y, X, D, D0, gamma, approx=FALSE, maxsteps=2000,
B = c(B,I[ihit])
I = I[-ihit]
s = c(s,shit)
D2 = rBind(D2,D1[ihit,])
D2 = rbind(D2,D1[ihit,])
D1 = D1[-ihit,,drop=FALSE]

if (verbose) {
Expand Down Expand Up @@ -384,7 +384,7 @@ dualpathFusedL1X <- function(y, X, D, D0, gamma, approx=FALSE, maxsteps=2000,
I = c(I,B[ileave])
B = B[-ileave]
s = s[-ileave]
D1 = rBind(D1,D2[ileave,])
D1 = rbind(D1,D2[ileave,])
D2 = D2[-ileave,,drop=FALSE]

if (verbose) {
Expand Down
4 changes: 2 additions & 2 deletions R/dualpathFusedX.R
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ dualpathFusedX <- function(y, X, D, approx=FALSE, maxsteps=2000, minlam=0,
B = c(B,I[ihit])
I = I[-ihit]
s = c(s,shit)
D2 = rBind(D2,D1[ihit,])
D2 = rbind(D2,D1[ihit,])
D1 = D1[-ihit,,drop=FALSE]

if (verbose) {
Expand Down Expand Up @@ -383,7 +383,7 @@ dualpathFusedX <- function(y, X, D, approx=FALSE, maxsteps=2000, minlam=0,
I = c(I,B[ileave])
B = B[-ileave]
s = s[-ileave]
D1 = rBind(D1,D2[ileave,])
D1 = rbind(D1,D2[ileave,])
D2 = D2[-ileave,,drop=FALSE]

if (verbose) {
Expand Down
56 changes: 28 additions & 28 deletions R/dualpathTrendX.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
n = length(y)

# Modify y,X in the case of a ridge penalty, but
# keep the originals
# keep the originals
y0 = y
X0 = X
if (eps>0) {
y = c(y,rep(0,p))
X = rbind(X,diag(sqrt(eps),p))
n = n+p
}
# Find the minimum 2-norm solution, using some linear algebra

# Find the minimum 2-norm solution, using some linear algebra
# tricks and a little bit of discrete calculus
if (is.null(pos)) pos = 1:p
Pos = matrix(rep(pos,each=ord),ord,p)
Expand All @@ -41,9 +41,9 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
for (i in Seq(2,ord+1)) {
ii = Seq(1,i-1)
basis[,i] = apply(pmax(Pos[ii,,drop=FALSE]-pos[ii],0),
2,prod)/factorial(i-1)
2,prod)/factorial(i-1)
}

# First project onto the row space of D*X^+
xy = t(X)%*%y
A = X%*%basis
Expand All @@ -54,7 +54,7 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
# e = solve(crossprod(A),z), for numerical stablity. Plus,
# it's not really any slower
g = xy-t(X)%*%(A%*%e)

# Here we perform our usual trend filter solve but
# with g in place of y
x = qr(t(D))
Expand All @@ -68,18 +68,18 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
cat(sprintf("1. lambda=%.3f, adding coordinate %i, |B|=%i...",
hit,ihit,1))
}

# Now iteratively find the new dual solution, and
# the next critical point

# Things to keep track of, and return at the end
buf = min(maxsteps,1000)
lams = numeric(buf) # Critical lambdas
h = logical(buf) # Hit or leave?
df = numeric(buf) # Degrees of freedom
u = matrix(0,m,buf) # Dual solutions
beta = matrix(0,p,buf) # Primal solutions

lams[1] = hit
h[1] = TRUE
df[1] = ncol(basis)
Expand All @@ -91,7 +91,7 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
2,prod)/factorial(ord)
newbv[Seq(1,ihit+ord)] = 0 # Only needed when ord=0
basis = cbind(basis,newbv)

# Other things to keep track of, but not return
r = 1 # Size of boundary set
B = ihit # Boundary set
Expand Down Expand Up @@ -124,7 +124,7 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
# In the case of a ridge penalty, modify X
if (eps>0) X = rbind(X,diag(sqrt(eps),p))
}

tryCatch({
while (k<=maxsteps && lams[k-1]>=minlam) {
##########
Expand All @@ -142,7 +142,7 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
# No updating, just recompute these every time
x = qr(t(D1))
Ds = as.numeric(t(D2)%*%s)

# Precomputation for the hitting times: first we project
# y and Ds onto the row space of D1*X^+
A = X%*%basis
Expand All @@ -158,12 +158,12 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
gb = Ds-t(X)%*%(A%*%eb)
fa = basis%*%ea
fb = basis%*%eb

# If the interior is empty, then nothing will hit
if (r==m) {
hit = 0
}

# Otherwise, find the next hitting time
else {
# Here we perform our usual trend filter solve but
Expand All @@ -177,12 +177,12 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
# than the current lambda (precision issue)
hits[hits>lams[k-1]+btol] = 0
hits[hits>lams[k-1]] = lams[k-1]

ihit = which.max(hits)
hit = hits[ihit]
shit = shits[ihit]
}

##########
# If nothing is on the boundary, then nothing will leave
# Also, skip this if we are in "approx" mode
Expand Down Expand Up @@ -228,21 +228,21 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
2,prod)/factorial(ord)
newbv[Seq(1,I[ihit]+ord)] = 0 # Only needed when ord=0
basis = cbind(basis,newbv)

# Update all other variables
r = r+1
B = c(B,I[ihit])
I = I[-ihit]
s = c(s,shit)
D2 = rBind(D2,D1[ihit,])
D2 = rbind(D2,D1[ihit,])
D1 = D1[-ihit,,drop=FALSE]

if (verbose) {
cat(sprintf("\n%i. lambda=%.3f, adding coordinate %i, |B|=%i...",
k,hit,B[r],r))
}
}

# Otherwise a leaving time comes next
else {
# Record the critical lambda and properties
Expand All @@ -253,16 +253,16 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
uhat[B] = leave*s
uhat[I] = a-leave*b
betahat = fa-leave*fb

# Update our basis
basis = basis[,-(ord+1+ileave)]

# Update all other variables
r = r-1
I = c(I,B[ileave])
B = B[-ileave]
s = s[-ileave]
D1 = rBind(D1,D2[ileave,])
D1 = rbind(D1,D2[ileave,])
D2 = D2[-ileave,,drop=FALSE]

if (verbose) {
Expand All @@ -281,14 +281,14 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
err$message = paste(err$message,"\n(Path computation has been terminated;",
" partial path is being returned.)",sep="")
warning(err)})
# Trim

# Trim
lams = lams[Seq(1,k-1)]
h = h[Seq(1,k-1)]
df = df[Seq(1,k-1)]
u = u[,Seq(1,k-1),drop=FALSE]
beta = beta[,Seq(1,k-1),drop=FALSE]

# If we reached the maximum number of steps
if (k>maxsteps) {
if (verbose) {
Expand All @@ -313,14 +313,14 @@ dualpathTrendX <- function(y, pos, X, D, ord, approx=FALSE, maxsteps=2000,
# The least squares solution (lambda=0)
bls = NULL
if (completepath) bls = fa

if (verbose) cat("\n")
# Save elements needed for continuing the path
pathobjs = list(type="trend.x", r=r, B=B, I=I, Q1=NA, approx=approx,
Q2=NA, k=k, df=df, D1=D1, D2=D2, Ds=Ds, ihit=ihit, m=m, n=n, p=p,
q=q, h=h, q0=NA, rtol=rtol, btol=btol, eps=eps, s=s, y=y, ord=ord,
pos=pos, Pos=Pos, basis=basis, xy=xy)

colnames(u) = as.character(round(lams,3))
colnames(beta) = as.character(round(lams,3))
return(list(lambda=lams,beta=beta,fit=X0%*%beta,u=u,hit=h,df=df,y=y0,X=X0,
Expand Down
Loading

0 comments on commit 12b7afb

Please sign in to comment.