-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Anirban166/Generalizedcomplexity
Generalizedcomplexity
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |