Skip to content

Commit

Permalink
feat: default idVariable and timeVariable methods using the respectiv…
Browse files Browse the repository at this point in the history
…e options
  • Loading branch information
niekdt committed May 3, 2024
1 parent 40ae2b5 commit a80696b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 48 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,6 @@ Collate:
'random.R'
'test.R'
'timing.R'
'variables.R'
'verbose.R'
'zzz.R'
45 changes: 0 additions & 45 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
57 changes: 57 additions & 0 deletions R/variables.R
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion man/dot-guessResponseVariable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/idVariable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/timeVariable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a80696b

Please sign in to comment.