/
methodMixtoolsGMM.R
131 lines (114 loc) · 3.34 KB
/
methodMixtoolsGMM.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
#' @include method.R
#' @name interface-mixtools
#' @rdname interface-mixtools
#' @title mixtools interface
#' @seealso [lcMethodMixtoolsGMM] [lcMethodMixtoolsNPRM] \link[mixtools]{regmixEM.mixed} \link[mixtools]{npEM}
#' @keywords internal
NULL
setClass('lcMethodMixtoolsGMM', contains = 'lcMethod')
#' @export
#' @title Specify mixed mixture regression model using mixtools
#' @inheritParams lcMethodGCKM
#' @param ... Arguments passed to [mixtools::regmixEM.mixed].
#' The following arguments are ignored: data, y, x, w, k, addintercept.fixed, verb.
#' @examples
#' \donttest{
#' data(latrendData)
#'
#' if (require("mixtools")) {
#' method <- lcMethodMixtoolsGMM(
#' formula = Y ~ Time + (1 | Id),
#' id = "Id", time = "Time",
#' nClusters = 3,
#' arb.R = FALSE
#' )
#' }
#' }
#' @family lcMethod implementations
#' @references
#' \insertRef{benaglia2009mixtools}{latrend}
lcMethodMixtoolsGMM = function(
formula,
time = getOption('latrend.time'),
id = getOption('latrend.id'),
nClusters = 2,
...
) {
mc = match.call.all()
mc$Class = 'lcMethodMixtoolsGMM'
do.call(new, as.list(mc))
}
#' @rdname interface-mixtools
setMethod('getArgumentDefaults', 'lcMethodMixtoolsGMM', function(object) {
c(
formals(lcMethodMixtoolsGMM),
formals(mixtools::regmixEM.mixed),
callNextMethod()
)
})
#' @rdname interface-mixtools
setMethod('getArgumentExclusions', 'lcMethodMixtoolsGMM', function(object) {
union(
callNextMethod(),
c('data', 'y', 'x', 'w', 'k', 'addintercept.fixed', 'verb')
)
})
#' @rdname interface-mixtools
setMethod('getCitation', 'lcMethodMixtoolsGMM', function(object, ...) {
citation('mixtools')
})
#' @rdname interface-mixtools
#' @inheritParams getName
setMethod('getName', 'lcMethodMixtoolsGMM', function(object) 'growth mixture modeling using mixtools')
#' @rdname interface-mixtools
setMethod('getShortName', 'lcMethodMixtoolsGMM', function(object) 'gmm')
#' @rdname interface-mixtools
setMethod('preFit', 'lcMethodMixtoolsGMM', function(method, data, envir, verbose, ...) {
e = new.env()
# Parse formula
f = formula(method)
valueColumn = responseVariable(method)
id = idVariable(method)
e$fixed = dropRE(f)
reTerms = getREterms(f)
if (length(reTerms) > 0) {
e$random = reTerms[[1]] %>% REtermAsFormula
} else {
stop('no random effects specified')
}
# Response
e$y = split(data[[valueColumn]], data[[id]])
# Fixed effects
W = model.matrix(e$fixed, data = data)
e$w = as.data.frame(W) %>%
split(data[[id]]) %>%
lapply(as.matrix)
# Random effects
X = model.matrix(e$random, data = data)
e$x = as.data.frame(X) %>%
split(data[[id]]) %>% #split() outputs a vector for matrix input..
lapply(as.matrix)
return(e)
})
#' @rdname interface-mixtools
#' @inheritParams fit
setMethod('fit', 'lcMethodMixtoolsGMM', function(method, data, envir, verbose, ...) {
args = as.list(method, args = mixtools::regmixEM.mixed)
args$y = envir$y
args$x = envir$x
args$w = envir$w
args$k = method$nClusters
args$addintercept.fixed = FALSE
args$addintercept.random = FALSE
args$verb = canShow(verbose, 'fine')
model = do.call(mixtools::regmixEM.mixed, args)
model$fixed = envir$fixed
model$random = envir$random
new(
'lcModelMixtoolsGMM',
method = method,
data = data,
model = model,
clusterNames = make.clusterNames(method$nClusters)
)
})