plotContWeights: Plots mean and individual weight trajectories.

Description Usage Arguments Details Author(s) Examples

View source: R/plotContWeights.R

Description

Plots weights for assessment.

Usage

1
plotContWeights(fr, stopTimeName = "to", startStatusName = "from.state", endStatusName = "to.state", idName = "id")

Arguments

fr

Data with weight column

stopTimeName

Name of column with stop time of the at risk period,

startStatusName

Name of the variable that contains the name of start state for each at-risk interval,

endStatusName

Name of the variable that contains the name of end state for each at-risk interval,

idName

Name of column in dataFr that identifies individuals,

Details

The mean should be close to 1, and the individual trajectories should not be extreme(i.e. negative or large).

Author(s)

Pål Christie Ryalen <p.c.ryalen@medisin.uio.no>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (fr, stopTimeName = "to", startStatusName = "from.state", 
    endStatusName = "to.state", idName = "id") 
{
    namesMatch <- match(c(startStatusName, endStatusName, stopTimeName, 
        idName), names(fr))
    names(fr)[namesMatch] <- c("from.state", "to.state", "to", 
        "id")
    fr <- as.data.table(fr)
    tms <- sort(unique(fr$to))
    tmx <- max(tms)
    idNs <- unique(fr$id)
    numIds <- length(idNs)
    fr[, `:=`(weights, c(weights[-1], weights[length(weights)])), 
        by = id]
    wtIn <- fr$weights[fr$id == idNs[1]]
    wt <- rep(NA, length(tms))
    wt[1] <- 1
    wt[match(fr[id == idNs[1]]$to, tms)] <- wtIn
    wt <- naReplace(wt)
    wpr <- wt
    ylm <- c(max(c(0, min(fr$weights))), min(c(5, max(fr$weights))))
    plot(tms, wt, type = "l", xlim = c(0, tmx), ylim = ylm, col = "grey", 
        xlab = "time", ylab = "weights")
    for (i in 2:numIds) {
        wtIn <- fr$weights[fr$id == idNs[i]]
        wt <- rep(NA, length(tms))
        wt[1] <- 1
        wt[match(fr[id == idNs[i]]$to, tms)] <- wtIn
        wt <- naReplace(wt)
        lines(tms, wt, col = "grey")
        if (any(is.na(wt))) 
            break
        wpr <- wpr + wt
    }
    wpr <- wpr/numIds
    lines(tms, wpr, col = "red")
    cat("Mean weights: ", mean(wpr))
  }

palryalen/ahw documentation built on Sept. 29, 2021, 6:20 p.m.