Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Version 2.0.0 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Dani-Basta committed Jan 8, 2020
1 parent 759a945 commit 5fb58a0
Show file tree
Hide file tree
Showing 38 changed files with 1,673 additions and 2,210 deletions.
27 changes: 11 additions & 16 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
Package: knnp
Version: 1.0.0
Date: 2018-06-18
Title: Time Series Prediction using K-Nearest Neighbors Algorithm
(Parallel)
Version: 2.0.0
Date: 2020-01-08
Title: Time Series Prediction using K-Nearest Neighbors Algorithm (Parallel)
Authors@R: c(
person("Daniel", "Bastarrica Lacalle", email="[email protected]", role=c("aut")),
person("Javier", "Berdecio Trigueros", email="[email protected]", role=c("aut", "cre"))
person("Daniel", "Bastarrica Lacalle", email="[email protected]", role=c("aut", "cre")),
person("Javier", "Berdecio Trigueros", email="[email protected]", role=c("aut")),
person("Javier", "Arroyo Gallardo", email="[email protected]", role=c("aut")),
person("Albert", "Meco Alias", email="[email protected]", role=c("aut"))
)
Depends: R (>= 3.3.3)
Imports: parallelDist, forecast, stats, utils, doParallel, foreach
Depends: R (>= 3.6.1)
Imports: parallelDist, forecast, stats, utils, doParallel, foreach, plyr
Suggests: tseries, tsibble
Description: Two main functionalities are provided. One of them is predicting values with
k-nearest neighbors algorithm and the other is optimizing the parameters k and d of the algorithm.
These are carried out in parallel using multiple threads.
License: AGPL-3
RoxygenNote: 6.0.1
URL: https://github.com/Dani-Basta/TFG
BugReports: https://github.com/Dani-Basta/TFG/issues
NeedsCompilation: no
Packaged: 2018-06-18 04:15:01 UTC; javier
Author: Daniel Bastarrica Lacalle [aut],
Javier Berdecio Trigueros [aut, cre]
Maintainer: Javier Berdecio Trigueros <[email protected]>
Repository: CRAN
Date/Publication: 2018-07-01 15:00:02 UTC
RoxygenNote: 6.1.1
661 changes: 661 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions MD5

This file was deleted.

10 changes: 3 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(knn_distances)
export(knn_next)
export(knn_optim)
export(knn_optim_parallel)
export(knn_optim_parallel2)
export(knn_optim_parallelf)
export(knn)
export(knn_forecast)
export(knn_param_search)
export(knn_past)
import(foreach)
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# knnp 2.0.0
* Simplified package use with less functions
* Improved result return with information about kNN forecast process
* Added unified wrapper to parameters search and prediction
* Improved parallelization
* Improved compatibility with forecast package
* Added tsibble objects support
* Changes in parameters names

# knnp 1.0.0
* Initial release

73 changes: 73 additions & 0 deletions R/knn.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' Generic function to make a prediction for a time series.
#' If a knn model is provided as the first argument, knn_forecast
#' will be directly called. If single values are provided as k and d
#' as no parameter search can be perfomed, knn_forecast will be
#' called automatically. If no values are provided for k and/or d,
#' values 1 to 50 will be used by default.
#'
#' @param y A time series or a trained kNN model generated by the
#' knn_param_search function. In case that a model is provided the knn_forecast
#' function will be automatically called.
#' @param k Values of k's to be analyzed or chosen k for knn forecasting.
#' Default value is 1 to 50.
#' @param d Values of d's to be analyzed or chosen d for knn forecasting.
#' Default value is 1 to 50.
#' @param initial Variable that determines the limit of the known past for
#' the first instant predicted.
#' @param distance Type of metric to evaluate the distance between points.
#' Many metrics are supported: euclidean, manhattan, dynamic time warping,
#' camberra and others. For more information about the supported metrics check
#' the values that 'method' argument of function parDist (from parallelDist
#' package) can take as this is the function used to calculate the distances.
#' Link to package info: https://cran.r-project.org/web/packages/parallelDist
#' Some of the values that this argument can take are "euclidean", "manhattan",
#' "dtw", "camberra", "chord".
#' @param error_measure Type of metric to evaluate the prediction error.
#' Five metrics supported:
#' \describe{
#' \item{ME}{Mean Error}
#' \item{RMSE}{Root Mean Squared Error}
#' \item{MAE}{Mean Absolute Error}
#' \item{MPE}{Mean Percentage Error}
#' \item{MAPE}{Mean Absolute Percentage Error}
#' }
#' @param weight Type of weight to be used at the time of calculating the
#' predicted value with a weighted mean. Three supported: proportional,
#' average, linear.
#' \describe{
#' \item{proportional}{the weight assigned to each neighbor is inversely
#' proportional to its distance}
#' \item{average}{all neighbors are assigned with the same weight}
#' \item{linear}{nearest neighbor is assigned with weight k, second closest
#' neighbor with weight k-1, and so on until the least nearest
#' neighbor which is assigned with a weight of 1.}
#' }
#' @param v Variable to be predicted if given multivariate time series.
#' @param threads Number of threads to be used when parallelizing, default is 1
#' @return A matrix of errors, optimal k and d. All tested ks and ks and all
#' the used metrics.
#' @examples
#' knn(AirPassengers, 1:5, 1:3)
#' knn(LakeHuron, 1:10, 1:6)
#' @export
knn <- function(y, k = 1:50, d = 1:50, distance = "euclidean", error_measure =
"MAE", weight = "proportional", v = 1, threads = 1) {
if (any(class(y) == "kNN")) {
warning("kNN model provided, simple prediction carried out",
immediate. = TRUE)
knn_forecast(y)
}
else if (length(k) == 1 && length(d) == 1) {
warning(paste0("k and d are single integers: supposing simple ",
"prediction is required"), immediate. = TRUE)
knn_forecast(y = y, k = k, d = d, distance = distance, weight = weight,
v = v, threads = threads)
}
else {
warning(paste0("Beginning parameter search process. This may take a ",
"while"), immediate. = TRUE)
knn_forecast(knn_param_search(y = y, k = k, d = d, distance = distance,
error_measure = error_measure, weight = weight,
v = v, threads = threads))
}
}
62 changes: 0 additions & 62 deletions R/knn_distances.R

This file was deleted.

8 changes: 5 additions & 3 deletions R/knn_elements.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#' 'Elements' matrix computation
#'
#' Creates a matrix to be used for calculating distances. The most
#' recent 'element' is put in the first row of the matrix, the
#' second most recent 'element' in the second row and so on. Therefore,
Expand All @@ -8,13 +6,17 @@
#' @param y A matrix.
#' @param d Length of each of the 'elements'.
#' @return A matrix to be used for calculating distances.
#' @examples
#' knn_elements(matrix(AirPassengers), 2)
#' knn_elements(matrix(LakeHuron), 6)
knn_elements <- function(y, d) {
n <- NROW(y)
m <- NCOL(y)
last_elem <- n - d

# Fill matrix as described above, it is done vertically for efficiency reasons
# Fill matrix as described above, done vertically for efficiency reasons
elements_matrix <- matrix(nrow = last_elem + 1, ncol = d * m)

col <- 1
for (i in 1:m) {
for (j in 1:d) {
Expand Down
Loading

0 comments on commit 5fb58a0

Please sign in to comment.