Principal Component Analysis  Demo
 Using Simulated examples

Principal Component Analysis Demo
Using Simulated examples

;; Tip 1 : If you are in a hurry, click me  and save the file. Then load
;; the saved file into xlisp-stat . Then the data are now generated
;; You may then skip (1) and go to (2) directly.

;; Tip 2 ; If you are in a super-hurry,  click me  and save the file. Then load
;; the saved file into xlisp-stat . Then the data are now generated and automatically analyzed using pca-models
;; you have to answer some simple questions.
 
 
 

;;(1) Generate Data : Step by Step instruction:

;; 1.a. Generate 100 cases of u1 from uniform(0,1) by typing

(def u1 (uniform-rand 1000))

;; 1.b similarly,

(def u2 (uniform-rand 1000))

;; 1.c. Generate the inner disk
;; Define x = u2 cos( 2 pi u1), y= u2 sin( 2 pi u1) by typing

(def x (* u2 (cos (* 2 pi u1) ) ))
(def y (* u2 (sin (* 2 pi u1)) ))

;; 1.d. Generate the outer ring

(def ox (* (+ u2 1.5) (cos (* 2 pi u1) )))
(def oy (* (+ u2 1.5) (sin (* 2 pi u1 ) )))

;;1.f put two rings together

(def x (append x ox))
(def y (append y oy))

;;1.g. Now you can see the scatterplot by typing

(plot-points x y)

;; 1.h. Suppose you are given four variables
;; x1= y ; x2= x + 7.5y ; x3= x - 7.5y; x4= x + 9y

(def x1 y)
(def x2 (+ x (* 7.5 y)))
(def x3 (- x (* 7.5 y)))
(def x4 (+ x (* 9 y)))

;; 1.k their scatterplots look like this :

(scatterplot-matrix (list x1 x2 x3 x4))

;; Note: in the plot, var0 denotes x, var1 denotes y, var 2 denotes u, and ;;so on;
;; remember in lisp language, everything begins with 0 .
 
 

;;(2). Apply PCA-model to reduce dimension by typing

(pca-model (list x1 x2 x3 x4))

;;(3). To see what happens by adding small errors; try this

(def x1 (+ x1 (* (normal-rand 2000) .1)))
(def x2 (+ x2 (* (normal-rand 2000) .1)))
(scatterplot-matrix (list x1 x2 x3 x4))
(pca-model (list x1 x2 x3 x4) )