@@ -94,6 +94,7 @@ ccdr.run <- function(data,
9494 max.iters = NULL ,
9595 alpha = 10 ,
9696 betas ,
97+ sigmas = NULL ,
9798 verbose = FALSE
9899){
99100 # ## Check data format
@@ -108,6 +109,7 @@ ccdr.run <- function(data,
108109 ccdr_call(data = data_matrix ,
109110 ivn = ivn_list ,
110111 betas = betas ,
112+ sigmas = sigmas ,
111113 lambdas = lambdas ,
112114 lambdas.length = lambdas.length ,
113115 whitelist = whitelist ,
@@ -132,6 +134,7 @@ MAX_CCS_ARRAY_SIZE <- function() 10000
132134ccdr_call <- function (data ,
133135 ivn = NULL ,
134136 betas ,
137+ sigmas ,
135138 lambdas ,
136139 lambdas.length ,
137140 whitelist = NULL ,
@@ -186,6 +189,11 @@ ccdr_call <- function(data,
186189 nj [j ] <- sum(! sapply(lapply(ivn , is.element , j ), any )) # # optimize for sorted column?
187190 }
188191
192+ # ## Set default for sigmas (negative values => ignore initial value and update as usual)
193+ if (is.null(sigmas )){
194+ sigmas <- rep(- 1 . , pp )
195+ }
196+
189197 # ## Use default values for lambda if not specified
190198 if (is.null(lambdas )){
191199 if (is.null(lambdas.length )){
@@ -273,6 +281,7 @@ ccdr_call <- function(data,
273281 as.integer(nj ),
274282 as.integer(indexj ),
275283 betas ,
284+ as.numeric(sigmas ),
276285 as.numeric(lambdas ),
277286 as.integer(weights ),
278287 as.numeric(gamma ),
@@ -309,6 +318,7 @@ ccdr_gridR <- function(cors,
309318 nj = NULL ,
310319 indexj = NULL ,
311320 betas ,
321+ sigmas ,
312322 lambdas ,
313323 weights ,
314324 gamma ,
@@ -341,6 +351,7 @@ ccdr_gridR <- function(cors,
341351 nj ,
342352 indexj ,
343353 betas ,
354+ sigmas ,
344355 lambdas [i ],
345356 weights ,
346357 gamma = gamma ,
@@ -380,6 +391,7 @@ ccdr_singleR <- function(cors,
380391 nj = NULL ,
381392 indexj = NULL ,
382393 betas ,
394+ sigmas ,
383395 lambda ,
384396 weights ,
385397 gamma ,
@@ -389,31 +401,36 @@ ccdr_singleR <- function(cors,
389401 verbose = FALSE
390402){
391403
392- if (is.null(indexj )) indexj <- rep(0L , pp + 1 )
404+ # ## Check dimension parameters
405+ if (! is.integer(pp ) || ! is.integer(nn )) stop(" Both pp and nn must be integers!" )
406+ if (pp < = 0 || nn < = 0 ) stop(" Both pp and nn must be positive!" )
407+
408+ # ## These variables, if NULL, need to be initialized before checking anything
409+ if (is.null(indexj )) indexj <- rep(0L , pp + 1 ) # initialize indexj
410+ if (is.null(nj )) nj <- as.integer(rep(nn , pp )) # initialize nj
411+
393412 # ## Check indexj
394413 if (! is.vector(indexj )) stop(" Index vector for cors is not a vector." )
395414 if (length(indexj ) > pp + 1 ) stop(sprintf(" Index vector for cors is too long, expected to be no greater than %d, the number of columns of data." , pp ))
396415 if (! is.integer(indexj )) stop(" Index vector for cors has non-integer component(s)." )
416+ if (any(is.na(indexj ) | is.null(indexj ))) stop(" Index vector cannot have missing or NULL values." )
397417 if (any(indexj < 0 | indexj > pp + 1 )) stop(sprintf(" Index vector for cors has out-of-range component(s), expected to be between 0 and %d." , pp ))
398418
399- if (is.null(nj )) nj <- as.integer(rep(nn , pp ))
400419 # ## Check nj
401420 if (! is.vector(nj )) stop(" Intervention times vector is not a vector." )
402- if (length(nj ) != pp ) stop(sprintf(" Length of intervention times vector is %d, expected %d% to match the number of columns of data" , length(nj ), pp ))
421+ if (length(nj ) != pp ) stop(sprintf(" Length of intervention times vector is %d, expected to match the number of columns of data = %d " , length(nj ), pp ))
403422 if (! is.integer(nj )) stop(" Intervention times vector has non-integer component(s)." )
423+ if (any(is.na(nj ) | is.null(nj ))) stop(" Intervention times vector cannot have missing or NULL values." )
404424 if (any(nj < 0 | nj > nn )) stop(sprintf(" Intervention times vector has out-of-range component(s), expected to be between 0 and %d." , nn ))
405425
406- # ## add a weight a_j to penalty on beta_{ij}
407- # ## since now with intervention data, beta_{ij} only appears n_j times out of total nn samples
408- aj <- nj / nn
409-
410426 # ## Check cors
427+ # ## This check must come after the checks for indexj, nj since these values are used to check cors
411428 if (! is.numeric(cors )) stop(" cors must be a numeric vector!" )
412429 if (length(cors ) != length(unique(indexj ))* pp * (pp + 1 )/ 2 ) stop(paste0(" cors has incorrect length: Expected length = " , length(unique(indexj ))* pp * (pp + 1 )/ 2 , " input length = " , length(cors )))
413430
414- # ## Check dimension parameters
415- if ( ! is.integer( pp ) || ! is.integer( nn )) stop( " Both pp and nn must be integers! " )
416- if ( pp < = 0 || nn < = 0 ) stop( " Both pp and nn must be positive! " )
431+ # ## add a weight a_j to penalty on beta_{ij}
432+ # ## since now with intervention data, beta_{ij} only appears n_j times out of total nn samples
433+ aj <- nj / nn
417434
418435 # ## Check betas
419436 if (sparsebnUtils :: check_if_matrix(betas )){ # if the input is a matrix, convert to SBM object
@@ -423,12 +440,22 @@ ccdr_singleR <- function(cors,
423440 stop(" Incompatible data passed for betas parameter: Should be either matrix or list in SparseBlockMatrixR format." )
424441 }
425442
443+ # ## Check sigmas
444+ if (! is.numeric(sigmas )) stop(" sigmas must be numeric!" )
445+ if (length(sigmas ) != pp ) stop(sprintf(" sigmas must have length = %d!" , pp ))
446+ if (any(sigmas < 0 )){
447+ # -1 is a sentinel value for updating sigmas via the CD updates
448+ if (any(sigmas != - 1 . )){
449+ stop(" sigmas must be > 0!" )
450+ }
451+ }
452+
426453 # ## Check lambda
427454 if (! is.numeric(lambda )) stop(" lambda must be numeric!" )
428455 if (lambda < 0 ) stop(" lambda must be >= 0!" )
429456
430457 # ## Check weights
431- if (length(weights ) != pp * pp ) stop(" weights must have length p^2! " )
458+ if (length(weights ) != pp * pp ) stop(sprintf( " weights must have length p^2 = %d! " , pp * pp ) )
432459 if (! is.numeric(weights )) stop(" weights must be numeric!" )
433460 if (weights < - 1 || weights > 1 ) stop(" weights out of bounds!" )
434461
@@ -453,6 +480,7 @@ ccdr_singleR <- function(cors,
453480 t1.ccdr <- proc.time()[3 ]
454481 ccdr.out <- singleCCDr(cors ,
455482 betas ,
483+ sigmas ,
456484 nj ,
457485 indexj ,
458486 aj ,
0 commit comments