Skip to content

Commit

Permalink
Conv func (#5)
Browse files Browse the repository at this point in the history
* epid-class

epid-class

* changed rolling and fixed episodes func

Changed rolling and fixed episodes func to wrapper funcs of episode_group func. They retain their convenience and have the same arguments as episode_group. Previous versions are been archived

Added deduplicate argument to episode_group

New "epid"  object class for the results of episode grouping

* docs and pid objects

- added pid s4 object class
- added to_s4 arg and to_s4 func
- added to_df func
- corrected bug with bi_direction (#4)
- updated docs and site pages
  • Loading branch information
OlisaNsonwu committed Oct 20, 2019
1 parent ce8f09e commit 28aa503
Show file tree
Hide file tree
Showing 41 changed files with 2,675 additions and 1,725 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
^archive$
^CRAN-RELEASE$
^cran-comments\.md$
^cobertura\.xml$
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Type: Package
Title: Multistage Record Linkage and Case Definition for Epidemiological Analysis
Date: 2019-10-01
Version: 0.0.1.9000
URL: https://github.com/OlisaNsonwu/diyar
URL: https://olisansonwu.github.io/diyar/index.html
BugReports: https://github.com/OlisaNsonwu/diyar/issues
Author: Olisaeloka Nsonwu
Maintainer: Olisaeloka Nsonwu <olisa.nsonwu@gmail.com>
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Generated by roxygen2: do not edit by hand

S3method(format,epid)
S3method(format,number_line)
S3method(format,pid)
S3method(sort,number_line)
S3method(unique,epid)
S3method(unique,number_line)
S3method(unique,pid)
export(across)
export(aligns_end)
export(aligns_start)
Expand All @@ -27,7 +31,11 @@ export(right_point)
export(rolling_episodes)
export(shift_number_line)
export(start_point)
export(to_df)
export(to_s4)
exportClasses(epid)
exportClasses(number_line)
exportClasses(pid)
importFrom("methods","new")
importFrom("utils","head")
importFrom(dplyr,"%>%")
Expand Down
16 changes: 16 additions & 0 deletions NEWS.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ knitr::opts_chunk$set(
)
```

#Version 0.0.1.900
##New feature
+ `pid` S4 object class for results of `record_group()`. This will replace the current default (`data.frame`) in the next major release
+ `epid` S4 object class for results of `episode_group()`, `fixed_episodes()` and `rolling_episodes()`. This will replace the current default (`data.frame`) in the next major release
+ `to_s4()` and `to_s4` argument in `record_group()`, `episode_group()`, `fixed_episodes()` and `rolling_episodes()`. Changes their output from a `data.frame` (current deault) to `epid` or `pid` objects
+ `to_df()` changes `epid` or `pid` objects to a `data.frame`
+ `deduplicate` argument from `fixed_episodes()` and `rolling_episodes()` added to `episode_group()`

##Changes
+ `fixed_episodes()` and `rolling_episodes()` are now wrapper functions of `episode_group()`. Their functionality, including ease of use remains the same but now includes all arguments available to `episode_group()`
+ `pid_cri` column retunred in `record_group` is now `numeric`. `0` indicates no match.

##Bug fixes
+ (#3) - Resolved a bug with `episode_unit` in `episode_group()`
+ (#4) - Resolved a bug with `bi_direction` in `episode_group()`

#Version 0.0.1
##Features
+ `fixed_episodes()` and `rolling_episodes()` - Group records into fixed or rolling episodes of events or period of events.
Expand Down
24 changes: 24 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@

Version 0.0.1.900
=================

New feature
-----------

- `pid` S4 object class for results of `record_group()`. This will replace the current default (`data.frame`) in the next major release
- `epid` S4 object class for results of `episode_group()`, `fixed_episodes()` and `rolling_episodes()`. This will replace the current default (`data.frame`) in the next major release
- `to_s4()` and `to_s4` argument in `record_group()`, `episode_group()`, `fixed_episodes()` and `rolling_episodes()`. Changes their output from a `data.frame` (current deault) to `epid` or `pid` objects
- `to_df()` changes `epid` or `pid` objects to a `data.frame`
- `deduplicate` argument from `fixed_episodes()` and `rolling_episodes()` added to `episode_group()`

Changes
-------

- `fixed_episodes()` and `rolling_episodes()` are now wrapper functions of `episode_group()`. Their functionality, including ease of use remains the same but now includes all arguments available to `episode_group()`
- `pid_cri` column retunred in `record_group` is now `numeric`. `0` indicates no match.

Bug fixes
---------

- (\#3) - Resolved a bug with `episode_unit` in `episode_group()`
- (\#4) - Resolved a bug with `bi_direction` in `episode_group()`

Version 0.0.1
=============

Expand Down
262 changes: 262 additions & 0 deletions R/classes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
#' @name number_line-class
#' @aliases number_line-class
#' @title \code{number_line} object
#'
#' @description
#' S4 objects representing a series of finite numbers on a number line
#' Used for range matching in \code{\link{record_group}} and interval grouping in \code{\link{fixed_episodes}}, \code{\link{rolling_episodes}} and \code{\link{episode_group}}
#'
#' @slot start Start of the number line
#' @slot id Unique \code{numeric} ID. Providing this is optional.
#' @slot gid Unique \code{numeric} Group ID. Providing this is optional.
#' @slot .Data Length/with and direction of the \code{number_line} object.
#' @importFrom "methods" "new"
#' @importFrom "utils" "head"
#' @export
setClass("number_line", contains = "numeric", representation(start = "ANY", id = "numeric", gid = "numeric"))

#' @rdname number_line-class
#' @param object object
setMethod("show", signature(object="number_line"), function(object){
print(format.number_line(object))
})

#' @rdname number_line-class
#' @param x x
#' @param ... ...
setMethod("rep", signature(x = "number_line"), function(x, ...) {
methods::new("number_line", rep(x@.Data, ...), start = rep(x@start, ...), id = rep(x@id, ...), gid = rep(x@gid, ...))
})

#' @aliases [,number_line-method
#' @rdname number_line-class
#' @param i i
#' @param j j
#' @param drop drop
setMethod("[", signature(x = "number_line"),
function(x, i, j, ..., drop = TRUE) {
methods::new("number_line", x@.Data[i], start = x@start[i], id = x@id[i], gid = x@gid[i])
})

#' @aliases [[,number_line-method
#' @rdname number_line-class
#' @param exact exact
setMethod("[[", signature(x = "number_line"),
function(x, i, j, ..., exact = TRUE) {
methods::new("number_line", x@.Data[i], start = x@start[i], id = x@id[i], gid = x@gid[i])
})

#' @aliases [<-,number_line-method
#' @rdname number_line-class
#' @param value value
setMethod("[<-", signature(x = "number_line"), function(x, i, j, ..., value) {
if (is.number_line(value)) {
x@.Data[i] <- value@.Data
x@start[i] <- value@start
x@id[i] <- value@id
x@gid[i] <- value@gid
new("number_line", x@.Data, start = x@start, id = x@id, gid = x@gid)
}
})

#' @aliases [[<-,number_line-method
#' @rdname number_line-class
setMethod("[[<-", signature(x = "number_line"), function(x, i, j, ..., value) {
if (is.number_line(value)) {
x@.Data[i] <- value@.Data
x@start[i] <- value@start
x@id[i] <- value@id
x@gid[i] <- value@gid
new("number_line", x@.Data, start = x@start, id = x@id, gid = x@gid)
}
})

#' @rdname number_line-class
#' @param name slot name
setMethod("$", signature(x = "number_line"), function(x, name) {
methods::slot(x, name)
})

#' @rdname number_line-class
setMethod("$<-", signature(x = "number_line"), function(x, name, value) {
methods::slot(x, name) <- value
x
})

#' @rdname number_line-class
setMethod("c", signature(x = "number_line"), function(x,...) {
a <- lapply(list(x, ...), function(y) as.number_line(y)@start)
for(i in 1:length(a)){
if(i==1) ai <- a[[i]]
if(i>1) ai <- c(ai, a[[i]])
}

id <- unlist(lapply(list(x, ...), function(y) as.number_line(y)@id))
gid <- unlist(lapply(list(x, ...), function(y) as.number_line(y)@gid))
zi <- unlist(list(x, ...))

methods::new("number_line", .Data = zi, id = id, gid = gid, start= ai)

})

#' @rdname number_line-class
#' @export
unique.number_line <- function(x, ...){
db <- unique(data.frame(l = left_point(x), r = right_point(x), ob = x))
db$cri <- paste(as.numeric(db$l), as.numeric(db$r),sep="")
db <- subset(db, !duplicated(db$cri))
x <- db$ob
return(x)
}

#' @rdname number_line-class
#' @param decreasing logical. Should the sort be increasing or decreasing
#' @export
sort.number_line <- function(x, decreasing = FALSE, ...){
db <- data.frame(sd = as.numeric(diyar::start_point(x)), id = x@id, nl= x)

if(decreasing) db$sd <- -db$sd
x <- db[order(db$sd, db$id),]$nl
return(x)
}

#' @rdname number_line-class
#' @export
format.number_line <- function(x, ...){
x <- x[1:length(x@start)]
s <- ifelse(x@start + x@.Data > x@start, "->","<-")
s <- ifelse(x@start + x@.Data == x@start, "==",s)
s <- ifelse(!is.finite(x@start + x@.Data) , "??",s)

paste(x@start, s, x@start + x@.Data, sep=" ")
}

#' @name epid-class
#' @title \code{epid} object
#'
#' @description
#' An S4 object to store the results of \code{\link{fixed_episodes}}, \code{\link{rolling_episodes}} and \code{\link{episode_group}}
#'
#' @aliases epid-class
#' @importFrom "methods" "new"
#' @importFrom "utils" "head"
#' @export
setClass("epid", contains = "numeric", representation(sn = "numeric", case_nm= "character", epid_interval = "number_line",
epid_length= "ANY", epid_total = "numeric", epid_dataset ="character"))

#' @rdname epid-class
#' @export
format.epid <- function(x, ...){
paste("E-",formatC(x@.Data, width= nchar(max(x@.Data)), flag="0"), ifelse(is.na(x@epid_interval),"", paste(" ",format.number_line(x@epid_interval),sep="")), " (", substr(x@case_nm,1,1), ")", sep="")
}

#' @rdname epid-class
#' @export
unique.epid <- function(x, ...){
db <- unique(data.frame(c = x@case_nm, ob = x))
db <- subset(db, db$c=="Case")
x <- db$ob
return(x)
}

#' @rdname epid-class
#' @param object object
setMethod("show", signature(object="epid"), function(object){
print(format.epid(object))
})

#' @rdname epid-class
#' @param x x
#' @param ... ...
setMethod("rep", signature(x = "epid"), function(x, ...) {
methods::new("epid", rep(x@.Data, ...), case_nm = rep(x@case_nm, ...), epid_interval = rep(x@epid_interval, ...),
epid_length = rep(x@epid_length, ...), epid_total = rep(x@epid_total, ...), epid_dataset = rep(x@epid_dataset, ...))
})

#' @aliases [,epid-method
#' @rdname epid-class
#' @param i i
#' @param j j
#' @param drop drop
setMethod("[", signature(x = "epid"),
function(x, i, j, ..., drop = TRUE) {
methods::new("epid", x@.Data[i], case_nm = x@case_nm[i], sn = x@sn[i],
epid_length = x@epid_length[i], epid_total = x@epid_total[i], epid_dataset = x@epid_dataset[i],
epid_interval = x@epid_interval[i])
})

#' @aliases [[,epid-method
#' @rdname epid-class
#' @param exact exact
setMethod("[[", signature(x = "epid"),
function(x, i, j, ..., exact = TRUE) {
methods::new("epid", x@.Data[i], case_nm = x@case_nm[i], sn = x@sn[i],
epid_length = x@epid_length[i], epid_total = x@epid_total[i], epid_dataset = x@epid_dataset[i],
epid_interval = x@epid_interval[i])
})

#' @name pid-class
#'
#' @title \code{pid} objects
#'
#' @description
#' An S4 object to store the results of \code{\link{record_group}}
#'
#' @aliases pid-class
#' @importFrom "methods" "new"
#' @importFrom "utils" "head"
#' @export
setClass("pid", contains = "numeric", representation(sn = "numeric", pid_cri= "numeric",
pid_dataset ="character", pid_total = "numeric"))

#' @rdname pid-class
#' @export
format.pid <- function(x, ...){

paste("P-", formatC(x@.Data, width= nchar(max(x@.Data)), flag="0"), " (",
ifelse(x@pid_cri==0,"No Hit", paste("CRI ", formatC(x@pid_cri, width = 2, flag=0), sep="")),
")", sep="")
}

#' @rdname pid-class
#' @export
unique.pid <- function(x, ...){
db <- unique(data.frame(p = x@.Data, ob = x))
db <- subset(db, !duplicated(db$p))
x <- db$ob
return(x)
}

#' @rdname pid-class
#' @param object object
setMethod("show", signature(object="pid"), function(object){
print(format.pid(object))
})

#' @rdname pid-class
#' @param x x
#' @param ... ...
setMethod("rep", signature(x = "pid"), function(x, ...) {
methods::new("pid", rep(x@.Data, ...), sn = rep(x@sn, ...), pid_total = rep(x@pid_total, ...),
pid_dataset = rep(x@pid_dataset, ...), pid_cri = rep(x@pid_cri, ...))
})

#' @aliases [,pid-method
#' @rdname pid-class
#' @param i i
#' @param j j
#' @param drop drop
setMethod("[", signature(x = "pid"),
function(x, i, j, ..., drop = TRUE) {
methods::new("pid", x@.Data[i], pid_cri = x@pid_cri[i], sn = x@sn[i],
pid_total = x@pid_total[i], pid_dataset = x@pid_dataset[i])
})

#' @aliases [[,pid-method
#' @rdname pid-class
#' @param exact exact
setMethod("[[", signature(x = "pid"),
function(x, i, j, ..., exact = TRUE) {
methods::new("pid", x@.Data[i], pid_cri = x@pid_cri[i], sn = x@sn[i],
pid_total = x@pid_total[i], pid_dataset = x@pid_dataset[i])
})
Loading

0 comments on commit 28aa503

Please sign in to comment.