Skip to content

Commit

Permalink
Merge pull request #12 from Anirban166/Generalizedcomplexity
Browse files Browse the repository at this point in the history
Generalizedcomplexity
  • Loading branch information
Anirban166 authored Jun 15, 2020
2 parents fd65471 + cbbf46e commit 016523f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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")
}

0 comments on commit 016523f

Please sign in to comment.