-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.R
50 lines (41 loc) · 2.96 KB
/
Functions.R
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
44
45
46
47
48
49
50
#---------------------------------------------------------------------------#
#### R function to sample states for multiple individuals simultaneously ####
#---------------------------------------------------------------------------#
# - Krijkamp EM, Alarid-Escudero F, Enns EA, Jalal HJ, Hunink MGM, Pechlivanoglou P.
# Microsimulation modeling for health decision sciences using R: A tutorial.
# Med Decis Making. 2018;38(3):400–22. https://www.ncbi.nlm.nih.gov/pubmed/29587047
#####################################################################################
# Copyright 2017, THE HOSPITAL FOR SICK CHILDREN AND THE COLLABORATING INSTITUTIONS.
# All rights reserved in Canada, the United States and worldwide.
# Copyright, trademarks, trade names and any and all associated intellectual property
# are exclusively owned by THE HOSPITAL FOR SICK CHILDREN and the collaborating
# institutions and may not be used, reproduced, modified, distributed or adapted
# in any way without appropriate citation.
#####################################################################################
# Developed by Petros Pechlivanoglou
samplev <- function(m.Probs, m) {
# Arguments
# m.Probs: matrix with probabilities (n.i * n.s)
# m: number of states than need to be sampled per individual
# Return
# ran: n.i x m matrix filled with sampled health state(s) per individual
d <- dim(m.Probs) # dimensions of the matrix filled with the multinomical probabilities for the health states
n <- d[1] # first dimension - number of rows (number of individuals to sample for)
k <- d[2] # second dimension - number of columns (number of health states considered)
lev <- dimnames(m.Probs)[[2]] # extract the names of the health states considered for sampling
if (!length(lev)) # in case names for the health states are missing, use numbers to specify the health states
lev <- 1:k # create a sequence from 1:k (number of health states considered)
# create a matrix
ran <- matrix(lev[1], ncol = m, nrow = n) # create the matrix ran, filled with the first health state of the levels
U <- t(m.Probs) # transposed m.Probs matrix n.i x n.s --> n.s x n.i
for(i in 2:k) { # start loop, from the 2nd health states
U[i, ] <- U[i, ] + U[i - 1, ] # start summing the probabilities of the different health states per individual
}
if (any((U[k, ] - 1) > 1e-05)) # sum of all probs per individual - 1 should be 0 (use 1e-05 for rounding issues), else print the error statement
stop("error in multinom: probabilities do not sum to 1")
for (j in 1:m) { # start loop of the state that needs to be sampled (m)
un <- rep(runif(n), rep(k, n)) # sample from a uniform distribution of length n*k
ran[, j] <- lev[1 + colSums(un > U)] # store the health state at the jth column of the U matrix
}
ran # return the new health state per individual n.i x m
} # close the function