Analisis Peubah Ganda: Pertemuan VIII
Analisis Peubah Ganda: Pertemuan VIII
Pertemuan VIII
ANALISIS KOMPONEN UTAMA
Let see this example: Air pollution in US
cities
Data were collected to investigate the determinants of
pollution. The following variables were obtained for 41 US
cities:
• SO2: SO2 content of air in micrograms per cubic metre;
• temp: average annual temperature in degrees Fahrenheit;
• manu: number of manufacturing enterprises employing 20
or more workers;
• popul: population size (1970 census) in thousands;
• wind: average annual wind speed in miles per hour;
• precip: average annual precipitation in inches;
• predays: average number of days with precipitation per
year.
The data are shown in next Table
• The main goal in the researcher's mind when collecting
the air pollution data, was to determine which of the
climate and human ecology variables are the best
predictors of the degree of air pollution in a city as
measured by the sulphur dioxide content of the air.
> data_awal<-read.csv("air_pollution.csv“,
row.names=1)
> round(cor(data_awal),digits=3)
SO2 temp manu popul wind precip predays
SO2 1.000 -0.434 0.645 0.494 0.095 0.054 0.370
temp -0.434 1.000 -0.190 -0.063 -0.350 0.386 -0.430
manu 0.645 -0.190 1.000 0.955 0.238 -0.032 0.132
popul 0.494 -0.063 0.955 1.000 0.213 -0.026 0.042
wind 0.095 -0.350 0.238 0.213 1.000 -0.013 0.164
precip 0.054 0.386 -0.032 -0.026 -0.013 1.000 0.496
predays 0.370 -0.430 0.132 0.042 0.164 0.496 1.000
So what???
> keeps<-c(2,3,4,5,6,7)
> data_polusi<-data_awal[,keeps]
Since all six variables are such that high values represent a
less attractive environment, then a case can be made to use
negative temperature values.
> data_polusi$negtemp<-(-1)*data_polusi$temp
> data_polusi$temp<-NULL
Prior to undertaking the principal components analysis on
the air pollution data, we will construct a scatterplot matrix
of the six variables, including the histograms for each
variable on the main diagonal.
> panel.hist <- function(x, ...) {
+ usr <- par("usr"); on.exit(par(usr))
+ par(usr = c(usr[1:2], 0, 1.5) )
+ h <- hist(x, plot = FALSE)
+ breaks <- h$breaks; nB <- length(breaks)
+ y <- h$counts; y <- y/max(y)
+ rect(breaks[-nB], 0, breaks[-1], y, col="grey", ...)
+ }
> pairs(data_polusi, diag.panel = panel.hist,
+ pch = ".", cex = 1.5)
• A clear message from Figure above is that there is at least
one city, and probably more than one, that should be
considered an outlier.
OUTPUT:
Importance of components:
Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6
Standard deviation 1.4819456 1.2247218 1.1809526 0.8719099 0.33848287 0.185599752
Proportion of Variance 0.3660271 0.2499906 0.2324415 0.1267045 0.01909511 0.005741211
Cumulative Proportion 0.3660271 0.6160177 0.8484592 0.9751637 0.99425879 1.000000000
Loadings:
Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6
manu -0.612 0.168 0.273 -0.137 0.102 0.703
popul -0.578 0.222 0.350 -0.695
wind -0.354 -0.131 -0.297 0.869 -0.113
precip -0.623 0.505 0.171 0.568
predays -0.238 -0.708 -0.311 -0.580
negtemp -0.330 -0.128 -0.672 -0.306 0.558 -0.136
Screeplot:
> screeplot(polusi_pca,type = "lines",col=4)
Interpretation??
• We see that the rest three components all have variances
(eigenvalues) greater than one and together account for
almost 85% of the variance of the original variables.
• Scores on these three components might be used to graph
the data with little loss of information.
• Many users of principal components analysis might be
tempted to search for an interpretation of the derived
components that allows them to be “labelled“ in some sense.
This requires examining the coeficients defining each
component.
• We may add component label like below
1. We see that the first component might be regarded as
some index of “quality of life", with high values
indicating a relatively poor environment.
2. The second component is largely concerned with a
city's rainfall having high coecients for precip and
predays and might be labelled as the “wet weather"
component.
3. Component three is essentially a contrast between
precip and negtemp and will separate cities having
high temperatures and high rainfall from those that are
colder but drier. A suitable label might be simply
“climate type".
But … you should know
It must be emphasized that no mathematical method is, or
could be, designed to give physically meaningful results. If a
mathematical expression of this sort has an obvious physical
meaning, it must be attributed to a lucky change, or to the
fact that the data have a strongly marked structure that
shows up in analysis. Even in the latter case, quite small
sampling fluctuations can upset the interpretation; for
example, the first two principal components may appear in
reverse order, or may become confused altogether.
Marriott (1974)
So, what’s next?
• The three components can be used as the basis of various
graphical displays of the cities. In fact, this is often the most
useful aspect of a principal components analysis.
• The first few component scores provide a low-dimensional
“map" of the observations.
Call:
lm(formula = SO2 ~ polusi_pca$scores, data = data_polusi)
Residuals:
Min 1Q Median 3Q Max
-23.004 -8.542 -0.991 5.758 48.758
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.049 2.286 13.146 6.91e-15 ***
polusi_pca$scoresComp.1 9.942 1.542 6.446 2.28e-07 ***
polusi_pca$scoresComp.2 -2.240 1.866 -1.200 0.23845
polusi_pca$scoresComp.3 0.375 1.935 0.194 0.84752
polusi_pca$scoresComp.4 -8.549 2.622 -3.261 0.00253 **
polusi_pca$scoresComp.5 -15.176 6.753 -2.247 0.03122 *
polusi_pca$scoresComp.6 39.271 12.316 3.189 0.00306 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Pertemuan IX
FACTOR ANALYSIS
Latent Variables vs Manifest Variables
• Latent variables is concepts that cannot be measured directly.
• But latent variables can be assumed to relate to a number of
measurable or manifest variables.
• Example: How could we measure intelligence?
Spearman sought to
Spearman, C. (1904). describe the influence of
“General intelligence” g on examinee’s test
objectively determined scores on several
and measured. American domains:
Journal of Psychology,
At the time, Psychologists
• Pitch
15, 201-293.
had thought that intelli- • Light
gence could be defined by • Weight
a single, all-encompassing • Classics
unobservable entity, • French
called “g” (for general • English
intelligence) • Mathematics
• The model proposed by Spearman was very similar to a linear
regression model:
Pitch
Light
Weight Intelligence
...
Math
The method of analysis most generally
used to help uncover the relationships
between the assumed latent variables and
the manifest variables is factor analysis
• Or
Love is
latent!!!
Variabel Manifes
Spearman berusaha
Spearman, C. (1904). menggambarkan
mencoba mengukur pengaruh g pada nilai tes
“General intelligence”. peserta ujian pada
American Journal of beberapa domain:
Psychology, 15, 201-293. • Pitch
Pada saat itu, para • Light
Psikolog berpikir bahwa • Weight
kecerdasan dapat • Classics
didefinisikan oleh entitas • French
tunggal yang tidak dapat • English
diobservasi, yang disebut
faktor "g"
• Mathematics
Pitch
Light
Weight Intelligence
...
Math
Faktor 1
Kita sudah memiliki
asumsi mengenai
faktor yang
terbentuk
Faktor 2
Faktor 1
Kita sudah memiliki
asumsi mengenai
faktor yang
terbentuk
Faktor 2
X3
X4
Faktor 1
Kita sudah memiliki
X5 asumsi mengenai
faktor yang
X1 terbentuk
Faktor 2
X2
Faktor 1
Faktor 5
Faktor 1
Faktor 5
X1
Faktor 1
X5
Faktor 5
X1
Faktor 1
X5
Faktor 5
X1
Faktor 1
X5
...
...
Xp
Faktor m
Atau
𝑿 = 𝝁 + Λ F + 𝝐
( p x 1) (p x 1) (p x m) (m x 1) (p x 1)
• Sehingga:
𝐶𝐶𝐶 𝑋 = 𝐶𝐶𝐶 𝜇 + Λ F + 𝜖
𝐶𝐶𝐶 𝑋 = 𝐶𝐶𝐶(𝜇) + 𝐶𝐶v (Λ F) + Cov(𝜖)
𝐶𝐶𝐶 𝑋 = Λ 𝐶𝑜v (F) Λ′+ Ψ
𝐶𝐶𝐶 𝑋 = Λ Λ′+ Ψ
Faktor 2
Faktor 1
μ = (μ1, μ2)
X1
Faktor 2
Faktor 1
μ = (μ1, μ2)
X1
Faktor 2
Faktor 1
μ = (μ1, μ2)
X1
Loading hasil
ekstraksi faktor
Communality
Communalities:
Kedua Faktor hasil ekstraksi telah
Oil menyumbangkan
mampu 0.666 76,1
Density
keragaman dari0.845
total
Crispy 0.958
Fracture 0.744
Hardness 0.584
• Sebagai contoh, ada lebih dari empat triliun kemungkinan cara agar 25 objek
dapat dikelompokkan menjadi 4 kelompok - solusi mana yang terbaik?
Metode Heuristik
• Numerical Data
• Euclidean Distance, Squared Euclidean Distance, Normalized Squared Euclidean Distance, Manhattan Distance,
Chessboard Distance, Bray-Curtis Distance, Canberra Distance, Cosine Distance, Correlation Distance, Binary
Distance, Warping Distance, Canonical Warping Distance
• Boolean Data
• Hamming Distance, Jaccard Dissimilarity, Matching Dissimilarity, Dice Dissimilarity, Rogers-Tanimoto Dissimilarity,
Russell-Rao Dissimilarity, Sokal-Sneath Dissimilarity, Yule Dissimilarity
• String Data
• Edit Distance, Damerau-Levenshtein Distance, Hamming Distance, Smith-Waterman Similarity, Needleman-Wunsch
Similarity
Jarak Euclidean vs Mahalanobis
• Jarak euclidean:
• Jarak mahalanobis:
Metode Hierarkhi
Metode Hierarkhi
• Untuk memvisualisasikan
proses pembentukan
cluster digunakan
dendogram. Dendrogram
adalah grafik yang
menggambarkan berbagai
cluster secara hierarki
berdasarkan urutan
penggabungan/pemisaha
nnya, disertai jarak di
mana masing-masing
cluster terbentuk.
Contoh: Clustering metode aglomeratif
• Problem: clustering analysis with agglomerative
algorithm
data matrix
Euclidean distance
distance matrix
17
• Merge two closest clusters (iteration 1)
18
• Update distance matrix (iteration 1)
19
• Merge two closest clusters (iteration 2)
20
• Update distance matrix (iteration 2)
21
• Merge two closest clusters/update distance matrix
(iteration 3)
22
• Merge two closest clusters/update distance matrix
(iteration 4)
23
• Final result (meeting termination condition)
24
• Dendrogram tree representation
1. In the beginning we have 6
clusters: A, B, C, D, E and F
6 2. We merge clusters D and F into
cluster (D, F) at distance 0.50
3. We merge cluster A and cluster B
into (A, B) at distance 0.71
lifetime
25
Metode Non-hierarkhi : K-Mean cluster
• MacQueen [25] yang memberikan istilah K-means untuk menjelaskan
suatu algoritma yang mengelompokkan setiap item ke dalam cluster yang
memiliki centroid (mean) terdekat.
• Dalam versi sederhana, langkah-langkah K-Means adalah sebagai berikut:
K=2
X2
Untuk mengetahui apakah terdapat perbedaan antara 2
kelompok sampel atau lebih maka kita bisa menggunakan uji T2-
Hotellings ataupun MANOVA. Namun kita tidak bisa
menjelaskan perbedaan tersebut, sehingga kita tidak bisa
melakukan prediksi pada data baru.
Untuk menjelaskan perbedaan tersebut, kita bisa menggunakan
analisis diskriminan. Dengan mengetahui perbedaan antar
kelompok, maka kita bisa melakukan prediksi pada data baru.
Pada analisis diskriminan, yang menjadi prediktor adalah variabel
kontinyu dan yang menjadi variabel respon (dependen) adalah
variabel kategorik. → Kebalikan dari MANOVA. X1
Jadi, analisis diskriminan adalah analisis yang berusaha untuk
menjelaskan perbedaan antara 2 kelompok data atau lebih.
Contoh kasus:
- Seorang direktur bank ingin mengetahui
perbedaan nasabah yang kreditnya lancar dengan
nasabah yang kreditnya macet dilihat dari
variabel umur nasabah, pendapatan, jumlah
anggota rumah tangga, dan besarnya kredit.
- Seorang dokter melakukan penelitian untuk
mengetahui perbedaan antara pasiennya yang
bisa sembuh dan gagal sembuh.
DASAR-DASAR ANALISIS DISKRIMINAN
Pengamatan Populasi
I II
1 𝒚𝟏𝟏𝟏 𝒚𝟏𝟏𝟏
𝒚𝟏𝟏𝟐 𝒚𝟏𝟏𝟐
𝒚𝟏𝟏 = ⋮ 𝒚𝟐𝟏 = ⋮
𝒚𝟏𝟏𝒑 𝒚𝟏𝟏𝒑
2 𝒚𝟏𝟐𝟏 𝒚𝟏𝟐𝟏
𝒚𝟏𝟐𝟐 𝒚𝟏𝟐𝟐
𝒚𝟏𝟐 = ⋮ 𝒚𝟐𝟐 = ⋮
𝒚𝟏𝟐𝒑 𝒚𝟏𝟐𝒑
⋮ ⋮ ⋮
n 𝒚𝟏𝒏𝟏 𝒚𝟏𝒏𝟏
𝒚𝟏𝒏𝟐 𝒚𝟏𝒏𝟐
𝒚𝟏𝒏 = ⋮ 𝒚𝟐𝒏 = ⋮
𝒚𝟏𝒏𝒑 𝒚𝟏𝒏𝒑
Fungsi diskriminan yang memisahkan 2 kelompok adalah
sebagai berikut:
• Misal terdapat 2 kelompok data dengan p variabel
(y1, …, yp) memiliki matriks kovarians sama namun
memiliki vektor mean berbeda, maka fungsi
diskriminannya adalah suatu kombinasi linier dari
p variabel tersebut yang memaksimalkan jarak
antara kedua vektor mean tersebut, yaitu:
z = a’ y
dimana jarak maksimum antar dua kelompok
terjadi pada
CUTTING SCORE
Prior probabilitiy adalah peluang suatu observasi akan diklasifikasikan ke dalam suatu kelas
tanpa ada pengetahuan apapun mengenai X ➔ apapun nilai X berapa peluang suatu observasi
masuk ke suatu kelas, biasanya berdasarkan data atau penelitian sebelumnya.
Prior probability dari kelas k dinotasikan dengan πk, dengan ∑ πk = 1
Biasanya πk diestimasi dengan frekuensi empiris dari data training, maka:
EXAMPLE
Misal di pabrik baja, baja diproduksi dengan dua rolling temperatures berbeda. Sampel diambil dari
kedua jenis baja yang diproduksi dengan temperatur berbeda tersebut, dan datanya bisa dilihat pada
tabel dibawah. Variabel yang diukur sebagai berikut: y1 = yield point and y2 = ultimate strength.
Figure 3
Ultimate strength and yield
point for steel rolled at two
temperatures.
Pertama kita cek dengan uji t univariat untuk melihat apakah pengamatan bisa dibedakan menurut masing-
masing variabel.
Terlihat dari uji t univariat, kedua jenis baja tidak berbeda signifikan pada setiap dimensi. Namun dari
plot sebelumnya secara visual kita tahu bahwa keduanya bisa dibedakan.
• Karena itu kita perlu mencari suatu fungsi diskriminan dimana titik-titik data bisa diproyeksikan pada
fungsi tersebut, dan tidak overlap antar kedua kelompok.
• Dalam contoh ini, maka kita hanya butuh fungsi diskriminan berdimensi tunggal. Melalui proses
matematis, diperoleh fungsi diskriminan berikut:
Group means:
y1 y2
1 36.4 62.60000
2 39.0 60.42857
$x
LD1 Argumen data ini bisa kita ganti dengan:
1 -1.9573434 - data testing, untuk memvalidasi
2 -0.8815264 fungsi diskriminan
3 -3.3586662 - data baru, untuk mengklasifikasikan
4 -1.0117288 data baru tersebut ke dalam kelas
5 -1.1419312
6 1.0902555
7 0.3895940
8 1.5305121
9 0.8298507
10 0.6996484
11 0.5694460
12 3.2418893
To see posterior probability for
membership:
$class
> fit2<-lda(Temperatur~y1+y2,data=temp, [1] 1 1 1 1 1 2 2 2 2 2 2 2
na.action="na.omit", CV=TRUE) Levels: 1 2
> fit2
$posterior
1 2
1 9.791546e-01 2.084543e-02
2 7.666647e-01 2.333353e-01
3 9.999839e-01 1.609932e-05
4 8.296252e-01 1.703748e-01
5 7.812124e-01 2.187876e-01
6 3.886802e-02 9.611320e-01
7 1.643678e-01 8.356322e-01
8 7.217866e-03 9.927821e-01
9 4.282416e-02 9.571758e-01
10 7.677870e-02 9.232213e-01
11 3.124067e-01 6.875933e-01
12 6.274357e-08 9.999999e-01
$terms
Temperatur ~ y1 + y2
PRAKTIKUM