ANOVA and MANOVA: predict a (counted/measured) response from group membership.
Discriminant analysis: predict group membership based on counted/measured variables.
Covers same ground as logistic regression (and its variations), but emphasis on classifying observed data into correct groups.
Does so by searching for linear combination of original variables that best separates data into groups (canonical variables).
Assumption here that groups are known (for data we have). If trying to “best separate” data into unknown groups, see cluster analysis.
ggrepel allows labelling points on a plot so they don’t overwrite each other.selectBoth dplyr (in tidyverse) and MASS have a function called select, and they do different things.
When you load MASS, make sure to load it without its select, so that when you use select, you get the one you’re used to.
If you forget, and you intend to use the tidyverse select, you will get a problem that is almost impossible to debug unless you have seen it before.
ggordggord (the package) contains a function, also called ggord, that makes a nice picture of a discriminant analysis.r-universe, so to install:Recall data from MANOVA: needed a multivariate analysis to find difference in seed yield and weight based on whether they were high or low fertilizer.
Uses lda from package MASS.
“Predicting” group membership from measured variables.
hilo.1)Call:
lda(fertilizer ~ yield + weight, data = hilo)
Prior probabilities of groups:
high low
0.5 0.5
Group means:
yield weight
high 35.0 13.25
low 32.5 12.00
Coefficients of linear discriminants:
LD1
yield -0.7666761
weight -1.2513563
Group means: high-fertilizer plants have (slightly) higher mean yield and weight than low-fertilizer plants.
“Coefficients of linear discriminants”: are scores constructed from observed variables that best separate the groups.
For any plant, get LD1 score by taking \(-0.76\) times yield plus \(-1.25\) times weight, add up, standardize.
the LD1 coefficients are like slopes:
High-fertilizer plants have higher yield and weight, thus low (negative) LD1 score. Low-fertilizer plants have low yield and weight, thus high (positive) LD1 score.
One LD1 score for each observation. Plot with actual groups, because the LD1 scale should do the best job of distinguishing the fertilizer groups.
Smaller of these:
Number of variables
Number of groups minus 1
Seed yield and weight: 2 variables, 2 groups, \(\min(2,2-1)=1\).
Feed output from LDA into predict:
Most positive LD1 score is most obviously low fertilizer, most negative is most obviously high.
High fertilizer have yield and weight high, negative LD1 scores.
With one LD score, plot against (true) groups, eg. boxplot:
hilo.2?class: predicted fertilizer level (based on values of yield and weight).
posterior: predicted probability of being low or high fertilizer given yield and weight.
LD1: scores for (each) linear discriminant (here is only LD1) on each observation.
based on yield and weight:
Each predicted fertilizer level is exactly same as observed one (perfect prediction).
Table shows no errors: all values on top-left to bottom-right diagonal.
show how clear-cut the classification decisions were:
244 people who do one of three different jobs also took a survey that gave them scores on three different traits called Outdoor, Social, and Conservative. Can we use these survey scores to distinguish the people who do the three different jobs?
The jobs are numbered 1, 2, and 3, but we actually know that 1 is customer service, 2 is mechanic, 3 is dispatcher. It would be better to have those names in the dataset.
recode_values:
Call:
lda(job ~ outdoor + social + conservative, data = jobs)
Prior probabilities of groups:
cs disp mech
0.3483607 0.2704918 0.3811475
Group means:
outdoor social conservative
cs 12.51765 24.22353 9.023529
disp 15.57576 15.45455 13.242424
mech 18.53763 21.13978 10.139785
Coefficients of linear discriminants:
LD1 LD2
outdoor -0.09198065 -0.22501431
social 0.19427415 -0.04978105
conservative -0.15499199 0.08734288
Proportion of trace:
LD1 LD2
0.7712 0.2288
social, low on other two.outdoor.predict:Rows: 244
Columns: 11
$ outdoor <dbl> 10, 14, 19, 14, 14, 20, 6, 13, 18, 16, 17, 10, 17, 10, …
$ social <dbl> 22, 17, 33, 29, 25, 25, 18, 27, 31, 35, 25, 29, 25, 22,…
$ conservative <dbl> 5, 6, 7, 12, 7, 12, 4, 7, 9, 13, 8, 11, 7, 13, 13, 5, 1…
$ job <chr> "cs", "cs", "cs", "cs", "cs", "cs", "cs", "cs", "cs", "…
$ id <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, …
$ class <fct> cs, mech, cs, cs, cs, mech, cs, cs, cs, cs, cs, cs, cs,…
$ posterior.cs <dbl> 0.9037622, 0.3677743, 0.7302117, 0.8100756, 0.7677607, …
$ posterior.disp <dbl> 7.289988e-03, 1.432468e-01, 3.186265e-04, 7.751215e-03,…
$ posterior.mech <dbl> 0.0889478485, 0.4889789008, 0.2694697105, 0.1821731894,…
$ x.LD1 <dbl> 1.6423155, 0.1480302, 2.6415213, 1.5493681, 1.5472314, …
$ x.LD2 <dbl> 0.71477348, 0.15096436, -1.68326115, 0.07764901, -0.159…
Individuals and original variables on same plot:
social, low conservativeoutdoorsocial, low on conservativeoutdoor pred
obs cs disp mech
cs 68 4 13
disp 3 50 13
mech 16 10 67
15 individuals from three different professions (politicians, administrators and belly dancers) each participate in four different leisure activities: reading, dancing, TV watching and skiing. After each activity they rate it on a 0–10 scale.
How can we best use the scores on the activities to predict a person’s profession?
Or, what combination(s) of scores best separate data into profession groups?
Call:
lda(job ~ reading + dance + tv + ski, data = active)
Prior probabilities of groups:
admin bellydancer politician
0.3333333 0.3333333 0.3333333
Group means:
reading dance tv ski
admin 5.0 2.0 1.8 3.8
bellydancer 6.6 9.4 5.8 7.4
politician 5.0 4.8 5.2 5.0
Coefficients of linear discriminants:
LD1 LD2
reading -0.01297465 -0.4748081
dance -0.95212396 -0.4614976
tv -0.47417264 1.2446327
ski 0.04153684 -0.2033122
Proportion of trace:
LD1 LD2
0.8917 0.1083
Two discriminants, first fair bit more important than second.
LD1 depends (negatively) most on dance, a bit on tv.
LD2 depends mostly (positively) on tv.
pred
obs admin bellydancer politician
admin 5 0 0
bellydancer 0 5 0
politician 0 0 5
Everyone correctly classified.
Groups well separated: bellydancers bottom left, administrators bottom right, politicians upper middle.
Bellydancers most negative on LD1: like dancing most.
Administrators most positive on LD1: like dancing least.
Politicians most negative on LD2: like TV-watching most.
Not much doubt.
View 25 crops from air, measure 4 variables x1-x4.
Go back and record what each crop was.
Can we use the 4 variables to distinguish crops?
Call:
lda(crop ~ x1 + x2 + x3 + x4, data = crops)
Prior probabilities of groups:
Corn Cotton Soybeans Sugarbeets
0.28 0.24 0.24 0.24
Group means:
x1 x2 x3 x4
Corn 15.28571 22.71429 27.42857 33.14286
Cotton 34.50000 32.66667 35.00000 39.16667
Soybeans 21.00000 27.00000 23.50000 29.66667
Sugarbeets 31.00000 32.16667 20.00000 40.50000
Coefficients of linear discriminants:
LD1 LD2 LD3
x1 0.14077479 0.007780184 -0.0312610362
x2 0.03006972 0.007318386 0.0085401510
x3 -0.06363974 -0.099520895 -0.0005309869
x4 -0.00677414 -0.035612707 0.0577718649
Proportion of trace:
LD1 LD2 LD3
0.8044 0.1832 0.0124
3 LDs (four variables, four groups).
1st two important.
LD1 mostly x1 (plus)
LD2 x3 (minus)
pred
obs Corn Cotton Soybeans Sugarbeets
Corn 6 0 1 0
Cotton 0 4 2 0
Soybeans 2 0 3 1
Sugarbeets 0 0 3 3
Corn (red) mostly left, cotton (green) sort of right, soybeans and sugarbeets (blue and purple) mixed up.
Corn low on LD1 (left), hence low on x1
Cotton tends to be high on LD1 (high x1)
one cotton very low on LD2 (high x3?)
Rather mixed up.
These were the misclassified ones, but the posterior probability of being correct was not usually too low.
The correctly-classified ones are not very clear-cut either.
Comments
Only obs. 7 has any doubt:
yieldlow for a high-fertilizer, but highweightmakes up for it.