-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathace-estimate.R
139 lines (132 loc) · 5.3 KB
/
ace-estimate.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
#' Class AceEstimate
#'
#' @description A class containing information about a single univariate ACE model.
#'
#' @name AceEstimate-class
#'
#' @docType class
#'
#' @note The contents of the `Details` list depends on the underlying
#' estimation routine. For example, when the ACE model is estimated with a DF
#' analysis, the output is an [stats::lm()] object, because the [stats::lm()] function
#' was used (i.e., the basic general linear model). Alternatively, if the
#' user specified the [lavaan::lavaan()] package should estimate that ACE model,
#' the output is a [lavaan::lavaan()] object.
#'
#' @section Objects from the Class: Objects can be created by calls of the
#' form:
#' `new("AceEstimate", aSquared, cSquared, eSquared, caseCount, unity, withinBounds, details, ...)`
#'
#' @keywords classes ACE
#'
#' @examples
#' library(NlsyLinks) # Load the package into the current R session.
#'
#' showClass("AceEstimate")
#' est <- CreateAceEstimate(.5, .2, .3, 40)
#' est
#' print(est)
#'
methods::setClass(
Class = "AceEstimate",
representation = representation(
ASquared = "numeric",
CSquared = "numeric",
ESquared = "numeric", # , CategoryStats="matrix"
CaseCount = "numeric",
# CaseCount="integer",
Unity = "logical",
WithinBounds = "logical",
Details = "list"
),
validity = function(object) {
# print( object@ASquared )
return(TRUE)
}
)
methods::setMethod(
f = "initialize",
signature = signature(.Object = "AceEstimate"), # signature="AceEstimate",
definition = function(.Object, aSquared, cSquared, eSquared, caseCount, unity, withinBounds, details, ...) {
if (missing(eSquared)) eSquared <- 1 - (aSquared + cSquared)
# print(missing(caseCount))
# if( is.na(caseCount) ) caseCount <- 444
# print(caseCount)
.Object@ASquared <- aSquared
.Object@CSquared <- cSquared
.Object@ESquared <- eSquared
.Object@CaseCount <- caseCount
.Object@Unity <- unity
.Object@WithinBounds <- withinBounds
.Object@Details <- details
return(.Object)
# callNextMethod(.Object, ASquared=aSquared, CSquared=cSquared, ESquared=eSquared, CaseCount=caseCount, Unity=unity, WithinBounds=withinBounds,...)
}
)
CreateAceEstimate <- function(aSquared, cSquared, eSquared, caseCount, details = list(), unityTolerance = 1e-11) {
componentSum <- aSquared + cSquared + eSquared
# print(class(caseCount))
if (missing(caseCount)) stop("The argument 'caseCount' is missing.")
# else if( class(caseCount) != "numeric" ) stop(paste0("The argument 'caseCount' should be class 'numeric', but was '", class(caseCount), "'."))
unity <- (abs(componentSum - 1.0) < unityTolerance)
withinBounds <- (0 <= min(aSquared, cSquared, eSquared)) && (max(aSquared, cSquared, eSquared) <= 1)
# withinBounds <- (0 <= min(aSquared, cSquared, eSquared)) & (max( aSquared, cSquared, eSquared) <= 1)
return(new("AceEstimate", aSquared, cSquared, eSquared, caseCount, unity, withinBounds, details))
}
methods::setMethod(f = "print", "AceEstimate", function(x, ...) {
# print("ACE Estimates: [print]")#\n")
# aceDisplay <- matrix(c("ASquared", "CSquared", "ESquared", methods::slot(x, "ASquared"), methods::slot(x, "CSquared"), methods::slot(x, "ESquared")), byrow=T, nrow=2)
# cat(aceDisplay, "\n")
# print(aceDisplay)
# print(c(ASquared=methods::slot(x, "ASquared"), CSquared=methods::slot(x, "CSquared"), ESquared=methods::slot(x, "ESquared"), CaseCount=methods::slot(x,"CaseCount")))
# print(c(a=33, d=43))
cat(
"Results of ACE estimation: [print]\n",
c(
ASquared = methods::slot(x, "ASquared"),
CSquared = methods::slot(x, "CSquared"),
ESquared = methods::slot(x, "ESquared"),
CaseCount = methods::slot(x, "CaseCount")
)
)
})
methods::setMethod(f = "show", "AceEstimate", function(object) {
print("Results of ACE estimation: [show]") # \n")
# print(c(GetEstimate(object), CaseCount=methods::slot(object, "CaseCount")))
print(c(
ASquared = methods::slot(object, "ASquared"),
CSquared = methods::slot(object, "CSquared"),
ESquared = methods::slot(object, "ESquared"),
CaseCount = base::round(methods::slot(object, "CaseCount"))
))
# cat(c(ASquared=methods::slot(object, "ASquared"), CSquared=methods::slot(object, "CSquared"), ESquared=methods::slot(object, "ESquared"), CaseCount=round(methods::slot(object,"CaseCount"))))
})
#' @name GetDetails-methods
#' @title GetDetails-methods
#' @aliases GetDetails-methods GetDetails AceEstimate-method
#' @param object ACE object
methods::setGeneric("GetDetails", function(object) {
standardGeneric("GetDetails")
})
#' @exportMethod GetDetails
#' @docType methods
#' @title A generic function for extracting the `Details` slot of an object.
#' @rdname AceEstimate-class
#'
#' @description A generic function for extracting the `Details` slot of an AceEstimation object.
# '
# For examples see https://r-forge.r-project.org/scm/viewvc.php/pkg/lme4/man/lmList-class.Rd?view=markup&revision=2&root=lme4&pathrev=452
# ' @section Methods
# ' \describe{
# ' \item{GetDetails}{`signature(object="AceEstimate")`: Extracts the `Details` slot of an [AceEstimation] object.}
# ' }
#' @param object ACE object
#' @author Will Beasley
#' @keywords methods
methods::setMethod(
f = "GetDetails", "AceEstimate",
definition = function(object) {
# print(str(object))
return(methods::slot(object, "Details")[[1]])
}
)