@@ -177,183 +177,3 @@ fast_nnls <- function(
177177
178178 return (d )
179179}
180-
181- fast_nnls_Matrix <- function (
182- ZTx ,
183- ZTZ ,
184- active = NULL ,
185- unconstrained = NULL ,
186- tolerance = 1e-10
187- ) {
188- # store number of covariates
189- p <- nrow(ZTZ )
190-
191- if (length(intersect(active , unconstrained )) > 0 ) {
192- stop(
193- " A variable cannot be assigned to the unconstrained set and to the active set."
194- )
195- }
196- # ## getting stuck in this initial loop for some reason! check it out!
197- if (! is.null(active )) {
198- # set d = 0 to start
199- d <- matrix (0 , nrow = p , ncol = 1 )
200- # check appropriateness of initial active set
201- s <- matrix (0 , nrow = p , ncol = 1 )
202- not_active <- ! (1 : p %in% active )
203- s_passive <- qr.solve(
204- ZTZ [not_active , not_active , drop = F ],
205- ZTx [not_active , drop = F ],
206- tol = 1e-14
207- )
208- s [not_active , ] <- s_passive
209-
210- # ## redefine not_active to excluded unconstrained variables (if included)
211- if (! is.null(unconstrained )) {
212- not_active <- ! (1 : p %in% union(active , unconstrained ))
213- s_passive <- s [not_active ]
214- }
215- # Enter inner loop
216- if (length(s_passive ) > 0 ) {
217- while (ifelse(length(s_passive ) > 0 , min(s_passive ) < = 0 , FALSE )) {
218- alphas <- (- (d [not_active ]) / (d [not_active ] - s_passive ))
219- alphas <- alphas [s_passive < 0 ]
220- alpha <- min(- alphas )
221-
222- d_archive <- d
223- d <- d + alpha * (s - d )
224- stopifnot(sum(is.na(d )) == 0 )
225-
226- if (is.null(unconstrained )) {
227- active <- (1 : p )[d == 0 ]
228- not_active <- ! (1 : p %in% active )
229- }
230- if (! is.null(unconstrained )) {
231- active <- (1 : p )[(d == 0 ) & (! ((1 : p ) %in% unconstrained ))]
232- not_active <- ! (1 : p %in% active )
233- }
234-
235- s <- matrix (0 , nrow = p )
236- if (length(active ) > 0 ) {
237- s_passive <- qr.solve(
238- ZTZ [not_active , not_active ],
239- ZTx [not_active , ],
240- tol = 1e-14
241- )
242- s [not_active , ] <- s_passive
243- } else {
244- s <- s_passive <- qr.solve(ZTZ , ZTx , tol = 1e-14 )
245- }
246-
247- if (! is.null(unconstrained )) {
248- not_active <- ! (1 : p %in% union(active , unconstrained ))
249- s_passive <- s [not_active ]
250- }
251- }
252- }
253- d <- s
254- active <- (1 : p )[d == 0 ]
255-
256- w <- ZTx - ZTZ %*% d
257- }
258- if (is.null(active )) {
259- if (is.null(unconstrained )) {
260- active <- 1 : p
261- d <- matrix (0 , nrow = p , ncol = 1 )
262- w <- ZTx
263- } else {
264- active <- (1 : p )[! (1 : p %in% unconstrained )]
265- d <- matrix (0 , nrow = p , ncol = 1 )
266- d [unconstrained ] <-
267- qr.solve(
268- ZTZ [unconstrained , unconstrained ],
269- ZTx [unconstrained , ],
270- tol = 1e-14
271- )
272- w <- (ZTx - ZTZ %*% d )
273- }
274- }
275-
276- # counter <- 0
277- # Enter main loop - ???? are loop conditions correct ???
278- while ((ifelse(length(active ) > 0 , max(w [active ]), - 1 ) > tolerance )) {
279- # counter <- counter + 1
280- # print(counter)
281-
282- to_remove <- which.max(w [active ])
283- # print(to_remove)
284- active <- active [- to_remove ]
285-
286- if (length(active ) > 0 ) {
287- s <- matrix (0 , nrow = p )
288- not_active <- ! (1 : p %in% active )
289-
290- s_passive <- qr.solve(
291- ZTZ [not_active , not_active ],
292- ZTx [not_active , ],
293- tol = 1e-14
294- )
295- s [not_active , ] <- s_passive
296- } else {
297- s <- s_passive <- qr.solve(ZTZ , ZTx , tol = 1e-14 )
298- }
299-
300- # ## redefine not_active to excluded unconstrained variables (if included)
301- if (! is.null(unconstrained )) {
302- not_active <- ! (1 : p %in% union(active , unconstrained ))
303- s_passive <- s [not_active ]
304- }
305-
306- # Enter inner loop
307- if (length(s_passive ) > 0 ) {
308- while (ifelse(length(s_passive ) > 0 , min(s_passive ) < = 0 , FALSE )) {
309- alphas <- (- (d [not_active ]) / (d [not_active ] - s_passive ))
310- alphas <- alphas [s_passive < 0 ]
311- alpha <- min(- alphas )
312-
313- d_archive <- d
314- d <- d + alpha * (s - d )
315- stopifnot(sum(is.na(d )) == 0 )
316-
317- if (is.null(unconstrained )) {
318- active <- (1 : p )[d == 0 ]
319- not_active <- ! (1 : p %in% active )
320- }
321- if (! is.null(unconstrained )) {
322- active <- (1 : p )[(d == 0 ) & (! ((1 : p ) %in% unconstrained ))]
323- not_active <- ! (1 : p %in% active )
324- }
325-
326- s <- matrix (0 , nrow = p )
327- if (length(active ) > 0 ) {
328- s_passive <- qr.solve(
329- ZTZ [not_active , not_active ],
330- ZTx [not_active , ],
331- tol = 1e-14
332- )
333- s [not_active , ] <- s_passive
334- } else {
335- s <- s_passive <- qr.solve(ZTZ , ZTx , tol = 1e-14 )
336- }
337-
338- # ## redefine not_active to excluded unconstrained variables (if included)
339- if (! is.null(unconstrained )) {
340- not_active <- ! (1 : p %in% union(active , unconstrained ))
341- s_passive <- s [not_active ]
342- }
343- }
344- }
345-
346- d <- s
347-
348- stopifnot(sum(is.na(d )) == 0 )
349-
350- w <- (ZTx - ZTZ %*% d )
351-
352- # counter <- counter + 1
353-
354- # print(counter)
355- # stopifnot(counter<13)
356- }
357-
358- return (d )
359- }
0 commit comments