/
method.R
1294 lines (1145 loc) · 44.4 KB
/
method.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' @name lcMethod-estimation
#' @aliases latrend-procedure lcMethod-steps
#' @title Longitudinal cluster method (`lcMethod`) estimation procedure
#' @description Each longitudinal cluster method represented by a [lcMethod class][lcMethod-class] implements a series of standardized steps that produce the estimated method as its output.
#' These steps, as part of the estimation procedure, are executed by the [latrend()] function and other functions prefixed by *"latrend"* (e.g., [latrendRep()], [latrendBoot()], [latrendCV()]).
#' @section Estimation procedure:
#' The steps for estimating a `lcMethod` object are defined and executed as follows:
#' \enumerate{
#' \item [compose()]: Evaluate and finalize the method argument values.
#' \item [validate()]: Check the validity of the method argument values in relation to the dataset.
#' \item [prepareData()]: Process the training data for fitting.
#' \item [preFit()]: Prepare environment for estimation, independent of training data.
#' \item [fit()]: Estimate the specified method on the training data, outputting an object inheriting from `lcModel`.
#' \item [postFit()]: Post-process the outputted `lcModel` object.
#' }
#'
#' The result of the fitting procedure is an [lcModel-class] object that inherits from the `lcModel` class.
#' @examples
#' data(latrendData)
#' method <- lcMethodLMKM(Y ~ Time, id = "Id", time = "Time")
#' model <- latrend(method, data = latrendData)
#' summary(model)
#' @seealso [lcMethod-class] [latrend]
NULL
#' @export
#' @name lcMethod-class
#' @aliases lcMethod
#' @title lcMethod class
#' @description `lcMethod` objects represent the specification of a method for longitudinal clustering.
#' Furthermore, the object class contains the logic for estimating the respective method.
#'
#' You can specify a longitudinal cluster method through one of the method-specific constructor functions,
#' e.g., [lcMethodKML()], [lcMethodLcmmGBTM()], or [lcMethodDtwclust()].
#' Alternatively, you can instantiate methods through [methods::new()], e.g., by calling `new("lcMethodKML", response = "Value")`.
#' In both cases, default values are specified for omitted arguments.
#'
#' @section Method arguments:
#' An `lcMethod` objects represent the specification of a method with a set of configurable parameters (referred to as arguments).
#'
#' Arguments can be of any type.
#' It is up to the `lcMethod` implementation of [validate()] to ensure that the required arguments are present and are of the expected type.
#'
#' Arguments can have almost any name. Exceptions include the names `"data"`, `"envir"`, and `"verbose"`.
#' Furthermore, argument names may not start with a period (`"."`).
#'
#' Arguments cannot be directly modified, i.e., `lcMethod` objects are immutable.
#' Modifying an argument involves creating an altered copy through the [update.lcMethod] method.
#'
#' @section Implementation:
#' The base class `lcMethod` provides the logic for storing, evaluating, and printing the method parameters.
#'
#' Subclasses of `lcMethod` differ only in the [fitting procedure logic][lcMethod-estimation].
#'
#' To implement your own `lcMethod` subclass, you'll want to implement at least the following functions:
#' \itemize{
#' \item [fit()]: The main function for estimating your method.
#' \item [getName()]: The name of your method.
#' \item [getShortName()]: The abbreviated name of your method.
#' \item [getArgumentDefaults()]: Sensible default argument values to your method.
#' }
#'
#' For more complex methods, the additional functions as part of the [fitting procedure][lcMethod-estimation] will be of use.
#'
#' @details Because the `lcMethod` arguments may be unevaluated, argument retrieval functions such as `[[` accept an `envir` argument.
#' A default `environment` can be assigned or obtained from a `lcMethod` object using the `environment()` function.
#' @seealso [environment]
#' @slot arguments A `list` representing the arguments of the `lcMethod` object.
#' Arguments are not evaluated upon creation of the method object.
#' Instead, arguments are stored similar to a `call` object, and are only evaluated when a method is fitted.
#' Do not modify or access.
#' @slot sourceCalls A list of calls for tracking the original call after substitution.
#' Used for printing objects which require too many characters (e.g. ,function definitions, matrices).
#' Do not modify or access.
#' @examples
#' method <- lcMethodLMKM(Y ~ Time, id = "Id", time = "Time", nClusters = 2)
#' method
#'
#' method <- new("lcMethodLMKM", formula = Y ~ Time, id = "Id", time = "Time", nClusters = 2)
#'
#' # get argument names
#' names(method)
#'
#' # evaluate argument
#' method$nClusters
#'
#' # create a copy with updated nClusters argument
#' method3 <- update(method, nClusters = 3)
#' @family lcMethod implementations
#' @family lcMethod functions
setClass('lcMethod', slots = c(arguments = 'environment', sourceCalls = 'list'))
#. initialize ####
#' @title lcMethod initialization
#' @description Initialization of `lcMethod` objects, converting arbitrary arguments to arguments as part of an `lcMethod` object.
#' @param .Object The newly allocated `lcMethod` object.
#' @param ... Other method arguments.
#' @examples
#' new("lcMethodLMKM", formula = Y ~ Time, id = "Id", time = "Time")
setMethod('initialize', 'lcMethod', function(.Object, ...) {
.Object <- callNextMethod(.Object)
.defaults = getArgumentDefaults(.Object)
.exclude = getArgumentExclusions(.Object)
assert_that(
is.list(.defaults),
is_named(.defaults),
msg = sprintf(
'Implementation error for %s object: getArgumentDefaults() must return a named list',
class(.Object)[1]
)
)
assert_that(
is.character(.exclude),
msg = sprintf(
'Implementation error for %s object: getArgumentExclusions() must return character vector',
class(.Object)[1]
)
)
# drop arguments without defaults (empty symbols)
symMask = vapply(.defaults, is.symbol, FUN.VALUE = TRUE)
dropSymMask = vapply(.defaults[symMask], nchar, FUN.VALUE = 0) == 0
.defaults[which(symMask)[dropSymMask]] = NULL
# drop default arguments that should be excluded
defArgs = .defaults[setdiff(names(.defaults), .exclude)]
# process user arguments
mc = match.call.all()
userArgs = as.list(mc) %>%
.[nchar(names(.)) > 0]
userArgs[names(formals())] = NULL
if (any(hasName(userArgs, .exclude))) {
warning(
sprintf(
'arguments (%s) cannot be defined for this lcMethod class. These arguments will be ignored.',
paste0(intersect(.exclude, names(userArgs)), collapse = ', ')
)
)
}
args = modifyList(defArgs, userArgs, keep.null = TRUE)
# construct arguments environment
argEnv = list2env(rev(args), hash = FALSE)
.Object@arguments = argEnv
validObject(.Object)
.Object
})
#. validity ####
setValidity('lcMethod', function(object) {
errors = validate_that(
all(nchar(names(object)) > 0),
msg = 'lcMethod argument names cannot be empty'
)
errors = errors %c% validate_that(
!any(startsWith(names(object), '.')),
msg = sprintf(
'Cannot construct %s: lcMethod argument names cannot start with "."\nYou should rename argument(s) %s',
class(object)[1],
paste0('"', names(object)[startsWith(names(object), '.')], '"', collapse = ', ')
)
)
errors = errors %c% validate_that(!has_name(object, 'data'), msg = 'lcMethod argument name cannot be "data"')
errors = errors %c% validate_that(!has_name(object, 'envir'), msg = 'lcMethod argument name cannot be "envir"')
errors = errors %c% validate_that(!has_name(object, 'verbose'), msg = 'lcMethod argument name cannot be "verbose"')
if (isArgDefined(object, 'nClusters')) {
errors = errors %c% validate_that(is.scalar(object$nClusters))
errors = errors %c% validate_that(is.na(object$nClusters) || is.count(object$nClusters))
}
errors[errors != TRUE]
})
#. $ ####
#' @export
#' @name [[,lcMethod-method
#' @rdname indexy
#' @aliases $,lcMethod-method
#' @param name The argument name, as `character`.
#' @examples
#' method <- lcMethodLMKM(Y ~ Time, id = "Id", time = "Time", nClusters = 3)
#' method$nClusters # 3
setMethod('$', 'lcMethod', function(x, name) {
x[[name]]
})
#. [[ ####
#' @export
#' @rdname indexy
#' @title Retrieve and evaluate a lcMethod argument by name
#' @param x The `lcMethod` object.
#' @param i Name or index of the argument to retrieve.
#' @param eval Whether to evaluate the call argument (enabled by default).
#' @param envir The `environment` in which to evaluate the argument. This argument is only applicable when `eval = TRUE`.
#' @return The argument `call` or evaluation result.
#' @examples
#' m = lcMethodLMKM(Y ~ Time, id = "Id", time = "Time", nClusters = 5)
#' m[["nClusters"]] # 5
#'
#' k = 2
#' m = lcMethodLMKM(Y ~ Time, id = "Id", time = "Time", nClusters = k)
#' m[["nClusters", eval=FALSE]] # k
#' @family lcMethod functions
setMethod('[[', 'lcMethod', function(x, i, eval = TRUE, envir = NULL) {
envir = .selectEnvironment(x, parent.frame(3), envir)
if (is.character(i)) {
assert_that(has_name(x, i), msg = sprintf('method does not have an argument named "%s"', i))
arg = get(i, envir = x@arguments)
} else {
argName = names(x)[i]
assert_that(!is.na(argName), msg = sprintf('index "%s" exceeded argument name options', i))
arg = get(i, envir = x@arguments)
}
if (eval) {
# within-method scope
value = tryCatch({
eval(
arg,
envir = mget(setdiff(names(x@arguments), i), envir = x@arguments),
enclos = envir
)
}, error = function(e) {
tryCatch({
eval(arg, envir = envir)
}, error = function(e2) {
# try evaluation within package scope instead
tryCatch({
eval(arg, envir = parent.env(getNamespace(.packageName)))
}, error = function(e3) {
stop(
sprintf('error in evaluating lcMethod argument "%s" with expression "%s":\n\t%s',
i, deparse(e2$call), e2$message
)
)
})
})
})
} else {
value = arg
}
if (is.formula(value)) {
environment(value) = new.env()
}
return(value)
})
as.lcMethod = function(x, ..., envir = parent.frame()) {
assert_that(is.lcMethod(x) || is.character(x))
if (is.character(x)) {
assert_that(
methods::isClass(x),
msg = sprintf(
'Cannot instantiate lcMethod object of class "%1$s": Class "%1$s" is not defined',
class(x)[1]
)
)
assert_that(
methods::extends(x, 'lcMethod'),
msg = sprintf(
'Cannot instantiate object of class "%1$s" as lcMethod: "%1$s" does not inherit from lcMethod class',
class(x)[1]
)
)
x = new(x, ...)
} else {
assert_that(is_class_defined(x))
update(x, ..., envir = envir)
}
}
#' @exportS3Method base::as.character
as.character.lcMethod = function(x, ..., eval = FALSE, width = 40, prefix = '', envir = NULL) {
assert_that(
is.lcMethod(x),
is.flag(eval)
)
envir = .selectEnvironment(x, parent.frame(), envir)
if (isTRUE(eval)) {
x = evaluate.lcMethod(x, envir = envir)
}
arg2char = function(a) {
if (is.null(a)) {
'NULL'
} else if (is.character(a)) {
paste0('"', a, '"', collapse = ', ')
} else if (is.atomic(a)) {
paste0(as.character(a), collapse = ', ')
} else {
deparse(a) %>% paste0(collapse = '')
}
}
argNames = names(x)
chrValues = lapply(x@arguments, arg2char) %>% unlist()
assert_that(all(vapply(chrValues, length, FUN.VALUE = 0) == 1))
sourceMask = vapply(chrValues, nchar, FUN.VALUE = 0) > width &
argNames %in% names(x@sourceCalls)
chrSource = lapply(x@sourceCalls[argNames[sourceMask]], arg2char) %>% unlist()
chrValues[sourceMask] = paste0('`', chrSource, '`')
args = vapply(chrValues, strtrim, width = width, FUN.VALUE = '')
header = sprintf('%s specifying "%s"', class(x)[1], getName(x))
if (length(args) > 0) {
body = sprintf('%s%-16s%s', prefix, paste0(argNames, ':'), args)
} else {
body = 'no arguments'
}
c(header, body)
}
#' @exportS3Method base::as.list
#' @title Extract the method arguments as a list
#' @param x The `lcMethod` object.
#' @param ... Additional arguments.
#' @param args A `character vector` of argument names to select. Only available arguments are returned.
#' Alternatively, a `function` or `list` of `function`s, whose formal arguments will be selected from the method.
#' @param eval Whether to evaluate the arguments.
#' @param expand Whether to return all method arguments when `"..."` is present among the requested argument names.
#' @param envir The `environment` in which to evaluate the arguments. If `NULL`, the environment associated with the object is used. If not available, the `parent.frame()` is used.
#' @return A `list` with the argument `call`s or evaluated results depending on the value for `eval`.
#' @examples
#' data(latrendData)
#' method <- lcMethodLMKM(Y ~ Time, id = "Id", time = "Time")
#' as.list(method)
#'
#' as.list(method, args = c("id", "time"))
#'
#' if (require("kml")) {
#' method <- lcMethodKML("Y", id = "Id", time = "Time")
#' as.list(method)
#'
#' # select arguments used by kml()
#' as.list(method, args = kml::kml)
#'
#' # select arguments used by either kml() or parALGO()
#' as.list(method, args = c(kml::kml, kml::parALGO))
#' }
#' @family lcMethod functions
as.list.lcMethod = function(x, ..., args = names(x), eval = TRUE, expand = FALSE, envir = NULL) {
assert_that(
is.lcMethod(x),
is.flag(eval),
is.flag(expand)
)
envir = .selectEnvironment(x, parent.frame(), envir)
if (is.function(args)) {
argNames = formalArgs(args)
}
else if (is.list(args)) {
# functions special case
argNames = lapply(args, formalArgs) %>%
Reduce(union, .)
} else {
assert_that(is.character(args))
argNames = args
}
# filter arguments
if (isTRUE(expand) && '...' %in% argNames) {
selArgNames = argNames
} else {
selArgNames = intersect(argNames, names(x))
}
if (isTRUE(eval)) {
# full evaluation
method = evaluate.lcMethod(x, envir = envir)
} else {
method = x
}
as.list(method@arguments)[selArgNames]
}
#' @exportS3Method base::as.data.frame
#' @title Convert lcMethod arguments to a list of atomic types
#' @description Converts the arguments of a `lcMethod` to a named `list` of [atomic] types.
#' @inheritParams as.list.lcMethod
#' @param x `lcMethod` to be coerced to a `character` `vector`.
#' @param ... Additional arguments.
#' @param eval Whether to evaluate the arguments in order to replace expression if the resulting value is of a class specified in `evalClasses`.
#' @param nullValue Value to use to represent the `NULL` type. Must be of length 1.
#' @return A single-row `data.frame` where each columns represents an argument call or evaluation.
#' @family lcMethod functions
as.data.frame.lcMethod = function(x, ..., eval = TRUE, nullValue = NA, envir = NULL) {
assert_that(
is.lcMethod(x),
is.flag(eval),
length(nullValue) == 1
)
if (isTRUE(eval)) {
envir = .selectEnvironment(x, parent.frame(), envir)
evalClasses = c(
'NULL',
'logical',
'numeric',
'complex',
'integer',
'character',
'factor'
)
method = evaluate.lcMethod(x, classes = evalClasses, envir = envir)
} else {
method = x
}
argList = as.list(method, eval = FALSE)
dfList = lapply(argList, function(a) {
if (is.null(a)) {
nullValue
} else if (is.atomic(a)) {
if (length(a) > 1) {
deparse(a) %>% as.character() %>% paste0(collapse = '')
} else {
a
}
} else {
deparse(a) %>% paste0(collapse = '')
}
})
argValueLengths = vapply(dfList, length, FUN.VALUE = 0)
errMask = argValueLengths != 1
assert_that(
all(argValueLengths == 1),
msg = sprintf(
'as.data.frame.lcMethod(%s) requires all method arguments to be/evaluate to length 1.\nArguments that violate this requirement: %s',
class(x)[1],
paste0('"', names(argValueLengths)[errMask], '"(', argValueLengths[errMask], ')', collapse = ', ')
)
)
as.data.frame(dfList, stringsAsFactors = FALSE)
}
#' @noRd
#' @title Select the preferred environment
#' @description Returns envir if specified. Otherwise, returns environment(object) if specified. The defaultEnvir is returned when the former two are NULL.
#' @keywords internal
.selectEnvironment = function(object, defaultEnvir, envir) {
assert_that(
is.lcMethod(object),
is.null(defaultEnvir) || is.environment(defaultEnvir),
is.null(envir) || is.environment(envir)
)
if (!is.null(envir)) {
envir
} else if (!is.null(environment(object))) {
environment(object)
} else {
defaultEnvir
}
}
#. compose ####
#' @export
#' @rdname compose
#' @aliases compose,lcMethod-method
#' @title `lcMethod` estimation step: compose an lcMethod object
#' @description Note: this function should not be called directly, as it is part of the `lcMethod` [estimation procedure][lcMethod-estimation].
#' For fitting an `lcMethod` object to a dataset, use the [latrend()] function or [one of the other standard estimation functions][latrend-estimation].
#'
#' The `compose()` function of the `lcMethod` object evaluates and finalizes the `lcMethod` arguments.
#'
#' The default implementation returns an updated object with all arguments having been evaluated.
#' @param method The `lcMethod` object.
#' @param envir The `environment` in which the `lcMethod` should be evaluated
#' @return The evaluated and finalized `lcMethod` object.
#' @inheritSection lcMethod-estimation Estimation procedure
#' @section Implementation:
#' In general, there is no need to extend this method for a specific method, as all arguments are automatically evaluated by the `compose,lcMethod` method.
#'
#' However, in case there is a need to extend processing or to prevent evaluation of specific arguments (e.g., for handling errors), the method can be overridden for the specific `lcMethod` subclass.
#' \preformatted{
#' setMethod("compose", "lcMethodExample", function(method, envir = NULL) {
#' newMethod <- callNextMethod()
#' # further processing
#' return(newMethod)
#' })
#' }
#' @seealso [evaluate.lcMethod]
setMethod('compose', 'lcMethod', function(method, envir = NULL) {
evaluate.lcMethod(method, try = FALSE, envir = envir)
})
# . fit ####
#' @export
#' @rdname fit
#' @aliases fit,lcMethod-method
#' @title `lcMethod` estimation step: logic for fitting the method to the processed data
#' @description Note: this function should not be called directly, as it is part of the `lcMethod` [estimation procedure][lcMethod-estimation].
#' For fitting an `lcMethod` object to a dataset, use the [latrend()] function or [one of the other standard estimation functions][latrend-estimation].
#'
#' The `fit()` function of the `lcMethod` object estimates the model with the evaluated method specification, processed training data, and prepared environment.
#' @inheritParams preFit
#' @param envir The `environment` containing variables generated by [prepareData()] and [preFit()].
#' @section Implementation:
#' This method should be implemented for all `lcMethod` subclasses.
#'
#' \preformatted{
#' setMethod("fit", "lcMethodExample", function(method, data, envir, verbose) {
#' # estimate the model or cluster parameters
#' coefs <- FIT_CODE
#'
#' # create the lcModel object
#' new("lcModelExample",
#' method = method,
#' data = data,
#' model = coefs,
#' clusterNames = make.clusterNames(method$nClusters)
#' )
#' })
#' }
#' @inheritSection lcMethod-estimation Estimation procedure
setMethod('fit', 'lcMethod', function(method, data, envir, verbose) {
stop(
sprintf('method cannot be estimated because the fit() function is not implemented for lcMethod of class %1$s.
define the fit() method using:
\tsetMethod("fit", "%1$s", function(method, data, verbose) {
\t\t<your code returning a lcModel-extended class here>
\t})")', class(method)[1])
)
})
#' @exportS3Method stats::formula
#' @title Extract formula
#' @description Extracts the associated `formula` for the given distributional parameter.
#' @inheritParams as.list.lcMethod
#' @param x The `lcMethod` object.
#' @param ... Additional arguments.
#' @param what The distributional parameter to which this formula applies. By default, the formula specifies `"mu"`.
#' @return The `formula` for the given distributional parameter.
#' @examples
#' method <- lcMethodLMKM(Y ~ Time, id = "Id", time = "Time")
#' formula(method) # Y ~ Time
#' @family lcMethod functions
formula.lcMethod = function(x, what = 'mu', envir = NULL, ...) {
assert_that(
is.lcMethod(x),
is.scalar(what),
is.character(what)
)
envir = .selectEnvironment(x, parent.frame(), envir)
if (what == 'mu') {
f = x$formula
} else {
f = x[[paste0('formula.', what)]]
}
environment(f) = envir
f
}
#' @exportS3Method stats::getCall
getCall.lcMethod = function(x, ...) {
assert_that(is.lcMethod(x))
do.call(call, c(class(x)[1], eapply(x@arguments, enquote)))
}
#. getArgumentDefaults ####
#' @export
#' @rdname getArgumentDefaults
#' @aliases getArgumentDefaults,lcMethod-method
#' @description Returns the default arguments associated with the respective `lcMethod` subclass.
#' These arguments are automatically included into the `lcMethod` object during initialization.
#'
#' @section Implementation:
#' Although implementing this method is optional, it prevents users from
#' having to specify all arguments every time they want to create a method specification.
#'
#' In this example, most of the default arguments are defined as arguments of the function
#' `lcMethodExample`, which we can include in the list by calling [formals]. Copying the arguments from functions
#' is especially useful when your method implementation is based on an existing function.
#' \preformatted{
#' setMethod("getArgumentDefaults", "lcMethodExample", function(object) {
#' list(
#' formals(lcMethodExample),
#' formals(funFEM::funFEM),
#' extra = Value ~ 1,
#' tol = 1e-4,
#' callNextMethod()
#' )
#' })
#' }
#'
#' It is recommended to add `callNextMethod()` to the end of the list.
#' This enables inheriting the default arguments from superclasses.
#' @seealso [lcMethod]
#' @family lcMethod implementations
setMethod('getArgumentDefaults', 'lcMethod', function(object) {
set_names(list(), character(0))
})
#. getCitation ####
#' @rdname getCitation
#' @aliases getCitation,lcMethod-method
setMethod('getCitation', 'lcMethod', function(object, ...) {
utils::citation(package = 'base')[0]
})
#. getArgumentExclusions ####
#' @export
#' @rdname getArgumentExclusions
#' @aliases getArgumentExclusions,lcMethod-method
#' @section Implementation:
#' This function only needs to be implemented if you want to avoid users from specifying
#' redundant arguments or arguments that are set automatically or conditionally on other arguments.
#'
#' \preformatted{
#' setMethod("getArgumentExclusions", "lcMethodExample", function(object) {
#' c(
#' "doPlot",
#' "verbose",
#' callNextMethod()
#' )
#' })
#'
#' Adding `callNextMethod()` to the end of the return vector enables inheriting exclusions from superclasses.
#' }
#' @seealso [lcMethod] [getArgumentExclusions]
#' @family lcMethod implementations
setMethod('getArgumentExclusions', 'lcMethod', function(object) {
c('verbose', 'envir', 'data')
})
#. getLabel ####
#' @export
#' @rdname getLabel
#' @aliases getLabel,lcMethod-method
#' @description Extracts the assigned label from the given `lcMethod` or `lcModel` object.
#' By default, the label is determined from the `"label"` argument of the `lcMethod` object.
#' The label of an `lcModel` object is set upon estimation by [latrend()] to the label of its associated `lcMethod` object.
#' @seealso [getName] [getShortName]
#' @examples
#' method <- lcMethodLMKM(Y ~ Time, time = "Time")
#' getLabel(method) # ""
#'
#' getLabel(update(method, label = "v2")) # "v2"
setMethod('getLabel', 'lcMethod', function(object, ...) {
if (hasName(object, 'label')) {
object$label
} else {
''
}
})
#. getName ####
#' @export
#' @rdname getName
#' @aliases getName,lcMethod-method
#' @section Implementation:
#' When implementing your own `lcMethod` subclass, override these methods to provide full and abbreviated names.
#' \preformatted{
#' setMethod("getName", "lcMethodExample", function(object) "example name")
#'
#' setMethod("getShortName", "lcMethodExample", function(object) "EX")
#' }
#'
#' Similar methods can be implemented for your `lcModel` subclass,
#' however in practice this is not needed as the names are determined by default from the `lcMethod` object that was used to fit the `lcModel` object.
#'
#' @examples
#' method <- lcMethodLMKM(Y ~ Time)
#' getName(method) # "lm-kmeans"
setMethod('getName', 'lcMethod', function(object, ...) 'undefined')
#' @export
#' @rdname getName
#' @aliases getName,NULL-method
setMethod('getName', 'NULL', function(object, ...) 'null')
#. getShortName ####
#' @export
#' @rdname getName
#' @aliases getShortName,lcMethod-method
#' @examples
#' method <- lcMethodLMKM(Y ~ Time)
#' getShortName(method) # "LMKM"
setMethod('getShortName', 'lcMethod', function(object, ...) getName(object, ...))
#' @export
#' @rdname getName
#' @aliases getShortName,NULL-method
setMethod('getShortName', 'NULL', function(object, ...) 'nul')
#. idVariable ####
#' @export
#' @rdname idVariable
#' @aliases idVariable,lcMethod-method
#' @examples
#' method <- lcMethodLMKM(Y ~ Time, id = "Traj")
#' idVariable(method) # "Traj"
#'
setMethod('idVariable', 'lcMethod', function(object, ...) object$id)
#' @export
#' @title Check whether the argument of a lcMethod has a defined value.
#' @description Determines whether the associated argument value is defined. If the argument value is of type `language`, the argument is evaluated to see if it can be resolved within its `environment`.
#' @param object The `lcMethod` object.
#' @param name The name of the argument, as `character`.
#' @param envir The `environment` to evaluate the arguments in. If `NULL`, the argument is not evaluated.
#' @keywords internal
isArgDefined = function(object, name, envir = environment(object)) {
assert_that(
is.lcMethod(object),
is.character(name),
is.scalar(name),
is.environment(envir) || is.null(envir)
)
if (!hasName(object, name)) {
return(FALSE)
}
arg = object[[name[1], eval = FALSE]]
if (is.language(arg)) {
arg = try(object[[name[1], envir = envir]], silent = TRUE)
return(!is(arg, 'try-error'))
} else {
return(TRUE)
}
}
#' @export
#' @name latrend-is
#' @rdname is
#' @title Check if object is of Class
#' @param x The object to check the class of.
#' @return `scalar logical`
#' @keywords internal
is.lcMethod = function(x) {
isS4(x) && inherits(x, 'lcMethod')
}
#. length ####
#' @export
#' @name names,lcMethod-method
#' @rdname names-lcMethod-method
#' @aliases length,lcMethod-method
#' @return The number of arguments, as `scalar integer`.
setMethod('length', 'lcMethod', function(x) {
length(x@arguments)
})
#. names ####
#' @export
#' @title lcMethod argument names
#' @description Extract the argument names or number of arguments from an `lcMethod` object.
#' @param x The `lcMethod` object.
#' @return A `character vector` of argument names.
#' @examples
#' method <- lcMethodLMKM(Y ~ Time)
#' names(method)
#' length(method)
#' @family lcMethod functions
setMethod('names', 'lcMethod', function(x) {
argNames = names(x@arguments)
if (is.null(argNames)) {
character(0)
} else {
argNames
}
})
# . preFit ####
#' @rdname preFit
#' @aliases preFit,lcMethod-method
#' @title `lcMethod` estimation step: method preparation logic
#' @description Note: this function should not be called directly, as it is part of the `lcMethod` [estimation procedure][lcMethod-estimation].
#' For fitting an `lcMethod` object to a dataset, use the [latrend()] function or [one of the other standard estimation functions][latrend-estimation].
#'
#' The `preFit()` function of the `lcMethod` object performs preparatory work that is needed for fitting the method but should not be counted towards the method estimation time.
#' The work is added to the provided `environment`, allowing the [fit()] function to make use of the prepared work.
#' @inheritParams prepareData
#' @param envir The `environment` containing additional data variables returned by [prepareData()].
#' @section Implementation:
#' \preformatted{
#' setMethod("preFit", "lcMethodExample", function(method, data, envir, verbose) {
#' # update envir with additional computed work
#' envir$x <- INTENSIVE_OPERATION
#' return(envir)
#' })
#' }
#' @inheritSection lcMethod-estimation Estimation procedure
#' @return The updated `environment` that will be passed to [fit()].
setMethod('preFit', 'lcMethod', function(method, data, envir, verbose) {
envir
})
# . prepareData ####
#' @rdname prepareData
#' @aliases prepareData,lcMethod-method
#' @title `lcMethod` estimation step: logic for preparing the training data
#' @description Note: this function should not be called directly, as it is part of the `lcMethod` [estimation procedure][lcMethod-estimation].
#' For fitting an `lcMethod` object to a dataset, use the [latrend()] function or [one of the other standard estimation functions][latrend-estimation].
#'
#' The `prepareData()` function of the `lcMethod` object processes the training data prior to fitting the method.
#' Example uses:
#' \itemize{
#' \item Transforming the data to another format, e.g., a matrix.
#' \item Truncating the response variable.
#' \item Computing derived covariates.
#' \item Creating additional data objects.
#' }
#' The computed variables are stored in an `environment` which is passed to the [preFit()] function for further processing.
#'
#' By default, this method does not do anything.
#'
#' @inheritParams validate
#' @param verbose A [R.utils::Verbose] object indicating the level of verbosity.
#' @return An `environment` with the prepared data variable(s) that will be passed to [preFit()].
#' @section Implementation:
#' A common use case for this method is when the internal method fitting procedure expects the data in a different format.
#' In this example, the method converts the training data `data.frame` to a `matrix` of repeated and aligned trajectory measurements.
#' \preformatted{
#' setMethod("prepareData", "lcMethodExample", function(method, data, verbose) {
#' envir = new.env()
#' # transform the data to matrix
#' envir$dataMat = tsmatrix(data,
#' id = idColumn, time = timeColumn, response = valueColumn)
#' return(envir)
#' })
#' }
#' @inheritSection lcMethod-estimation Estimation procedure
setMethod('prepareData', 'lcMethod', function(method, data, verbose) {
new.env(parent = emptyenv())
})
# . postFit ####
#' @rdname postFit
#' @aliases postFit,lcMethod-method
#' @title `lcMethod` estimation step: logic for post-processing the fitted lcModel
#' @description Note: this function should not be called directly, as it is part of the `lcMethod` [estimation procedure][lcMethod-estimation].
#' For fitting an `lcMethod` object to a dataset, use the [latrend()] function or [one of the other standard estimation functions][latrend-estimation].
#'
#' The `postFit()` function of the `lcMethod` object defines how the `lcModel` object returned by [fit()] should be post-processed.
#' This can be used, for example, to:
#' \itemize{
#' \item Resolve label switching.
#' \item Clean up the internal model representation.
#' \item Correct estimation errors.
#' \item Compute additional metrics.
#' }
#' By default, this method does not do anything. It merely returns the original `lcModel` object.
#'
#' This is the last step in the `lcMethod` fitting procedure. The `postFit` method may be called again on fitted `lcModel` objects, allowing post-processing to be updated for existing models.
#'
#' @inheritParams fit
#' @param model The `lcModel` object returned by [fit()].
#' @return The updated `lcModel` object.
#' @section Implementation:
#' The method is intended to be able to be called on previously fitted `lcModel` objects as well, allowing for potential bugfixes or additions to previously fitted models.
#' Therefore, when implementing this method, ensure that you do not discard information from the model which would prevent the method from being run a second time on the object.
#'
#' In this example, the `lcModelExample` class is assumed to be defined with a slot named `"centers"`:
#' \preformatted{
#' setMethod("postFit", "lcMethodExample", function(method, data, model, envir, verbose) {
#' # compute and store the cluster centers
#' model@centers <- INTENSIVE_COMPUTATION
#' return(model)
#' })
#' }
#' @inheritSection lcMethod-estimation Estimation procedure
setMethod('postFit', 'lcMethod', function(method, data, model, envir, verbose) {
model
})
#' @exportS3Method base::print
#' @title Print the arguments of an lcMethod object
#' @param x The `lcMethod` object.
#' @param eval Whether to print the evaluated argument values.
#' @param width Maximum number of characters per argument.
#' @param envir The environment in which to evaluate the arguments when `eval = TRUE`.
#' @param ... Not used.
print.lcMethod = function(x, ..., eval = FALSE, width = 40, envir = NULL) {
out = as.character(x, ..., eval = eval, width = width, envir = envir, prefix = ' ')
cat(out, sep = '\n')
}
#' @exportS3Method R.utils::evaluate
#' @importFrom R.utils evaluate
#' @title Substitute the call arguments for their evaluated values
#' @description Substitutes the call arguments if they can be evaluated without error.
#' @inheritParams as.list.lcMethod
#' @param object The `lcMethod` object.
#' @param classes Substitute only arguments with specific class types. By default, all types are substituted.
#' @param try Whether to try to evaluate arguments and ignore errors (the default), or to fail on any argument evaluation error.
#' @param exclude Arguments to exclude from evaluation.
#' @param ... Not used.
#' @return A new `lcMethod` object with the substituted arguments.
#' @seealso [compose]
#' @family lcMethod functions
evaluate.lcMethod = function(
object,
classes = 'ANY',
try = TRUE,
exclude = character(),
envir = NULL,
...
) {
rawObject = as.lcMethod(object)
assert_that(is.character(classes))
envir = .selectEnvironment(rawObject, parent.frame(), envir)
argNames = names(rawObject)
if (isTRUE(try)) {
evalMask = vapply(
argNames,
isArgDefined,
object = rawObject,
envir = envir,
FUN.VALUE = FALSE
) & !(argNames %in% exclude)
} else {
evalMask = !(argNames %in% exclude)
}
evalValues = vector(mode = 'list', length = length(rawObject))
evalValues[evalMask] = lapply(
argNames[evalMask],
function(name) rawObject[[name, eval = TRUE, envir = envir]]
)
if ('ANY' %in% classes) {
updateMask = evalMask
} else {
updateMask = evalMask &
vapply(evalValues, function(x) class(x)[1], FUN.VALUE = '') %in% classes
}
newObject = rawObject
sourceMask = vapply(newObject@arguments, is.language, FUN.VALUE = FALSE)
sourceNames = argNames[updateMask & sourceMask]
newObject@sourceCalls[sourceNames] = mget(sourceNames, newObject@arguments)
updateNames = argNames[updateMask]
updateValues = evalValues[updateMask]
for (i in seq_along(updateNames)) {
assign(updateNames[i], updateValues[[i]], pos = rawObject@arguments)
}
# newObject@arguments = replace(object@arguments, names(object)[updateMask], evalValues[updateMask])
return(newObject)
}
#' @export