Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions for generalized complexity classification #12

Merged
merged 3 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions R/asymptoticComplexityClass.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' Function to classify the complexity trend between two selected parameters from the data frame provided as input here
#'
#' @title Asymptotic Complexity Classification function
#'
#' @param model.df A data frame composing for two columns at the least, where one should be the contain the output-parameter sizes and one should contain the data sizes.
#'
#' @param output.size A string specifying the column name in the passed data frame to be used as the output size.
#'
#' @param data.size A string specifying the column name in the passed data frame to be used as the data size.
#'
#' @return A string specifying the resultant complexity class. (Eg: 'Linear', 'Log-linear', 'Quadratic')
#'
#' @export

asymptoticComplexityClass = function(df, output.size, data.size)
{
if(class(df) == "data.frame" & output.size %in% colnames(df) & data.size %in% colnames(df))
{
d <- data.frame('output' = df[[output.size]], 'size' = df[[data.size]])
asymptoticComplexityClassifier(d)
}
else stop("Input parameter must be a data frame containing the two specified columns passed as parameters")
}

38 changes: 38 additions & 0 deletions R/asymptoticComplexityClassifier.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' Function to classify the complexity trend between the two parameters as provided by asymptoticComplexityClass
#'
#' @title Asymptotic Complexity Classifier function
#'
#' @param model.df A data frame returned by asymptoticComplexityClass.
#'
#' @return A string specifying the resultant complexity class. (Eg: 'Linear', 'Log-linear', 'Quadratic')
#'
#' @export
#' @importFrom boot cv.glm
#' @importFrom stats fitted

asymptoticComplexityClassifier = function(df)
{
if(class(df) == "data.frame" & 'output' %in% colnames(df) & 'size' %in% colnames(df))
{
constant <- glm(output~1, data = df); df['constant'] = fitted(constant)
linear <- glm(output~size, data = df); df['linear'] = fitted(linear)
squareroot <- glm(output~sqrt(size), data = df); df['squareroot'] = fitted(squareroot)
log <- glm(output~log(size), data = df); df['log'] = fitted(log)
log.linear <- glm(output~size*log(size), data = df); df['log-linear'] = fitted(log.linear)
quadratic <- glm(output~I(size^2), data = df); df['quadratic'] = fitted(quadratic)
cubic <- glm(output~I(size^3), data = df); df['cubic'] = fitted(cubic)

model.list <- list('constant' = constant,
'linear' = linear,
'squareroot' = squareroot,
'log' = log,
'log-linear' = log.linear,
'quadratic' = quadratic,
'cubic' = cubic)

cross.validated.errors <- lapply(model.list, function(x) cv.glm(df, x)$delta[2])
best.model <- names(which.min(cross.validated.errors))
print(best.model)
}
else stop("Input parameter must be a data frame containing the two specified columns passed as parameters")
}