-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeqDBApp.R
More file actions
53 lines (53 loc) · 1.96 KB
/
SeqDBApp.R
File metadata and controls
53 lines (53 loc) · 1.96 KB
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
51
52
53
# Sequential DB function for shiny
#
# Small changes in alt.cte argument in comparison with the \code{SeqDB}
# function. This way the function can be easily used in the SurveyApp function
SeqDBApp <- function(des, cand.set, n.alts, par.draws, prior.covar, alt.cte, reduce = TRUE, w = NULL) {
# Initialize.
n.sets <- nrow(des) / n.alts
# If no w, equal w.
if (is.null(w)) {
w <- rep(1, nrow(par.draws))
}
# Create alternative specific design if necessay.
if (!all(alt.cte == 0)) {
cte.set <- Altspec(alt.cte = alt.cte, n.sets = 1)
cte.des <- Altspec(alt.cte = alt.cte, n.sets = n.sets)
kcte <- ncol(cte.des)
fdes <- cbind(cte.des, des)
} else {
cte.set <- NULL
kcte <- 0
fdes <- des
}
# Error handling cte.des
if (ncol(cand.set) + kcte != ncol(par.draws)) {
stop("dimension of par.draws does not match the dimension of alt.cte + cand.set.")
}
# Handling par.draws.
if (!(is.matrix(par.draws))) {
par.draws <- matrix(par.draws, nrow = 1)
}
# Error identifying model.
if (n.sets < ncol(par.draws)) {
stop("Model is unidentified. Increase the number of choice sets or decrease parameters to estimate.")
}
# Error par.draws
if (ncol(fdes) != ncol(par.draws)) {
stop("Numbers of parameters in par.draws does not match the number of parameters in the design.")
}
# Starting and initializing values.
i.cov <- solve(prior.covar)
d.start <- apply(par.draws, 1, Derr, des = fdes, n.alts = n.alts)
db.start <- mean(d.start, na.rm = TRUE)
full.comb <- gtools::combinations(n = nrow(cand.set), r = n.alts, repeats.allowed = !reduce)
n.par <- ncol(par.draws)
# For each potential set, select best.
db.errors <- apply(full.comb, 1, DBerrS, cand.set, par.draws, fdes, n.alts, cte.set, i.cov, n.par, w)
comb.nr <- as.numeric(full.comb[which.min(db.errors), ])
set <- cand.set[comb.nr, ]
row.names(set) <- NULL
db <- min(db.errors)
#return best set and db error design.
return(list(set = set, db.error = db))
}