2011年財經計量分析講習會

2011年財經計量分析講習會
多層次資料的圖形分析法
主講人: 世新大學財金系
郭迺鋒 副教授
簡報製作 林政勳 博士
2011年財經計量分析講習會
1
內容大綱
 所需軟體簡介 (R、Tinn-R、R-studio、Rcmdr)
 文字雲 (Word Cloud)
 R-igraph
 Categorical Data with R-Mosaic Plots
 R Graphical Manual-Star (Spider / Radar)
 R Co-plot
 Google Earth & googleVis
2011年財經計量分析講習會
2
主題一: 所需軟體簡介 (R、Tinn-R、
R-studio、Rcmdr)
2011年財經計量分析講習會
3
所需軟體簡介-R (1/16)
 R is a language and environment for statistical
computing and graphics.
 R provides a wide variety of statistical (linear and
nonlinear modelling, classical statistical tests,
time-series analysis, classification, clustering, ...)
and graphical techniques, and is highly extensible.
 R provides an Open Source route to participation
in that activity.
 Download Site:
http://www.r-project.org/
2011年財經計量分析講習會
4
所需軟體簡介-R (2/16)
首頁
點選進入CRAN
2011年財經計量分析講習會
Taiwan位置提
供三個載點
5
所需軟體簡介-R (3/16)
依據使用者
作業系統下
載主程式
2011年財經計量分析講習會
6
所需軟體簡介-R (4/16)
點選進入base
2011年財經計量分析講習會
7
所需軟體簡介-R (5/16)
下載最新版
本R 2.13.0
2011年財經計量分析講習會
8
所需軟體簡介-R (6/16)
目前R開發團隊建
議使用者安裝32位
元之主程式,囿於
多數R模組尚未支
援64位元系統
2011年財經計量分析講習會
9
所需軟體簡介-R (7/16)
 R安裝步驟一:
2011年財經計量分析講習會
10
所需軟體簡介-R (8/16)
 R安裝步驟二:
2011年財經計量分析講習會
11
所需軟體簡介-R (9/16)
 R安裝步驟三:
2011年財經計量分析講習會
12
所需軟體簡介-R (10/16)
 R安裝步驟四:
2011年財經計量分析講習會
13
所需軟體簡介-R (11/16)
 R安裝步驟五:
2011年財經計量分析講習會
14
所需軟體簡介-R (12/16)
 R安裝步驟六:
2011年財經計量分析講習會
15
所需軟體簡介-R (13/16)
 R安裝步驟七:
2011年財經計量分析講習會
16
所需軟體簡介-R (14/16)
 R安裝步驟八:
2011年財經計量分析講習會
17
所需軟體簡介-R (15/16)
 R安裝步驟九:
2011年財經計量分析講習會
18
所需軟體簡介-R (16/16)
 R安裝步驟十:
2011年財經計量分析講習會
19
所需軟體簡介-RStudio (1/10)
 RStudio™ is a new integrated development
environment (IDE) for R.
 RStudio combines an intuitive user interface
with powerful coding tools to help you get the
most out of R.
 Download Site
http://www.rstudio.org/
2011年財經計量分析講習會
20
所需軟體簡介-RStudio (2/10)
Productive
Runs Everywhere
Free & Open
RStudio功能特色
2011年財經計量分析講習會
21
所需軟體簡介-RStudio (3/10)
首頁
下載主程式
2011年財經計量分析講習會
22
所需軟體簡介-RStudio (4/10)
點選進入下載
2011年財經計量分析講習會
23
所需軟體簡介-RStudio (5/10)
點選下載Rstudio 0.93.92版
2011年財經計量分析講習會
24
所需軟體簡介-RStudio (6/10)
 安裝步驟一:
2011年財經計量分析講習會
25
所需軟體簡介-RStudio (7/10)
 安裝步驟二:
2011年財經計量分析講習會
26
所需軟體簡介-RStudio (8/10)
 安裝步驟三:
2011年財經計量分析講習會
27
所需軟體簡介-RStudio (9/10)
 安裝步驟四:
2011年財經計量分析講習會
28
所需軟體簡介-RStudio (10/10)
 安裝步驟五:
2011年財經計量分析講習會
29
所需軟體簡介Tinn-R (1/14)
 Tinn-R is free, simple but efficient
replacement for the basic code editor
provided by Rgui
 Download Site
http://www.sciviews.org/Tinn-R/
2011年財經計量分析講習會
30
所需軟體簡介Tinn-R (2/14)
首頁
相關網路資源連結
2011年財經計量分析講習會
31
所需軟體簡介Tinn-R (3/14)
點選SourceForge
2011年財經計量分析講習會
32
所需軟體簡介Tinn-R (4/14)
下載最新TinnR_2.3.7.1版本
2011年財經計量分析講習會
33
所需軟體簡介Tinn-R (5/14)
 開啟Tinn-R後,需先設定Rgui路徑
 Options → Application →R →Path
2011年財經計量分析講習會
34
所需軟體簡介Tinn-R (6/14)
路徑改為C:\Program Files\R\R-2.13.0\bin\i386\Rgui.exe
2011年財經計量分析講習會
35
所需軟體簡介Tinn-R (7/14)
 安裝步驟一
2011年財經計量分析講習會
36
所需軟體簡介Tinn-R (8/14)
 安裝步驟二
