/
cov.R
170 lines (165 loc) · 4.59 KB
/
cov.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
.setCov <- function(obj, ...) {
.pt <- proc.time()
.env <- obj
if (rxode2::rxIs(obj, "nlmixr2FitData")) {
.env <- obj$env
}
if (exists("cov", .env)) {
.cur <- list(.env$cov)
names(.cur) <- .env$covMethod
if (exists("covList", .env)) {
if (is.null(.env$covList[[.env$covMethod]])) {
.covList <- c(.env$covList, .cur)
} else {
.covList <- .env$covList
}
} else {
.covList <- .cur
}
assign("covList", .covList, .env)
}
.control <- .env$foceiControl
.control$maxInnerIterations <- 0L
.control$maxOuterIterations <- 0L
.control$boundTol <- 0 # turn off boundary
.control$calcTables <- FALSE
.lst <- list(...)
.env2 <- new.env(parent = emptyenv())
for (.n in names(.lst)) {
.control[[.n]] <- .lst[[.n]]
}
if (!is.null(.lst$covMethod)) {
if (rxode2::rxIs(.lst$covMethod, "character")) {
.lst$covMethod <- match.arg(.lst$covMethod, c("r,s", "r", "s"))
.covMethodIdx <- c("r,s" = 1L, "r" = 2L, "s" = 3L)
.control$covMethod <- .covMethodIdx[.lst$covMethod]
} else if (inherits(.lst$covMethod, "matrix")) {
.env2$cov <- as.matrix(.lst$covMethod)
.env2$ui <- obj$ui
.control$covMethod <- 0L
} else if (length(.lst$covMethod) == 1) {
if (.lst$covMethod == "") {
.control$covMethod <- 0L
}
}
} else if (.control$covMethod == 0L) {
.control$covMethod <- 1L
}
## covDerivMethod=c("central", "forward"),
if (!is.null(.lst$hessEps)) {
.control$hessEps <- .lst$hessEps
.lst$hessEps <- NULL
}
if (!is.null(.lst$gillKcov)) {
.control$gillKcov <- .lst$gillKcov
.lst$gillKcov <- NULL
}
if (!is.null(.lst$gillStepCov)) {
.control$gillStepCov <- .lst$gillStepCov
.lst$gillStepCov <- NULL
}
if (!is.null(.lst$gillFtolCov)) {
.control$gillFtolCov <- .lst$gillFtolCov
.lst$gillFtolCov <- NULL
}
if (!is.null(.lst$rmatNorm)) {
.control$rmatNorm <- .lst$rmatNorm
.lst$rmatNorm <- NULL
}
if (!is.null(.lst$smatNorm)) {
.control$smatNorm <- .lst$smatNorm
.lst$smatNorm <- NULL
}
if (!is.null(.lst$covGillF)) {
.control$covGillF <- .lst$covGillF
.lst$covGillF <- NULL
}
if (!is.null(.lst$covSmall)) {
.control$covSmall <- .lst$covSmall
.lst$covSmall <- NULL
}
.dat <- getData(obj)
.ui <- obj$ui
.mat <- as.matrix(nlme::random.effects(obj)[, -1])
.control$skipCov <- obj$skipCov
.control$etaMat <- .mat
.fit2 <- nlmixr2CreateOutputFromUi(.ui, data=.dat, control=.control,
table=.env$table,env=.env2, est="none")
.env$cov <- .fit2$cov
.env$parFixedDf <- .fit2$parFixedDf
.env$parFixed <- .fit2$parFixed
.env$covMethod <- .fit2$covMethod
#.updateParFixed(.env)
.parent <- parent.frame(2)
.bound <- do.call("c", lapply(ls(.parent), function(.cur) {
if (identical(.parent[[.cur]], obj)) {
return(.cur)
}
return(NULL)
}))
message(paste0("Updated original fit object ", ifelse(is.null(.bound), "", crayon::yellow(.bound))))
.env$time$covariance <- (proc.time() - .pt)["elapsed"]
return(.env$cov)
}
#' Set the covariance type based on prior calculated covariances
#'
#' @param fit nlmixr2 fit
#' @param method covariance method (see the `covMethod` argument for the control
#' options for the choices)
#' @return Fit object with covariance updated
#' @author Matt Fidler
#' @seealso \code{\link{foceiControl}()}, \code{\link{saemControl}()}
#' @export
setCov <- function(fit, method) {
.pt <- proc.time()
.env <- fit
if (rxode2::rxIs(fit, "nlmixr2FitData")) {
.env <- fit$env
}
if (method == .env$covMethod) {
stop("no need to switch covariance methods, already set to '",
method,
"'",
call. = FALSE
)
}
if (exists("covList", .env)) {
.covList <- .env$covList
.cov <- .covList[[method]]
if (!is.null(.cov)) {
.setCov(fit, covMethod = .cov)
.env$covMethod <- method
return(invisible(fit))
}
}
stop("different covariance types have not been calculated",
call. = FALSE
)
}
##' @export
getVarCov.nlmixr2FitCore <- function(obj, ...) {
.env <- obj
if (rxode2::rxIs(obj, "nlmixr2FitData")) {
.env <- obj$env
}
.force <- FALSE
.args <- list(...)
if (!is.null(.args$force)) {
.force <- .args$force
}
if (exists("cov", envir = .env) && !.force) {
if (rxode2::rxIs(.env$cov, "matrix")) {
return(.env$cov)
}
}
.setCov(obj, ...)
}
##' @export
getVarCov.nlmixr2FitCoreSilent <- getVarCov.nlmixr2FitCore
.cov2cor <- function(cov) {
.sd2 <- sqrt(diag(cov))
.cor <- stats::cov2cor(cov)
dimnames(.cor) <- dimnames(cov)
diag(.cor) <- .sd2
.cor
}