Skip to content

Commit

Permalink
upload 0.02
Browse files Browse the repository at this point in the history
  • Loading branch information
flystar233 committed Aug 23, 2024
1 parent 9cf3b2b commit ec9b827
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: outqrf
Type: Package
Title: Find the outlier by quantile random forests
Version: 0.0.1
Date: 2024-08-22
Version: 0.0.2
Date: 2024-08-23
Author: Lucian Xu
Maintainer: Lucian Xu <[email protected]>
Description: Find the outlier by quantile random forests
Expand All @@ -15,4 +15,6 @@ Roxygen: list(markdown = TRUE)
Packaged: 2024-08-22 11:06:44 UTC;
RoxygenNote: 7.3.2
Imports:
ranger
ranger,
dplyr
URL: https://github.com/flystar233/outqrf
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(find_max_index)
export(find_index)
export(generateOutliers)
export(get_quantily_value)
export(outqrf)
61 changes: 39 additions & 22 deletions R/outqrf.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,57 @@ get_quantily_value <- function(name){
#' @examples
#' find_max_index(c(1, 2, 3, 4, 5), 3.5)
#' @export
find_max_index <- function(x, y) {
find_index <- function(x, y) {
index <- which(x == y)
if (length(index) >= 1) {
index_name <- names(x)[index]
value<-get_quantily_value(index_name)
return(value)
}
else {
closest_index <- which.min(abs(x - y))
closest_index <- which(abs(x - y) == min(abs(x-y)))
closest_index_name <- names(x)[closest_index]
value <- get_quantily_value(closest_index_name)
return(value)
}
}

#' @title find the right rank
#'
#' @description
#' This function finds the right rank of a response value in a quantile random forest.
#'
#' @param response a vector of response values
#' @param outMatrix a matrix of out values
#' @param median_outMatrix a vector of median out values
#' @param rmse_ a vector of rmse values
#'
#' @return a vector of ranks
#'
get_right_rank <- function(response,outMatrix,median_outMatrix,rmse_){
rank_value <-c()
for (i in 1:length(response)){
rank_<- find_index(outMatrix[i,],response[i])
if (length(rank_)>1){
diff = response[i] -median_outMatrix[i]
if (abs(diff)>3*rmse_ & diff<0 ){
min_value <- min(rank_)
rank_value<-c(rank_value,min_value)
} else if (abs(diff)>3*rmse_ & diff>0) {
max_value <- max(rank_)
rank_value<-c(rank_value,max_value)
}else {
mean_value <- mean(rank_)
rank_value<-c(rank_value,mean_value)
}
}else {
rank_value<-c(rank_value,rank_)
}

}
return(rank_value)
}

#' @title find outliers
#'
#' @description
Expand Down Expand Up @@ -101,26 +137,7 @@ outqrf <-function(data,
diffs = response - median_outMatrix
rmse_ <- sqrt(sum(diffs*diffs)/(length(diffs)-1))
rmse <- c(rmse,rmse_)
rank_value <-c()
for (i in 1:length(response)){
rank_<- find_max_index(outMatrix[i,],response[i])
if (length(rank_)>1){
diff = response[i] -median_outMatrix[i]
if (abs(diff)>3*rmse_ & diff<0 ){
min_value <- min(rank_)
rank_value<-c(rank_value,min_value)
} else if (abs(diff)>3*rmse_ & diff>0) {
max_value <- max(rank_)
rank_value<-c(rank_value,max_value)
}else {
mean_value <- mean(rank_)
rank_value<-c(rank_value,mean_value)
}
}else {
rank_value<-c(rank_value,rank_)
}

}
rank_value <- get_right_rank(response,outMatrix,median_outMatrix,rmse_)
outlier <- data.frame(row = as.numeric(row.names(data)),col = v,observed = response, predicted = median_outMatrix,rank = rank_value)
outlier<- outlier|>dplyr::filter(rank<=threshold_low| rank>=threshold_high)
outliers <- rbind(outliers,outlier)
Expand Down
6 changes: 3 additions & 3 deletions man/find_max_index.Rd → man/find_index.Rd

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

4 changes: 2 additions & 2 deletions man/generateOutliers.Rd

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

23 changes: 23 additions & 0 deletions man/get_right_rank.Rd

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

0 comments on commit ec9b827

Please sign in to comment.