mahalanobisComposite: Score Data Using the Mahalanobis Distance

Description Usage Arguments Value See Also Examples

View source: R/composite.R

Description

Create a composite using the Mahalanobis Distance

Usage

1
mahalanobisComposite(object, ncomponents)

Arguments

object

An object of class compositedata ready for use

ncomponents

the number of components to use from the principal component analysis. If missing, defaults to the number of columns in the data.

Value

A list of results.

See Also

Other composite: distanceScores; factorComposite; prepareComposite; sumComposite

Examples

 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
# 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
# defined by these thresholds
thresholds <- with(mtcars, c(
  mpg = max(mpg),
  hp = max(hp),
  wt = min(wt),
  qsec = min(qsec)))

# higher mpg and hp are better,
# whereas lower wt and qsec are better
dres <- distanceScores(mtcars[, c("mpg", "hp", "wt", "qsec")],
  thresholds = list(thresholds),
  higherisbetter = c(TRUE, TRUE, FALSE, FALSE),
  saveall = TRUE)

# see a density plot of the distance scores
dres$Density

# now prepare to create the composite
# covariance matrix will be calculated from the data
# and data will be standardized to unit variance by default
cprep <- prepareComposite(dres)

# now we can create the composite based on mahalanobis distances
# from our defined thresholds
mcomp <- mahalanobisComposite(cprep)

# 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(cprep, 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(thresholds, dres, cprep, mcomp, mcomp2)

Example output

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  5.240   7.718   8.176   8.069   8.777   9.241 
     C1      C2      C3      C4     
mpg  " 0.55" "-0.27" "-0.53" " 0.59"
hp   "-0.55" "-0.21" "-0.76" "-0.26"
wt   " 0.49" "-0.54" " 0.02" "-0.68"
qsec "-0.38" "-0.77" " 0.38" " 0.34"
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  2.275   3.235   3.862   3.856   4.452   6.088 

pscore documentation built on May 1, 2019, 6:28 p.m.