Title: | Standardizing Physiological Composite Risk Endpoints |
---|---|
Description: | Provides a number of functions to simplify and automate the scoring, comparison, and evaluation of different ways of creating composites of data. It is particularly aimed at facilitating the creation of physiological composites of metabolic syndrome symptom score (MetSSS) and allostatic load (AL). Provides a wrapper to calculate the MetSSS on new data using the Healthy Hearts formula. |
Authors: | Joshua F. Wiley [aut, cre] |
Maintainer: | Joshua F. Wiley <[email protected]> |
License: | LGPL-3 |
Version: | 0.4.0 |
Built: | 2024-10-26 03:26:08 UTC |
Source: | https://github.com/jwiley/score-project |
Internal function to prepare data for prediction
.preparePredict(object, newdata, groups)
.preparePredict(object, newdata, groups)
object |
An object of S4 class “MahalanobisScores”, “SumScores”, or “FactorScores” containing a model and results to be used to get predictions on new data. |
newdata |
A data frame with identical variable names as was used to build the initial model. |
groups |
A vector with the same length as the data frame in |
An object of S4 class “CompositeReady”
This data set lists the clinical high risk thresholds for a variety of biomarkers
a list
.
Various publications
An constructor function for the S4 CompositeData class
CompositeData(rawdata, groups, thresholds, higherisbetter, k, rawtrans)
CompositeData(rawdata, groups, thresholds, higherisbetter, k, rawtrans)
rawdata |
A data frame with at least one row and column |
groups |
an optional character vector. If omitted defaults to a character vector of all “one”s. |
thresholds |
an optional named list where names match the names in groups. If using defaults for groups, should name it “one”. |
higherisbetter |
an optional logical vector |
k |
an optional integer, the number of columns in the raw data |
rawtrans |
A list of functions to transform the raw data (and thresholds).
This list should be in the same order as the variables. For variables
that should not be transformed, use: |
An S4 object of class “CompositeData”
## no custom raw data transformations d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) ## square root transform qsec d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE), rawtrans = list( mpg = function(x) x, hp = function(x) x, wt = function(x) x, qsec = sqrt)) ## cleanup rm(d)
## no custom raw data transformations d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) ## square root transform qsec d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE), rawtrans = list( mpg = function(x) x, hp = function(x) x, wt = function(x) x, qsec = sqrt)) ## cleanup rm(d)
A S4 class to represent data for creating a composite
rawdata
A data frame of the data to be used for the composite scores
groups
A character string, the same length as the number of rows of the data indicating to which group each row belong. May be all the same if only one group present in the data.
thresholds
A list with as many elements as there are unique groups in the data, and where each element is a vector the same length as the number of columns in the data frame, indicating the reference thresholds for each variable, by group (all the same if only one group).
higherisbetter
A logical vector the same length as the number of
columns in the data frame, indicating whether higher is better for
each variable (if TRUE
) and otherwise (if FALSE
) that
lower is better, indicating that variable should be reversed.
k
The number of variables as an integer.
rawtrans
A list of functions to transform the raw data (and thresholds).
An constructor function for the S4 CompositeReady class
CompositeReady( data, covmat, sigma, standardize, use.prethreshold, distances, distanceDensity, winsorizedValues, better, rawdata, groups, thresholds, higherisbetter, k, rawtrans )
CompositeReady( data, covmat, sigma, standardize, use.prethreshold, distances, distanceDensity, winsorizedValues, better, rawdata, groups, thresholds, higherisbetter, k, rawtrans )
data |
A data frame of the ready to use data |
covmat |
A covariance matrix of the data |
sigma |
A vector of the standard deviations of each variable |
standardize |
A logical whether the data were standardized or not |
use.prethreshold |
A logical value whether to calculate covariance matrix
based on the data after winsorizing, but before applying the threshold.
Defaults to |
distances |
A data frame of the distance scores |
distanceDensity |
A ggplot2 graph of the densities of each distance score. If not passed, generated automatically from the data. |
winsorizedValues |
A data frame indicating the values used (if any) for winsorization. Should have one row for each variable in the dataset. |
better |
A logical vector the same length as the number of columns in the distance scores indicating whether higher or lower values are better for each. |
rawdata |
A data frame with at least one row and column |
groups |
an optional character vector. If omitted defaults to a character vector of all “one”s. |
thresholds |
an optional named list where names match the names in groups. If using defaults for groups, should name it “a”. |
higherisbetter |
an optional logical vector |
k |
an optional integer, the number of columns in the raw data |
rawtrans |
A list of functions to transform the raw data (and thresholds). |
An S4 object of class “CompositeReady”
#make me!
#make me!
An S4 class to represent composite ready data
data
A data frame ready for use to generate composite scores
covmat
A covariance matrix
sigma
The standard deviation of each variable
standardize
A logical value whether standardization was applied
distances
A data frame of the distance scores
distanceDensity
A ggplot2 density graph
winsorizedValues
A data frame of the values at which each variable was winsorized. If percentile is 0, that means no winsorization, and values will simply be min and max.
better
A logical value whether better scores than threshold were allowed
use.prethreshold
A logical value whether covariance matrix was calculated from pre threshold but post winsorizing data.
This function removes graphs and other sensitive data.
dropData(object)
dropData(object)
object |
A MahalanobisComposite object |
A MahalanobisComposite object with some slots replaced with missing values
# make me!!!
# make me!!!
Create a composite using a Factor Model
factorComposite( object, type = c("onefactor", "secondorderfactor", "bifactor"), factors = list(NA_character_) )
factorComposite( object, type = c("onefactor", "secondorderfactor", "bifactor"), factors = list(NA_character_) )
object |
An object of class |
type |
A character string indicating the type of factor model to use |
factors |
A named list where names are the factor names and each element is a character string of the indicator names. |
An S4 object of class FactorScores
.
Other composite:
mahalanobisComposite()
,
sumComposite()
# this example creates distances for the built in mtcars data # see ?mtcars for more details # The distances are calculated from the "best" in the dataset # First we create an appropriate CompositeData class object # higher mpg & hp are better and lower wt & qsec are better d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) # create the distance scores # and the composite # covariance matrix will be calculated from the data # and data will be standardized to unit variance by default dres <- prepareComposite(d) # see a density plot of the distance scores dres@distanceDensity # regular summary of distance scores summary(dres@distances) # examine covariance matrix round(dres@covmat,2) # now we can create the composite based on summing the (standardized) # distances from our defined thresholds # by default, distances are squared, then summed, and then square rooted # to be back on the original scale fcomp <- factorComposite(dres, type = "onefactor") # view a histogram of the composite scores fcomp@scoreHistogram # summarize the composite scores summary(fcomp@scores) ## Not run: # we can also fit a second-order factor model # there are not enough indicators to identify the factor # and so lavaan gives us warning messages fcomp2 <- factorComposite(dres, type = "secondorderfactor", factors = list(speed = c("hp", "qsec"))) # view a histogram of the composite scores fcomp2@scoreHistogram # summarize the composite scores summary(fcomp2@scores) # compare one and second-order factor model scores plot(fcomp@scores, fcomp2@scores) # cleanup rm(d, dres, fcomp, fcomp2) ## End(Not run)
# this example creates distances for the built in mtcars data # see ?mtcars for more details # The distances are calculated from the "best" in the dataset # First we create an appropriate CompositeData class object # higher mpg & hp are better and lower wt & qsec are better d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) # create the distance scores # and the composite # covariance matrix will be calculated from the data # and data will be standardized to unit variance by default dres <- prepareComposite(d) # see a density plot of the distance scores dres@distanceDensity # regular summary of distance scores summary(dres@distances) # examine covariance matrix round(dres@covmat,2) # now we can create the composite based on summing the (standardized) # distances from our defined thresholds # by default, distances are squared, then summed, and then square rooted # to be back on the original scale fcomp <- factorComposite(dres, type = "onefactor") # view a histogram of the composite scores fcomp@scoreHistogram # summarize the composite scores summary(fcomp@scores) ## Not run: # we can also fit a second-order factor model # there are not enough indicators to identify the factor # and so lavaan gives us warning messages fcomp2 <- factorComposite(dres, type = "secondorderfactor", factors = list(speed = c("hp", "qsec"))) # view a histogram of the composite scores fcomp2@scoreHistogram # summarize the composite scores summary(fcomp2@scores) # compare one and second-order factor model scores plot(fcomp@scores, fcomp2@scores) # cleanup rm(d, dres, fcomp, fcomp2) ## End(Not run)
Internal function only, not meant for general use Simple wrapper around ggplot2 functionaly to create density plots, potentially for many variables and coloured by group.
ldensity(data, melt = FALSE, x, facet, g, hist = FALSE)
ldensity(data, melt = FALSE, x, facet, g, hist = FALSE)
data |
A dataset (or melt()ed dataset) |
melt |
Logical whether to melt() dataset |
x |
name of variable for density |
facet |
A variable to use for facetting |
g |
A variable to use for grouping/colouring. If |
hist |
Logical whether to make a density plot or histogram (if TRUE). |
A ggplot2 graph.
# simple facetted plot pscore:::ldensity(mtcars, TRUE) # simple coloured plot pscore:::ldensity(mtcars, x = "mpg", g = "cyl")
# simple facetted plot pscore:::ldensity(mtcars, TRUE) # simple coloured plot pscore:::ldensity(mtcars, x = "mpg", g = "cyl")
Create a composite using the Mahalanobis Distance
mahalanobisComposite(object, ncomponents, pca)
mahalanobisComposite(object, ncomponents, pca)
object |
An object of class |
ncomponents |
the number of components to use from the principal component analysis. If missing, defaults to the number of columns in the data. |
pca |
An optional PCA object from princomp to use. If not passed, will be calculated from the data. |
An S4 object of class MahalanobisScores
.
Other composite:
factorComposite()
,
sumComposite()
# this example creates distances for the built in mtcars data # see ?mtcars for more details # The distances are calculated from the "best" in the dataset # First we create an appropriate CompositeData class object # higher mpg & hp are better and lower wt & qsec are better d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) # create the distance scores # and the composite # covariance matrix will be calculated from the data # and data will be standardized to unit variance by default dres <- prepareComposite(d) # see a density plot of the distance scores dres@distanceDensity # regular summary of distance scores summary(dres@distances) # examine covariance matrix round(dres@covmat,2) # now we can create the composite based on mahalanobis distances # from our defined thresholds mcomp <- mahalanobisComposite(dres, 1) # view a histogram of the composite scores mcomp@scoreHistogram # summarize the composite scores summary(mcomp@scores) # check the screeplot and loadings mcomp@screePlot mcomp@loadingGraph # examine the loadings as a table mcomp@loadingTable # one component is adequate to explain these data # to be safe can pick first two and re-run model # use only first two components mcomp2 <- mahalanobisComposite(dres, ncomponents = 2) # view a histogram of the updated composite scores mcomp2@scoreHistogram # summarize the composite scores summary(mcomp2@scores) # compare using all versus two components plot(mcomp@scores, mcomp2@scores) # cleanup rm(d, dres, mcomp, mcomp2)
# this example creates distances for the built in mtcars data # see ?mtcars for more details # The distances are calculated from the "best" in the dataset # First we create an appropriate CompositeData class object # higher mpg & hp are better and lower wt & qsec are better d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) # create the distance scores # and the composite # covariance matrix will be calculated from the data # and data will be standardized to unit variance by default dres <- prepareComposite(d) # see a density plot of the distance scores dres@distanceDensity # regular summary of distance scores summary(dres@distances) # examine covariance matrix round(dres@covmat,2) # now we can create the composite based on mahalanobis distances # from our defined thresholds mcomp <- mahalanobisComposite(dres, 1) # view a histogram of the composite scores mcomp@scoreHistogram # summarize the composite scores summary(mcomp@scores) # check the screeplot and loadings mcomp@screePlot mcomp@loadingGraph # examine the loadings as a table mcomp@loadingTable # one component is adequate to explain these data # to be safe can pick first two and re-run model # use only first two components mcomp2 <- mahalanobisComposite(dres, ncomponents = 2) # view a histogram of the updated composite scores mcomp2@scoreHistogram # summarize the composite scores summary(mcomp2@scores) # compare using all versus two components plot(mcomp@scores, mcomp2@scores) # cleanup rm(d, dres, mcomp, mcomp2)
An S4 class to represent composite scores based on Mahalanobis distance
An S4 class to represent composite scores based on summing
An S4 class to represent composite scores based on (confirmatory) factor analysis
scores
A vector of the final scores
scoreHistogram
A histogram of the final scores
screePlot
A screeplot from the PCA
loadingGraph
A graph of the component loadings
loadingTable
A table of all the component loadings
pca
Prinicipal component analysis results. A list (coercied from princomp output).
ncomponents
The number of components of the PCA to be used.
CompositeReady
The original CompositeReady class object passed in
scores
A vector of the final scores
scoreHistogram
A histogram of the final scores
transform
A character string of the type of transformation applied
type
A character string indicating whether data were summed or averaged
trans
A list of functions to transform data and backtransform it
systems
A list with as many elements as there are systems where each element is a character vector containing the variable names for each system. If all variables belong to one system, a list with one element that is missing.
CompositeReady
The original CompositeReady class object passed in
scores
A vector of the final scores
scoreHistogram
A histogram of the final scores
factorScores
A data frame with all factor score predictions
type
A character string indicating the type of factor model used
factors
A list with as many elements as there are specific factors, where each element is a character vector of the variables for a specific factor
Fit
A fitted model object from lavaan.
CompositeReady
The original CompositeReady class object passed in
Function requires systolic and diastolic blood pressure, triglycerides, waist circumference, HDL cholesterol, blood glucose, and sex.
MetSSS(input)
MetSSS(input)
input |
data passed on as |
mydata <- data.frame( sbp = c(122, 102.5), dbp = c(76.5, 64), trigs = c(1.47, 1.27), hdl = c(2.22, 1.59), waist = c(71, 91), glucose = c(5.16, 5.82), sex = c("Female", "Male")) MetSSS(mydata)
mydata <- data.frame( sbp = c(122, 102.5), dbp = c(76.5, 64), trigs = c(1.47, 1.27), hdl = c(2.22, 1.59), waist = c(71, 91), glucose = c(5.16, 5.82), sex = c("Female", "Male")) MetSSS(mydata)
Simple example model that predicts hours of watching TV from age and marital status
MetSSS_model
MetSSS_model
an object from mahalanobisComposite
Healthy Hearts Study
Internal function to prepare data for prediction
predictCS(object, newdata, groups)
predictCS(object, newdata, groups)
object |
An object of S4 class “MahalanobisScores”, “SumScores”, or “FactorScores” containing a model and results to be used to get predictions on new data. |
newdata |
A data frame with identical variable names as was used to build the initial model. |
groups |
A vector with the same length as the data frame in |
An object of S4 class “CompositeReady”
d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "disp")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), disp = min(disp)))), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) ## create the distance scores ## and prepare to create the composite dres <- prepareComposite(d) ## create composite based on summing the (standardized) scomp <- sumComposite(dres, "square", "sum") ## use model to generate predictions on new data predictCS(scomp, newdata = mtcars[1, c("mpg", "hp", "wt", "qsec")], groups = "one") ## create composite based on mahalanobis distances mcomp <- mahalanobisComposite(dres) ## use model to generate predictions on new data predictCS(mcomp, newdata = mtcars[1, c("mpg", "hp", "wt", "qsec")], groups = "one") ## note in this too simple example, there are negative variance estimates ## create composite based on factor scores fcomp <- factorComposite(dres, type = "onefactor") ## use model to generate predictions on new data predictCS(fcomp, newdata = mtcars[1:5, c("mpg", "hp", "wt", "disp")], groups = rep("one", 5))
d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "disp")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), disp = min(disp)))), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) ## create the distance scores ## and prepare to create the composite dres <- prepareComposite(d) ## create composite based on summing the (standardized) scomp <- sumComposite(dres, "square", "sum") ## use model to generate predictions on new data predictCS(scomp, newdata = mtcars[1, c("mpg", "hp", "wt", "qsec")], groups = "one") ## create composite based on mahalanobis distances mcomp <- mahalanobisComposite(dres) ## use model to generate predictions on new data predictCS(mcomp, newdata = mtcars[1, c("mpg", "hp", "wt", "qsec")], groups = "one") ## note in this too simple example, there are negative variance estimates ## create composite based on factor scores fcomp <- factorComposite(dres, type = "onefactor") ## use model to generate predictions on new data predictCS(fcomp, newdata = mtcars[1:5, c("mpg", "hp", "wt", "disp")], groups = rep("one", 5))
Prepare distance scores on data in preparation for composite scoring
prepareComposite( object, winsorize = 0, values, better = TRUE, covmat, standardize = TRUE, use.prethreshold = FALSE )
prepareComposite( object, winsorize = 0, values, better = TRUE, covmat, standardize = TRUE, use.prethreshold = FALSE )
object |
An object of class ‘CompositeData’. |
winsorize |
Whether to winsorize the data or not. Defaults to |
values |
The values to use for winsorization. Optional. If specified, preempts the percentiles given by winsorize. |
better |
Logical indicating whether “better” values than the threshold
are allowed. Defaults to |
covmat |
The covariance matrix to use. If missing, austomatically calculated from the data. |
standardize |
A logical value whether to standardize the data or not.
Defaults to |
use.prethreshold |
A logical value whether to calculate covariance matrix
based on the data after winsorizing, but before applying the threshold.
Defaults to |
An S4 object of class “CompositeReady”.
# this example creates distances for the built in mtcars data # see ?mtcars for more details # The distances are calculated from the "best" in the dataset # First we create an appropriate CompositeData class object # higher mpg & hp are better and lower wt & qsec are better d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE), rawtrans = list( mpg = function(x) x, hp = function(x) x, wt = function(x) x, qsec = sqrt)) # create the distance scores dres <- prepareComposite(d) # see a density plot of the distance scores dres@distanceDensity # regular summary of distance scores summary(dres@distances) # examine covariance matrix round(dres@covmat,2) # cleanup rm(d, dres)
# this example creates distances for the built in mtcars data # see ?mtcars for more details # The distances are calculated from the "best" in the dataset # First we create an appropriate CompositeData class object # higher mpg & hp are better and lower wt & qsec are better d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE), rawtrans = list( mpg = function(x) x, hp = function(x) x, wt = function(x) x, qsec = sqrt)) # create the distance scores dres <- prepareComposite(d) # see a density plot of the distance scores dres@distanceDensity # regular summary of distance scores summary(dres@distances) # examine covariance matrix round(dres@covmat,2) # cleanup rm(d, dres)
Create a composite using summation
sumComposite( object, transform = c("square", "abs", "none"), type = c("sum", "mean"), systems )
sumComposite( object, transform = c("square", "abs", "none"), type = c("sum", "mean"), systems )
object |
An object of class |
transform |
A character string indicating the type of transformation to use. One of “square”, “abs”, or “none”, which either sums the raw data, sums the squared data and then takes the square root, or sums the absolute values of the data. |
type |
A character string indicating the type of aggregation to use. One of “sum” or “mean”. |
systems |
An optional list where each element is a character vector of the variable names within a particular system. If given, scores are first averaged within a system, before being aggregated across systems. |
An S4 object of class SumScores
.
Other composite:
factorComposite()
,
mahalanobisComposite()
# this example creates distances for the built in mtcars data # see ?mtcars for more details # The distances are calculated from the "best" in the dataset # First we create an appropriate CompositeData class object # higher mpg & hp are better and lower wt & qsec are better d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) # create the distance scores # and the composite # covariance matrix will be calculated from the data # and data will be standardized to unit variance by default dres <- prepareComposite(d) # see a density plot of the distance scores dres@distanceDensity # regular summary of distance scores summary(dres@distances) # examine covariance matrix round(dres@covmat,2) # now we can create the composite based on summing the (standardized) # distances from our defined thresholds # by default, distances are squared, then summed, and then square rooted # to be back on the original scale scomp <- sumComposite(dres, "square", "sum") # view a histogram and summary of the composite scores scomp@scoreHistogram summary(scomp@scores) # calculate average (mean) instead of sum scomp2 <- sumComposite(dres, "square", "mean") # view a histogram and summary of the composite scores scomp2@scoreHistogram summary(scomp2@scores) # scores are still the same (just different scaling) plot(scomp@scores, scomp2@scores) # first average scores within a system, then sum # within a system, scores are always averaged, never summed scomp3 <- sumComposite(dres, "square", "sum", systems = list( environment = c("mpg"), performance = c("hp", "qsec", "wt"))) # view a histogram and summary of the composite scores scomp3@scoreHistogram summary(scomp3@scores) # compare all three scores # because of the different number of indicators within each system # there is a re-weighting for S3 plot(data.frame(S1 = scomp@scores, S2 = scomp2@scores, S3 = scomp3@scores)) # cleanup rm(d, dres, scomp, scomp2, scomp3)
# this example creates distances for the built in mtcars data # see ?mtcars for more details # The distances are calculated from the "best" in the dataset # First we create an appropriate CompositeData class object # higher mpg & hp are better and lower wt & qsec are better d <- CompositeData(mtcars[, c("mpg", "hp", "wt", "qsec")], thresholds = list(one = with(mtcars, c( mpg = max(mpg), hp = max(hp), wt = min(wt), qsec = min(qsec))) ), higherisbetter = c(TRUE, TRUE, FALSE, FALSE)) # create the distance scores # and the composite # covariance matrix will be calculated from the data # and data will be standardized to unit variance by default dres <- prepareComposite(d) # see a density plot of the distance scores dres@distanceDensity # regular summary of distance scores summary(dres@distances) # examine covariance matrix round(dres@covmat,2) # now we can create the composite based on summing the (standardized) # distances from our defined thresholds # by default, distances are squared, then summed, and then square rooted # to be back on the original scale scomp <- sumComposite(dres, "square", "sum") # view a histogram and summary of the composite scores scomp@scoreHistogram summary(scomp@scores) # calculate average (mean) instead of sum scomp2 <- sumComposite(dres, "square", "mean") # view a histogram and summary of the composite scores scomp2@scoreHistogram summary(scomp2@scores) # scores are still the same (just different scaling) plot(scomp@scores, scomp2@scores) # first average scores within a system, then sum # within a system, scores are always averaged, never summed scomp3 <- sumComposite(dres, "square", "sum", systems = list( environment = c("mpg"), performance = c("hp", "qsec", "wt"))) # view a histogram and summary of the composite scores scomp3@scoreHistogram summary(scomp3@scores) # compare all three scores # because of the different number of indicators within each system # there is a re-weighting for S3 plot(data.frame(S1 = scomp@scores, S2 = scomp2@scores, S3 = scomp3@scores)) # cleanup rm(d, dres, scomp, scomp2, scomp3)