2011年財經計量分析講習會
37
所需軟體簡介Tinn-R (9/14)
 安裝步驟三
2011年財經計量分析講習會
38
所需軟體簡介Tinn-R (10/14)
 安裝步驟四
2011年財經計量分析講習會
39
所需軟體簡介Tinn-R (11/14)
 安裝步驟五
2011年財經計量分析講習會
40
所需軟體簡介Tinn-R (12/14)
 安裝步驟六
2011年財經計量分析講習會
41
所需軟體簡介Tinn-R (13/14)
 安裝步驟七
2011年財經計量分析講習會
42
所需軟體簡介Tinn-R (14/14)
 安裝步驟八
2011年財經計量分析講習會
43
所需軟體簡介-Rcmdr (1/17)
 簡介
 The R-Commander GUI consists of a window
containing several menus, buttons, and information
fields.
 The R-Commander menus are easily configurable
through a text file or, preferably, through plug-in
packages.
 The menus lead to simple dialog boxes, the general
contents of which are more or less obvious from the
names of the menu items.
 When a new data set is read (from an attached
package or imported), it becomes the active data set.
 參考連結網址:
http://socserv.mcmaster.ca/jfox/Misc/Rcmdr/
2011年財經計量分析講習會
44
所需軟體簡介-Rcmdr (2/17)
 開啟R軟體視窗
2011年財經計量分析講習會
45
所需軟體簡介-Rcmdr (3/17)
 程式套件→安裝程式套件
2011年財經計量分析講習會
46
所需軟體簡介-Rcmdr (4/17)
 選取Taiwan (Taipei 1)
2011年財經計量分析講習會
47
所需軟體簡介-Rcmdr (5/17)
 點選Rcmdr
2011年財經計量分析講習會
48
所需軟體簡介-Rcmdr (6/17)
 Rcmdr模組安裝完成後之畫面
2011年財經計量分析講習會
49
所需軟體簡介-Rcmdr (7/17)
 輸入library(Rcmdr)後按Enter
2011年財經計量分析講習會
50
所需軟體簡介-Rcmdr (8/17)
 R Commander視窗
2011年財經計量分析講習會
51
所需軟體簡介-Rcmdr (9/17)
 開啟內建範例資料
Data→Data in packages →Read Data From Package
2011年財經計量分析講習會
52
所需軟體簡介-Rcmdr (10/17)
 選取datasets下之CO2資料,按OK
2011年財經計量分析講習會
53
所需軟體簡介-Rcmdr (11/17)
 可點選 View data set檢視CO2資料
2011年財經計量分析講習會
54
所需軟體簡介-Rcmdr (12/17)
 圖形一: XY conditioning plot
 Graphs→XY conditioning plot
 點選解釋變數(Explanatory variables)、
反應變數(Response variables)、
Conditions、Groups後按OK
2011年財經計量分析講習會
55
所需軟體簡介-Rcmdr (13/17)
2011年財經計量分析講習會
56
所需軟體簡介-Rcmdr (14/17)
 XY conditioning plot之圖形
2011年財經計量分析講習會
57
所需軟體簡介-Rcmdr (15/17)
 圖形二: Box plot
 Graphs→Box plot
2011年財經計量分析講習會
58
所需軟體簡介-Rcmdr (16/17)
點選Plot by groups可設定分
組變數(Plant),完成後按OK
2011年財經計量分析講習會
59
所需軟體簡介-Rcmdr (17/17)
 Box plot之圖形
2011年財經計量分析講習會
60
主題二: 文字雲
2011年財經計量分析講習會
61
文字雲
 文字雲是用於分析文章內容,然後組合成一
幅圖像,在這幅圖裡會將文章中出現高頻率
文字用比較大的字體顯示,低頻率文字則用
比較小的字體顯示
 相關文字雲網站
 wordcloud (支援中文)
 tagxedo (僅英文)
 wordle (僅英文)
 TagCrowd (僅英文)
 Tag Cloud by R
2011年財經計量分析講習會
62
文字雲-wordcloud (1/8)
 以民國100年5月中央銀行金融穩定報告,連
結網址:
http://www.cbc.gov.tw/content.asp?CuItem=38
685
 開啟一記事本,將該內文複製於該記事本中,
存成txt檔
2011年財經計量分析講習會
63
文字雲-wordcloud (2/8)
 連結網址:
http://timc.idv.tw/wordcloud/zh/
先安裝Google
Chrome
2011年財經計量分析講習會
64
文字雲-wordcloud (3/8)
 安裝後開啟Google Chrome即可連結
2011年財經計量分析講習會
65
文字雲-wordcloud (4/8)
 點選進入後出現:
2011年財經計量分析講習會
66
文字雲-wordcloud (5/8)
 點選進入後出現:
選擇路徑
讀取中文需改成big5
2011年財經計量分析講習會
67
文字雲-wordcloud (6/8)
 初步分析結果:
點選進入
2011年財經計量分析講習會
68
文字雲-wordcloud (7/8)
 主要分析結果:
可縮小文字
2011年財經計量分析講習會
69
文字雲-wordcloud (8/8)
 縮小後之最終結果:
2011年財經計量分析講習會
70
文字雲-tagxedo (1/5)
 連結網址:
