diff --git a/DESCRIPTION b/DESCRIPTION index 2bfeffd6..52642885 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -152,5 +152,6 @@ Collate: 'random.R' 'test.R' 'timing.R' + 'variables.R' 'verbose.R' 'zzz.R' diff --git a/R/data.R b/R/data.R index ecc2531e..880ee23e 100644 --- a/R/data.R +++ b/R/data.R @@ -292,48 +292,3 @@ generateLongData = function( alldata[, Class := factor(clusterNames[Class], levels = clusterNames)] return(alldata[]) } - - -#' @title Guess the response variable -#' @description -#' Attempts to identify the response variable for the given trajectory data -#' @param data A trajectories `data.frame`. -#' @param id Id variable(s) to exclude. -#' @param time Time variable(s) to exclude. -#' @param cluster Cluster variable(s) to exclude. -#' @keywords internal -.guessResponseVariable = function( - data, - id = getOption('latrend.id'), - time = getOption('latrend.time'), - cluster = 'Cluster' -) { - assert_that( - is.data.frame(data) - ) - - excludeColumns = unique(c(id, time, cluster)) - - numMask = vapply(data, is.numeric, FUN.VALUE = TRUE) - numericColumns = names(data)[numMask] - - candidates = setdiff(numericColumns, excludeColumns) - if (length(candidates) == 0L) { - stop('unable to automatically determine the response variable. Specify the "response" argument.') - } - - dfNum = subset(data, select = candidates) - - counts = vapply(dfNum, uniqueN, FUN.VALUE = 0L) - response = names(dfNum)[which.max(counts)] - - message( - sprintf( - 'Automatically selected "%s" as the response variable. - To override this, specify the "response" argument.', - response - ) - ) - - response -} diff --git a/R/variables.R b/R/variables.R new file mode 100644 index 00000000..f176cb9b --- /dev/null +++ b/R/variables.R @@ -0,0 +1,57 @@ +#. idVariable #### +#' @export +#' @rdname idVariable +#' @aliases idVariable,ANY-method +setMethod('idVariable', 'ANY', function(object) getOption('latrend.id')) + + +#. timeVariable #### +#' @export +#' @rdname timeVariable +#' @aliases timeariable,ANY-method +setMethod('timeVariable', 'ANY', function(object) getOption('latrend.time')) + + +#' @title Guess the response variable +#' @description +#' Attempts to identify the response variable for the given trajectory data +#' @param data A trajectories `data.frame`. +#' @param id Id variable(s) to exclude. +#' @param time Time variable(s) to exclude. +#' @param cluster Cluster variable(s) to exclude. +#' @keywords internal +.guessResponseVariable = function( + data, + id = getOption('latrend.id'), + time = getOption('latrend.time'), + cluster = 'Cluster' +) { + assert_that( + is.data.frame(data) + ) + + excludeColumns = unique(c(id, time, cluster)) + + numMask = vapply(data, is.numeric, FUN.VALUE = TRUE) + numericColumns = names(data)[numMask] + + candidates = setdiff(numericColumns, excludeColumns) + if (length(candidates) == 0L) { + stop('unable to automatically determine the response variable. Specify the "response" argument.') + } + + dfNum = subset(data, select = candidates) + + counts = vapply(dfNum, uniqueN, FUN.VALUE = 0L) + response = names(dfNum)[which.max(counts)] + + message( + sprintf( + 'Automatically selected "%s" as the response variable. + To override this, specify the "response" argument.', + response + ) + ) + + response +} diff --git a/man/dot-guessResponseVariable.Rd b/man/dot-guessResponseVariable.Rd index bdfc895c..a26ed8b2 100644 --- a/man/dot-guessResponseVariable.Rd +++ b/man/dot-guessResponseVariable.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data.R +% Please edit documentation in R/variables.R \name{.guessResponseVariable} \alias{.guessResponseVariable} \title{Guess the response variable} diff --git a/man/idVariable.Rd b/man/idVariable.Rd index b322aa22..8841ed76 100644 --- a/man/idVariable.Rd +++ b/man/idVariable.Rd @@ -1,9 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/generics.R, R/method.R, R/model.R +% Please edit documentation in R/generics.R, R/method.R, R/model.R, +% R/variables.R \name{idVariable} \alias{idVariable} \alias{idVariable,lcMethod-method} \alias{idVariable,lcModel-method} +\alias{idVariable,ANY-method} \title{Extract the trajectory identifier variable} \usage{ idVariable(object, ...) @@ -11,6 +13,8 @@ idVariable(object, ...) \S4method{idVariable}{lcMethod}(object, ...) \S4method{idVariable}{lcModel}(object) + +\S4method{idVariable}{ANY}(object) } \arguments{ \item{object}{The object.} diff --git a/man/timeVariable.Rd b/man/timeVariable.Rd index 24c3bf22..0e5032de 100644 --- a/man/timeVariable.Rd +++ b/man/timeVariable.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/generics.R, R/method.R, R/model.R +% Please edit documentation in R/generics.R, R/method.R, R/model.R, +% R/variables.R \name{timeVariable} \alias{timeVariable} \alias{timeVariable,lcMethod-method} \alias{timeVariable,lcModel-method} +\alias{timeVariable,ANY-method} +\alias{timeariable,ANY-method} \title{Extract the time variable} \usage{ timeVariable(object, ...) @@ -11,6 +14,8 @@ timeVariable(object, ...) \S4method{timeVariable}{lcMethod}(object, ...) \S4method{timeVariable}{lcModel}(object) + +\S4method{timeVariable}{ANY}(object) } \arguments{ \item{object}{The object.}