http://www.tagxedo.com/
2011年財經計量分析講習會
71
文字雲-tagxedo (2/5)
複製美國聯準會每日講稿
http://www.federalreserve.gov/newsevents/sp
eech/yellen20110609a.htm,選Submit
2011年財經計量分析講習會
72
文字雲-tagxedo (3/5)
2011年財經計量分析講習會
73
文字雲-tagxedo (4/5)
可點選Shape更
換文字雲形狀
2011年財經計量分析講習會
74
文字雲-tagxedo (5/5)
 一些tagxedo文字雲有趣的範例
http://www.tagxedo.com/gallery.html
Michael Jackson
2011年財經計量分析講習會
Titanic
Taj Mahal
75
文字雲-wordle (1/4)
 連結網址:
http://www.wordle.net/
2011年財經計量分析講習會
76
文字雲-wordle (2/4)
點選Go
2011年財經計量分析講習會
複製美國聯準會每日講稿
http://www.federalreserve.gov/newsevents/sp
eech/yellen20110609a.htm
77
文字雲-wordle (3/4)
點選Randomize可
改變文字雲圖案
2011年財經計量分析講習會
78
文字雲-wordle (4/4)
隨機化文字雲圖案
2011年財經計量分析講習會
79
文字雲-TagCrowd (1/3)
 連結網址:
http://www.tagcrowd.com/
複製美國聯準會每日講稿
http://www.federalreserve.gov/newsev
ents/speech/yellen20110609a.htm
2011年財經計量分析講習會
80
文字雲-TagCrowd (2/3)
2011年財經計量分析講習會
部分功能設定
81
文字雲-TagCrowd (3/3)
 分析結果:
2011年財經計量分析講習會
82
文字雲-Tag Cloud by R (1/6)
 簡介
Tag cloud is a bunch of words drawn in a graph
with their sizes proportional to their frequency; it’s
widely used in blogs to visualize tags.
 相關網站連結
 http://yihui.name/en/2009/06/creating-tagcloud-using-r-and-flash-javascript-swfobject/
 http://www.r-bloggers.com/creating-tagcloud-using-r-and-flash-javascript-swfobject/
2011年財經計量分析講習會
83
文字雲-Tag Cloud by R (2/6)
 開啟R軟體視窗
2011年財經計量分析講習會
84
文字雲-Tag Cloud by R (3/6)
 開啟R語法: 檔案輸入→R程式碼→選擇路徑
2011年財經計量分析講習會
85
文字雲-Tag Cloud by R (4/6)
 選擇 Taiwan (Taipei 1) ,按確定
2011年財經計量分析講習會
86
文字雲-Tag Cloud by R (5/6)
 安裝執行中
2011年財經計量分析講習會
87
文字雲-Tag Cloud by R (6/6)
 結果畫面
2011年財經計量分析講習會
88
主題三: R-igraph
2011年財經計量分析講習會
89
R-igraph (1/13)
 igraph為用於網絡分析(network analysis)模組
 igraph模組的主要目標係提供一組資料型態與其
估能用於:
 pain-free implementation of graph
algorithms
 fast handling of large graphs, with millions of
vertices and edges
 allowing rapid prototyping via high level
languages like R
2011年財經計量分析講習會
90
R-igraph (2/13)
 可參考網站資源:
http://cran.r-project.org/web/packages/vcdExtra/
http://igraph.sourceforge.net/igraphbook/
http://igraph.sourceforge.net/
2011年財經計量分析講習會
91
R-igraph-Example (3/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
 Let us suppose that you've collected
information about the social network of a small
group, say ten people. You want to store various
attributes of the people and different types of
relations (like friendship, business relationship,
etc.) among them.
2011年財經計量分析講習會
92
R-igraph-Example (4/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
 The following traits are collected about the people in
the experiment: name, age, gender and R will assign
numeric ids to them.
2011年財經計量分析講習會
93
R-igraph-Example (5/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
 語法一:(讀取資料與安裝模組)
traits <- read.csv("d:/traits.csv", head=FALSE)
rel <- read.csv("d:/relations.csv", head=FALSE)
library(igraph)
g <- graph.empty()
g <- add.vertices(g, nrow(traits),
name=as.character(traits[,1]), age=traits[,2],
gender=as.character(traits[,3]))
2011年財經計量分析講習會
94
R-igraph-Example (6/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
 語法二:(檢查變數資料)
V(g)$name
V(g)$age
V(g)$gender
2011年財經計量分析講習會
95
R-igraph-Example (7/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
2011年財經計量分析講習會
96
R-igraph-Example (8/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
 語法三:(加入姓名編號)
names <- sapply(strsplit(V(g)$name, " "), "[",1)
ids <- 1:length(names)-1
names(ids) <- names
ids
2011年財經計量分析講習會
97
R-igraph-Example (9/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
2011年財經計量分析講習會
98
R-igraph-Example (10/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
 語法四:(利用欄位編號取代姓名編號)
from <- as.character(rel[,1])
to <- as.character(rel[,2])
edges <- matrix(c(ids[from], ids[to]), nc=2)
edges
2011年財經計量分析講習會
99
R-igraph-Example (11/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
2011年財經計量分析講習會
100
R-igraph-Example (12/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
 語法五:(繪製出群體網絡圖)
E(g)$color <- "black"
E(g)[ room=="Y" ]$color <- "red"
tkplot(g, layout=layout.kamada.kawai,
edge.color=E(g)$color)
2011年財經計量分析講習會
101
R-igraph-Example (13/13)
http://igraph.sourceforge.net/igraphbook/igraphbook-creating.html
2011年財經計量分析講習會
102
主題四: Categorical Data with
R Mosaic Plots
2011年財經計量分析講習會
103
Categorical Data with RMosaic Plots (1/24)
 各種可繪製R-Mosaic Plots 軟體環境之
比較
2011年財經計量分析講習會
104
Categorical Data with RMosaic Plots (2/24)
 strucplot架構成分
2011年財經計量分析講習會
105
Categorical Data with RMosaic Plots (3/24)
 關節炎資料 (Arthritis)需先安裝vcd和
vcdextra模組後即可得到
 進入R
 程式套件→安裝程式套件→Taiwan
(Taipei 1) →vcd與vcdExtra
2011年財經計量分析講習會
106
Categorical Data with RMosaic Plots (4/24)
語法一:
library(mosaic)
library(vcd)
(HEC <- structable(Eye ~ Sex + Hair, data =
HairEyeColor))
mosaic(HEC)
2011年財經計量分析講習會
107
Categorical Data with RMosaic Plots (5/24)
Eye
2011年財經計量分析講習會
Hazel
Green
Brown Black BlondRed Brown Black
Hair
Blue
Blond Red
Female
Sex
Male
Brown
108
Categorical Data with RMosaic Plots (6/24)
語法二: (等同語法一)
mosaic(~Sex + Eye + Hair, data = HairEyeColor)
2011年財經計量分析講習會
109
2011年財經計量分析講習會
Hazel
Green
Brown Black Blond Red
Hair
Eye
Blue
Blond Red
Female
Sex
Male
Brown
Brown Black
Categorical Data with RMosaic Plots (7/24)
110
Categorical Data with RMosaic Plots (8/24)
語法三: (圖形完成後再加入其他元素)
mosaic(~Hair + Eye, data = HEC, pop = FALSE)
seekViewport("cell:Hair=Blond")
grid.rect(gp = gpar(col = "red", lwd = 4))
seekViewport("cell:Hair=Blond,Eye=Blue")
grid.circle(r = 0.2, gp = gpar(fill = "cyan"))
2011年財經計量分析講習會
111
Categorical Data with RMosaic Plots (9/24)
Eye
Blue
Hazel Green
Blond
Red
Hair
Brown
Black
Brown
2011年財經計量分析講習會
112
Categorical Data with RMosaic Plots (10/24)
語法四: (UCBAdmissions data with highlighted cells)
(fill_colors <- matrix(c("dark cyan", "gray", "gray",
"dark magenta"), ncol = 2))
mosaic(ucb, gp = gpar(fill = fill_colors, col = 0))
2011年財經計量分析講習會
113
Categorical Data with RMosaic Plots (11/24)
Gender
Female
Rejected
Admit
Admitted
Male
2011年財經計量分析講習會
114
Categorical Data with RMosaic Plots (12/24)
語法五: (Three mosaic plots for the HairEyeColor data
using dierent color palettes. Top: default HSV color
palette. Middle: default HCL color palette. Bottom: a
custom HCL color palette.)
haireye <- margin.table(HairEyeColor, 1:2)
mosaic(haireye, gp = shading_hsv)
mosaic(haireye, gp = shading_hcl)
mosaic(haireye, gp = shading_hcl, gp_args = list(h = c(130, 43),
c = 100, l = c(90, 70)))
mosaic(haireye, shade = TRUE, gp_args = list(interpolate = 1:4))
2011年財經計量分析講習會
115
Categorical Data with RMosaic Plots (13/24)
Eye
Black
Brown
Blue
Hazel
Green
Pearson
residuals:
7.05
Blond Red
Hair
Brown
4.00
2011年財經計量分析講習會
2.00
0.00
-2.00
-4.00
-5.85
p-value =
< 2.22e-16
default HSV color palette
116
Categorical Data with RMosaic Plots (14/24)
Eye
Black
Brown
Blue
Hazel
Green
Pearson
residuals:
7.05
Blond Red
Hair
Brown
4.00
2011年財經計量分析講習會
2.00
0.00
-2.00
-4.00
-5.85
p-value =
< 2.22e-16
default HCL color palette
117
Categorical Data with RMosaic Plots (15/24)
Eye
Black
Brown
Blue
Hazel
Green
Pearson
residuals:
7.05
Blond Red
Hair
Brown
4.00
2011年財經計量分析講習會
2.00
0.00
-2.00
-4.00
-5.85
p-value =
< 2.22e-16
a custom HCL color palette
118
Categorical Data with RMosaic Plots (16/24)
語法六: (The HairEyeColor data-continuous shading)
ipol <- function(x) pmin(x/4, 1)
mosaic(haireye, shade = TRUE, gp_args = list(interpolate = ipol),
labeling_args = list(abbreviate = c(Sex = TRUE)))
2011年財經計量分析講習會
119
Categorical Data with RMosaic Plots (17/24)
Eye
Black
Brown
Blue
Hazel
Green
Pearson
residuals:
7.050
5.620
Hair
Brown
4.180
2.750
1.320
-0.117
Blond Red
-1.550
2011年財經計量分析講習會
-2.980
-4.420
-5.850
p-value =
< 2.22e-16
120
Categorical Data with RMosaic Plots (18/24)
語法七: (The Bundesliga data for 1995)
BL <- xtabs(~HomeGoals + AwayGoals, data = Bundesliga,
subset = Year == 1995)
mosaic(BL, shade = TRUE)
mosaic(BL, gp = shading_Friendly, legend = legend_fixed,
zero_size = 0)
2011年財經計量分析講習會
121
Categorical Data with RMosaic Plots (19/24)
0
3 456
Pearson
residuals:
2.87
AwayGoals
1
2
3 456
2.9
0
2
0
0
AwayGoals
1
2.0
2.0
0.00
0.0
HomeGoals
2
1
HomeGoals
2
1
2.00
0.0
-2.0
-2.0
Non-signicant
chi-square test
2011年財經計量分析講習會
3
Pearson
residuals:
65 4
-2.00
-2.42
p-value =
0.12133
65 4
3
-2.4
using the Friendly
shading and a legend
with fixed bins
122
Categorical Data with RMosaic Plots (20/24)
語法八: (範例:Ovarian cancer survival)
In the following, we demonstrate some of the described
techniques in analyzing a data set originating from (Obel,
1975) about a retrospective study of ovary cancer carried out
in 1973.
Obel E (1975). A Comparative Study of Patients with Cancer of
the Ovary Who Have Survived More or Less Than 10 Years. Acta
Obstetricia et Gynecologica Scandinavica, 55, 429-439.
2011年財經計量分析講習會
123
Categorical Data with RMosaic Plots (21/24)
語法八: (範例:Ovarian cancer survival)
tab <- xtabs(Freq ~ stage + operation + xray + survival, data
= OvaryCancer)
structable(survival ~ ., data = tab)
dpa <- list(var_offset = 1.2, rot = -30, just_leveltext = "left")
pairs(tab, diag_panel_args = dpa)
doubledecker(survival ~ stage + operation + xray, data = tab)
2011年財經計量分析講習會
124
Categorical Data with RMosaic Plots (22/24)
200
150
100
50
0
survival
stage
ea ad
rly va
n
ce
doperation
300
no
200
100
0
rad lim
ica ited
l
200
150
100
50
0
xray
no ye
s
yes
survival
150
100
50
0
no ye
s
Showing mosaic displays for all
pairwise distributions and bar plots
2011年財經計量分析講習會
for all marginal distributions
no
radical
early
yes
no yes no
yes
limited radical
advanced
noyes xray
limitedoperation
stage
The conditional distribution of X-ray,
given operation, given stage, and with
125
survival highlighted.
Categorical Data with RMosaic Plots (23/24)
語法九: (範例:Ovarian cancer survival)
split <- c(TRUE, TRUE, TRUE, FALSE)
mosaic(tab, expected = ~survival + operation * xray * stage,
split_vertical = split)
mosaic(tab, expected = ~(survival + operation * xray) * stage,
split_vertical = split)
2011年財經計量分析講習會
126
Categorical Data with RMosaic Plots (24/24)
stage / xray
noyes
stage / xray
advanced
no
yes
noyes
no
early
yes
noyes
no
advanced
yes
noyes
Pearson
residuals:
1.20
no
Pearson
residuals:
4.36
4.00
no
no
early
yes
0.00
yes
yes
0.00
survival
survival
2.00
-2.00
-4.00
-4.34
p-value =
< 2.22e-16
radical
limited
operation
radical
limited
With residual-based shading for the
(clearly rejected) null model
(survival)(operation, X-ray, stage)
2011年財經計量分析講習會
-1.05
p-value =
0.67229
radical
limited
operation
radical
limited
With residual-based shading for the
hypothesis of survival being independent
of X-ray and operation, given stage. The
hypothesis is not rejected.
127
主題五: R Graphical ManualStar (Spider / Radar)
2011年財經計量分析講習會
128
R Graphical-Star (1/19)
 R Graphical Manual提供R軟體能繪製圖形之
平台
 R Graphical Manual目前提供36,428種圖形、
2,936模組、 58,146函數、 9,528種資料數據
 連結網址:
http://rgm2.lab.nig.ac.jp/RGM2/images.ph
p?show=all&pageID=1743
2011年財經計量分析講習會
129
R Graphical-Star (2/19)
2011年財經計量分析講習會
130
R Graphical-Star (3/19)
輸入Star spider
後點選search
2011年財經計量分析講習會
131
R Graphical-Star (4/19)
以此圖形為例,
點選進入
2011年財經計量分析講習會
132
R Graphical-Star (5/19)
將R語法複製
於貼上R中
2011年財經計量分析講習會
133
R Graphical-Star (6/19)
 語法一
require(grDevices)
stars(mtcars[, 1:7], key.loc = c(14, 2), main =
"Motor Trend Cars : stars(*, full = F)", full =
FALSE)
stars(mtcars[, 1:7], key.loc = c(14, 1.5), main =
"Motor Trend Cars : full
stars()",flip.labels=FALSE)
2011年財經計量分析講習會
134
R Graphical-Star (7/19)
Motor T rend Cars : full stars()
Hornet Sportabout Valiant
Mazda RX4Mazda RX4 WagDatsun 710 Hornet 4 Drive
Duster 360
Merc 240D
Merc 230
Merc 280
Merc 280C
Merc 450SE
Chrysler Imperial Fiat 128
Lincoln Continental
Cadillac Fleetwood
Merc 450SL Merc 450SLC
AMC Javelin Camaro Z28
Dodge Challenger
Toyota Corona
Honda Civic Toyota Corolla
Pontiac Firebird Fiat X1-9
Porsche 914-2Lotus EuropaFord Pantera LFerrari Dino
disp
cyl
hp
Maserati Bora Volvo 142E
mpg
drat
wt
2011年財經計量分析講習會
qsec
135
R Graphical-Star (8/19)
 語法二
## 'Spider' or 'Radar' plot:
stars(mtcars[, 1:7], locations = c(0,0), radius =
FALSE, key.loc=c(0,0), main="Motor Trend Cars",
lty = 2)
2011年財經計量分析講習會
136
R Graphical-Star (9/19)
Motor Trend Cars
disp
cyl
hp
mpg
drat
qsec
wt
2011年財經計量分析講習會
137
R Graphical-Star (10/19)
 語法三
## Segment Diagrams:
palette(rainbow(12, s = 0.6, v = 0.75))
stars(mtcars[, 1:7], len = 0.8, key.loc = c(12, 1.5),
main = "Motor Trend Cars", draw.segments =
TRUE)
stars(mtcars[, 1:7], len = 0.6, key.loc = c(1.5, 0),
main = “Motor Trend Cars”, draw.segments =
TRUE, frame.plot=TRUE, nrow = 4, cex = .7)
2011年財經計量分析講習會
138
R Graphical-Star (11/19)
Motor T rend Cars
Valiant
Hornet 4 Drive
Mazda RX4 Wag
Hornet Sportabout
Datsun 710
Mazda RX4
Merc 450SE
Merc 280
Merc 240D
Merc 230
Duster 360
Merc 280C
Merc 450SL
Fiat 128
Lincoln Continental
Merc 450SLC
Chrysler Imperial
Cadillac Fleetwood
Honda Civic
Dodge Challenger
Toyota Corolla
AMC Javelin
Toyota Corona
Fiat X1-9
Pontiac Firebird
Ferrari Dino
Lotus Europa
Ford Pantera L
Porsche 914-2
disp
Volvo 142E
cyl
mpg
hp
Maserati Bora
drat
2011年財經計量分析講習會
Camaro Z28
qsec
wt
139
R Graphical-Star (12/19)
 語法四
## scale linearly (not affinely) to [0, 1]
USJudge <- apply(USJudgeRatings, 2, function(x) x/max(x))
Jnam <- row.names(USJudgeRatings)
Snam <- abbreviate(substring(Jnam,1,regexpr("[,.]",Jnam) - 1), 7)
stars(USJudge, labels = Jnam, scale = FALSE,
key.loc = c(13, 1.5), main = "Judge not ...", len = 0.8)
stars(USJudge, labels = Snam, scale = FALSE,
key.loc = c(13, 1.5), radius = FALSE)
loc <- stars(USJudge, labels = NULL, scale = FALSE,
radius = FALSE, frame.plot = TRUE,
key.loc = c(13, 1.5), main = "Judge not ...", len = 1.2)
text(loc, Snam, col = "blue", cex = 0.8, xpd = TRUE)
2011年財經計量分析講習會
140
R Graphical-Star (13/19)
Judge not ...
CFMG
DECI
DILG
PREP
FAMI
ORAL
2011年財經計量分析講習會
DMNR
INTG
CONT
WRIT
RTEN
PHYS
141
R Graphical-Star (14/19)
 語法五
## 'Segments':
stars(USJudge, draw.segments = TRUE, scale =
FALSE, key.loc = c(13,1.5))
2011年財經計量分析講習會
142
R Graphical-Star (15/19)
ALEXANDER,J.M.
BERDON,R.I.
BURNS,E.B.
AARONSON,L.H.
ARMENTANO,A.J.
BRACKEN,J.J.
CALLAHAN,R.J.
COHEN,S.S.
DANNEHY,J.F.
DEVITA,H.J.
GRILLO,A.E.
DALY,J.J.
DEAN,H.H.
DRISCOLL,P.J.
HAMILL,E.C.
HULL,T.C.
HADDEN,W.L.JR.
HEALEY.A.H.
LEVISTER,R.L.
LEVINE,I.
MARTIN,L.F.
MCGRATH,J.F.
MISSAL,H.M.
NARUK,H.J.
O'SULLIVAN,T.J.
MIGNONE,A.F.
MULVEY,H.M.
O'BRIEN,F.J.
RUBINOW,J.E.
SATANIELLO,A.G.
SHEA,J.F.JR.
PASKEY,L.
SADEN.G.A.
SHEA,D.M.
SIDOR,W.J.
SPEZIALE,J.A.
STAPLETON,J.F.
TIERNEY,W.L.JR.
WRIGHT,D.B.
SPONZO,M.J.
TESTO,R.J.
WALL,R.A.
DILG
DMNR
CFMG
INTG
DECI
CONT
PREP
RTEN
ZARRILLI,K.J.
FAMI
PHYS
ORAL
WRIT
2011年財經計量分析講習會
143
R Graphical-Star (16/19)
 語法六
## 'Spider':
stars(USJudgeRatings, locations=c(0,0),
scale=FALSE,radius = FALSE,
col.stars=1:10, key.loc = c(0,0), main="US Judges
rated")
2011年財經計量分析講習會
144
R Graphical-Star (17/19)
US Judges rated
CFMGDILGDMNR
DECI
INTG
PREP
CONT
FAMI
RTEN
ORAL
PHYS
WRIT
2011年財經計量分析講習會
145
R Graphical-Star (18/19)
 語法七
## 'Radar-Segments'
stars(USJudgeRatings[1:10,], locations = 0:1,
scale=FALSE,
draw.segments = TRUE, col.segments=0,
col.stars=1:10,key.loc= 0:1,
main="US Judges 1-10 ")
palette("default")
stars(cbind(1:16,10*(16:1)),draw.segments=TRUE,
main = "A Joke -- do *not* use symbols on 2D data!")
2011年財經計量分析講習會
146
R Graphical-Star (19/19)
A Joke -- do *not* use symbols on 2D data!
2011年財經計量分析講習會
147
主題六: R Coplot
2011年財經計量分析講習會
148
R Coplot (1/15)
 Introduction coplot (Conditioning Plots)
This function produces two variants of the
conditioning plots discussed in the reference
below
2011年財經計量分析講習會
149
R Coplot (2/15)
語法一: (Tonga Trench Earthquakes )
## Tonga Trench Earthquakes
coplot(lat ~ long | depth, data = quakes) given.depth <co.intervals(quakes$depth, number=4, overlap=.1)
coplot(lat ~ long | depth, data = quakes,
given.v=given.depth, rows=1)
2011年財經計量分析講習會
150
R Coplot (3/15)
Given : depth
100
175
300
400
185
165
500
175
600
185
-25
-35
-30
lat
-20
-15
-10
165
200
165
175
185
165
175
185
long
2011年財經計量分析講習會
151
R Coplot (4/15)
 利用R graphic manual中之Coplot範例
http://rgm2.lab.nig.ac.jp/RGM2/images.php?query=
2011年財經計量分析講習會
152
R Coplot (5/15)
語法二:
library(car)
coplot(prestige ~ income|education,
panel=panel.car, col="red", data=Prestige)
2011年財經計量分析講習會
153
R Coplot (6/15)
Given : education
8
5000
15000
12
25000
14
0
5000
16
15000
25000
20
80
20
40
60
prestige
40
60
80
0
10
0
5000
15000
25000
income
2011年財經計量分析講習會
154
R Coplot (7/15)
語法三: (安裝模組)
library(SciViews)
png(filename="panels_%03d_med.png", width=480, height=480)
### Name: panels
### Title: More panel plots
### Aliases: panel.reg panel.ellipse panel.cor
### Keywords: aplot
### ** Examples
## Smooth lines in lower graphs and straight lines in upper graphs
pairs(trees, lower.panel = panel.smooth, upper.panel = panel.reg)
## Robust regression lines
require(MASS) # For rlm()
pairs(trees, panel = panel.reg, diag.panel = panel.boxplot, reg.line = rlm, line.col = "blue", line.lwd = 2)
## A Double log graph
pairs(trees, lower.panel = panel.smooth, upper.panel = panel.reg, log = "xy")
## Graph suitables to explore correlations (take care that there are potentially
## many simultaneous tests done here... So, you loose much power is the whole
## analysis... use it just as an indication, nothing more!)
## Pearson's r
pairs(trees, lower.panel = panel.ellipse, upper.panel = panel.cor)
## Spearman's rho (ellipse and straight lines not suitable here!)
pairs(trees, lower.panel = panel.smooth, upper.panel = panel.cor,method = "spearman", span = 1)
## Several groups (visualize how bad it is to consider the whole set at once!)
pairs(iris[, -5], lower.panel = panel.smooth, upper.panel = panel.cor,method = "kendall", span = 1, col = c("red3", "blue3", "green3")[iris$Species])
## Now analyze correlation for one species only
pairs(iris[iris$Species == "virginica", -5], lower.panel = panel.ellipse,upper.panel = panel.cor)
## A coplot with custom panes
coplot(Petal.Length ~ Sepal.Length | Species, data = iris, panel = panel.ellipse)
2011年財經計量分析講習會
155
R Coplot (8/15)
語法四:執行
## Robust regression lines
require(MASS) # For rlm() pairs(trees, panel =
panel.reg, diag.panel = panel.boxplot, reg.line = rlm,
line.col = "blue", line.lwd = 2)
2011年財經計量分析講習會
156
R Coplot (9/15)
70
75
80
85
8 10 12 14 16 18 20
65
65 70 75 80 85
Girth
10 20 30 40 50 60 70
Height
Volume
8
10
12
2011年財經計量分析講習會
14
16
18
20
10
20
30
40
50
60
70
157
R Coplot (10/15)
語法五:
## Several groups (visualize how bad it is to
consider the whole set at once!) pairs(iris[, -5],
lower.panel = panel.smooth, upper.panel =
panel.cor, method = "kendall", span = 1, col =
c("red3", "blue3", "green3")[iris$Species])
2011年財經計量分析講習會
158
R Coplot (11/15)
4.0
0.5
***
0.72
- 0. 077
4.0
Sepal.Length
3.0
1.5
***
0.66
**
**
- 0.19
- 0.16
***
0.81
1.5
2.5
Petal.Length
1 2 3 4 5 6 7
2.0
3.0
Sepal.Width
2.5
4.5 5.5 6.5 7.5
2.0
0.5
Petal.Width
4.5
5.5
6.5
2011年財經計量分析講習會
7.5
1
2
3
4
5
6
7
159
R Coplot (12/15)
語法六:
## given two factors
Index <- seq(length=nrow(warpbreaks)) # to get
nicer default labels coplot(breaks ~ Index | wool *
tension, data = warpbreaks, show.given = 0:1)
coplot(breaks ~ Index | wool * tension, data =
warpbreaks, col = "red", bg = "pink", pch = 21,
bar.bg = c(fac = "light blue"))
2011年財經計量分析講習會
160
R Coplot (13/15)
Given : wool
B
A
10
20
30
40
50
Given : tension
10
30
L
50
70
10
30
M
breaks
50
70
10
30
H
50
70
0
0
10
20
30
40
50
Index
2011年財經計量分析講習會
161
R Coplot (14/15)
語法七:
library(STAR)
## load Purkinje cell data recorded with the NeuroNexus probes
data(mPK)
mPK <- lapply(mPK, as.repeatedTrain)
## get a rate evolution data frame
mPKreDF <- mkREdf(mPK)
## use coplot to display result
coplot(rate ~ time | longitudinal * across,data = mPKreDF,panel=lines)
## Not run:
##D ## make it prettier with with xyplot of package lattice
##D library(lattice)
##D xyplot(rate ~ time | across,data = mPKreDF,groups=longitudinal,panel=panel.lines)
##D xyplot(rate ~ time | across * longitudinal,data = mPKreDF, panel=panel.lines)
##D ## if ggplot2 is installed, try it out
##D library(ggplot2)
##D qplot(time,rate,data=mPKreDF,geom="line",colour=longitudinal,facets=across ~ .)
## End(Not run)
2011年財經計量分析講習會
162
2011年財經計量分析講習會
0
50
100
200
Given : across
neuron 6
neuron 7
neuron 8
200
neuron 5
0 15 30
100
neuron 4
0 15 30
0 15 30
50
neuron 3
0 15 30
0 15 30
0
neuron 2
0 15 30
0 15 30
ctl
neuron 1
0 15 30
rate
R Coplot (15/15)
Given : longitudinal
bicu
300
300
time
163
主題七: Google Earth &
Google Vis
2011年財經計量分析講習會
164
Google Earth (1)
 先下載Google Earth 6.0版,網址:
http://www.google.com/earth/index.html
2011年財經計量分析講習會
165
Google Earth (2)
 點選後儲存至使用者電腦
2011年財經計量分析講習會
166
Google Earth (3)
 執行安裝 Google Earth,安裝過程僅
會出現兩個對話方塊
2011年財經計量分析講習會
167
Google Earth (4)
 如何利用R與Google Earth之連結
 參考範例: London Cycle Hire locations
 網址:
http://rspatialtips.org.uk/
2011年財經計量分析講習會
168
Google Earth (5)
2011年財經計量分析講習會
下載資
料
下載語
法
169
Google Earth (6)
 開啟Rstudio軟體
2011年財經計量分析講習會
170
Google Earth (7)
 開啟下載之語法
File → Open File →
開啟儲存之R語法
2011年財經計量分析講習會
171
Google Earth (8)
 點選Run All
建置一個空的資料夾路徑
將資料放入該空的資料夾中
2011年財經計量分析講習會
172
Google Earth (9)
 結果出現一張散布點圖
2011年財經計量分析講習會
173
Google Earth (10)
 至剛剛建立的資料夾中,會出現一個 .KML
檔案
點選該KML檔即可啟動Google Earth呈現
London Cycle Hire locations的位置圖
2011年財經計量分析講習會
174
Google Earth (11)
2011年財經計量分析講習會
175
Google Earth (12)
 將地圖拉近可讓分布之點位置更清楚
2011年財經計量分析講習會
176
googleVis (1)
 簡介
 googleVis provides an interface between R and the Google
Visualisation API to create interactive charts for web pages.
 The functions of the package allow the user to visualize
data stored in R data frames with the Google Visualization
API without uploading their data to Google.
 The output of a googleVis function is html code that
contains the data and references to JavaScript functions
hosted by Google and can be displayed locally with the R
HTTP help server.
 A browser with Flash and Internet connection is required.
2011年財經計量分析講習會
177
googleVis (2)
 開啟R軟體
2011年財經計量分析講習會
178
googleVis (3)
 開啟語法
檔案 → 輸入R程式碼
→ 開啟儲存之R語法
CRAN mirror中點選
Taiwan (Taipei 1)
2011年財經計量分析講習會
179
googleVis (4)
 出現安裝畫面
2011年財經計量分析講習會
180
googleVis (5)
 安裝完成後的畫面
2011年財經計量分析講習會
181
googleVis (6)
 並產生一網頁呈現結果
2011年財經計量分析講習會
點選播放鑑可呈現不
同年度之動態變化
182
~ 報告完畢 恭請指教 ~
2011年財經計量分析講習會
183