データ解析基礎論A
講義02:データの可視化
アナウンスメント
質問・コメント → 発言
第1週の発言回数:16/300
E-mailの書き方 題名:[データ解析A] XXXについて 氏名を必ず明記してください(学籍番号は不要)無記名 → ペナルティ (口頭で発表)
Rの基本:データ(ベクトル)の作成
c
(var1,var2,...,varN)
複数のテ ータ(数字等)を1つのオフ シ ェクトとして結合する関数
> x<-c(1,2,3,4)
> x
[1] 1 2 3 4
> x2<-c(x,5,6,7,8)
> x2
[1] 1 2 3 4 5 6 7 8
メモ • 関数:何か入力すると何かを出力してくれる • 必ず () を用いる • 引数:関数に入力するもの • = と <- は同じ役割Rの基本:データ(ベクトル)
c
(var1,var2,...,varN)
複数のテ ータ(数字等)を1つのオフ シ ェクトとして結合する関数
> y=c('a0','a1','b0','b1')
> y
[1] "a0" "a1" "b0" "b1”
> z=
c
(x,y)
> z
[1] "1" "2" "3" "4" "a0" "a1" "b0" "b1"
Rの基本:データ(ベクトル)
rep
(x, times)
繰り返し関数 > x<-rep(1,4) #(1) 1を4回繰り返す [1] 1 1 1 1 > x<-rep(c(1,7,87),3) #(2) [1,7,87]のベクトルを3回繰り返す [1] 1 7 87 1 7 87 1 7 87 > x<-rep(1:4,3) #(3) 1:4のseqを3回繰り返す [1] 1 2 3 4 1 2 3 4 1 2 3 4 > x<-sort(rep(1:4,3)) #(4) (3)を順番に並び替える [1] 1 1 1 2 2 2 3 3 3 4 4 4Rの基本:データ(ベクトル)
seq
(start, end, increment/decrement)
等間隔の数列を作るための関数
> x<-seq(1,4,1)
[1] 1 2 3 4
> x<-seq(0,40,10)
[1] 0 10 20 30 40
> x<-seq(10,2,-2)
[1] 10 8 6 4 2
> x<-1:4 #
[1] 1 2 3 4
> x<-10:1 #
-[1] 10 9 8 7 6 5 4 3 2 1
実習
以下のようなベクトルを作りましょう
[1, 2, 3, 4, 5, 4, 3, 2, 1]
[1, 4, 9, 16, 25]
Rの基本:論理演算
> a=1:10
[1] 1 2 3 4 5 6 7 8 9 10
> which(a<5)
[1] 1 2 3 4
#
aの1,2,3,4番目の要素が5より小さい> b=10:1
[1] 10 9 8 7 6 5 4 3 2 1
> which(b<5)
[1] 7 8 9 10
#bの7,8,9,10番目の要素が5より小さいRの基本:データ(行列)
matrix
(vector, nrow, ncol, byrow):
nrow ncol の行列の作成(通常はべクトルの長さは一定なので、nrow か ncol だけで足りる) byrow はヘクトルを 1 行目から行毎に振り分ける。Default では列毎に振り分けられる。 > x<-matrix(1:8, nrow=2) [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 > x<-matrix(1:8, nrow=2,byrow=T) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8
Rの基本:データフレーム
data.frame
(varName1=vector1, varName2 = vecotr2, …. ):
Matrixは数値、文字、因子が混在できない。 data.frameでは変数内では数値、文字、因子が混在できないが、変数間では数値、文字、因子が 混在できる。 data01<-data.frame(score = c(2,4,3,4), dose = c(rep(10,2),rep(100,2)), condition = rep(c('exp','control'),2)) > data01
score dose condition 1 2 10 exp 2 4 10 control 3 3 100 exp 4 4 100 control
Rの基本:データの入力
read.csv
( FILE_NAME , header=?, row.names=N)
FILE_NAME はディレクトリやURLも含む。
変数の名前がある場合は header=T、無い場合は header=F とする。
dat01<-read.csv("http://www.matsuka.info/data_folder/temp_data01.txt",
header=T)
> dat01
x y z
1 11 12 13
2 21 22 23
3 31 32 33
Rの基本:データの入力
各行に 名前か ある場合は now.names=NでN列目を観測点の名前として使うこともできる。
dat02<-read.csv("http://www.matsuka.info/data_folder/temp_data02.txt",
header=T,
row.name=1
)
> dat02
x y z
katsuo
11 12 13
wakame
21 22 23
tarachan
31 32 33
Rの基本:データの入力2
read.table( FILE_NAME ,header=?,sep= delim ,row.names=N)
基本的にread.csvと同じ。
sep = でdelimiter を定義する(tabの場合は不要)。 例えは delimiter が「;」の場合、sep= ; となる > dat03<-read.table("http://www.matsuka.info/data_folder/temp_data03.txt", header=T, row.name=4) > dat03 x y z sazae 11 12 13 masuo 21 22 23 tarachan 31 32 33
Rの基本:データ(INDEX要素の参照)
参照方法(行列またはmatrixの場合)
M[
参照したい行、参照したい列]
> dat03
x y z
sazae
11 12 13
masuo
21 22 23
tarachan 31 32 33
> dat03[1,1] #1
行、1列目を参照
[1] 11
Rの基本:データ(INDEX)
列を問わず、すべてのn行目を参照したい場合: M[n, 空] 行を問わず、すべてのm列目を参照したい場合: M[空, m] > dat03 x y z sazae 11 12 13 masuo 21 22 23 tarachan 31 32 33 > dat03[2,] #2行目 x y z masuo 21 22 23 > dat03[,1] #1列目 [1] 11 21 31Rの基本:データ(INDEX)
列に名前(変数名)がある場合 M$varName > dat03 x y z sazae 11 12 13 masuo 21 22 23 tarachan 31 32 33 > dat03$x [1] 11 21 31 > dat03$y [1] 12 22 32 > dat03$z [1] 13 23 33Rの基本:データ(INDEX)
変数名を変えたい場合 > dat03 x y z sazae 11 12 13 masuo 21 22 23 tarachan 31 32 33 > colnames(dat03)<-c("var1","var2","var3") > dat03var1 var2 var3
sazae 11 12 13
masuo 21 22 23
tarachan 31 32 33
Rの基本:実習
•
課題:
• Dat03を使用
• scoreという変数にdat03のvar1、var2、var3をベクトルとして入力
• Nameという変数に名前を入力
• Conditionn,それぞれ”var1”,”var2”,”var3”の文字を入力 > dat04
score name condition 1 11 sazae var1 2 21 masuo var2 3 31 tarachan var3 4 12 sazae var1 5 22 masuo var2 6 32 tarachan var3 7 13 sazae var1 8 23 masuo var2 9 33 tarachan var3 > dat03
var1 var2 var3 sazae 11 12 13 masuo 21 22 23 tarachan 31 32 33
Rの基本:データ(INDEX)
•
課題:
• scoreという変数にvar1、var2、var3をベクトルとして入力 • Nameという変数に名前を入力 • Conditionn,それぞれ”var1”,”var2”,”var3”の文字を入力•
解答例
> dat04<-data.frame(score=c(dat03$var1,dat03$var2,dat03$var3),
name=rep(rownames(dat03),3),
condition = rep(c("var1","var2","var3"),3))
Rの基本:応用例
dat<-read.csv("http://www.matsuka.info/data_folder/datWA01.txt",
header=T);
> head(dat)
shoesize
h gender
1 27.0 181.4 M
2 26.5 170.8 M
3 27.5 182.3 M
4 26.5 166.8 M
5 23.5 153.2 F
6 23.0 151.6 F
> head(dat)shoesize height (meter) gender 1 27.0 1.814 M 2 26.5 1.708 M 3 27.5 1.823 M 4 26.5 1.668 M 5 23.5 1.532 F 6 23.0 1.516 F
Rの基本:応用例
mean(dat$shoesize[dat$gender == "M"])
[1] 25.98529
#
男性の足のサイズの平均
mean(dat$shoesize[dat$gender == "F"])
[1] 23.72222
#
女性の足のサイズの平均
mean(dat$shoesize[dat$h > 180])
[1] 27.5
#
身長が180cmより大きい人の足のサイズの平均
アドバイス
データを収集したら、まず可視化!
•
変数の性質・分布の把握
•
外れ値の確認
•
変数間の関係性の把握
•
複雑な関係は分かりにくい場合も多い…
PLOTの基礎
v1 = seq(-3,3,0.1)
v2 = v1^2
基本
> plot(x = v1, y = v2)
色の変更
col=‘color’
> plot(v1, v2,
col = 'red'
)
-3 -2 -1 0 1 2 3 0 2 4 6 8 x y -3 -2 -1 0 1 2 3 0 2 4 6 8 x y
PLOTの基礎
marker
の変更
pch = N
> plot(v1, v2, col=“red”)
> plot(v1, v2, col=“red”,
pch = 20
)
# N: 0~25
# help(points)
PLOTの基礎
marker
のサイズの変更
cex = N
> plot(v1, v2, col=“red”, pch = 20)
PCHの種類
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25RでのPLOTの基礎
スタイルの変更
type=‘style’
> plot(v1, v2)
> plot(v1, v2,
type = ‘l’
)
# style:
p:points
l:line
b:both (points & line)
o:overlay (points & line)
n:none
PLOTの基礎
ラインスタイルの変更
lty = N #N: 1~6
ラインの太さの変更
lwd = W
> plot(v1,v2,type='l',lty=4,lwd=3) # ラインの種類0 = blank, 1 = solid (default) 2 = dashed, 3 = dotted 4 = dotdash, 5 = longdash 6 = twodash -3 -2 -1 0 1 2 3 0 2 4 6 8 x y
PLOTの基礎
タイトルと軸のラベルの挿入
plot(v1, v2,
main
= "THIS IS THE TITLE",
xlab
= "Label for X-axis",
ylab
= "Label for Y-axis")
軸のラベルは必ず明記しましょう!
-3 -2 -1 0 1 2 3 0 2 4 6 8THIS IS THE TITLE
Label for X-axis
La be l f or Y-a xi s
PLOTの基礎
ラベルサイズの変更
plot(v1, v2, main = "THIS IS THE TITLE",
cex.lab = 1.5
,
xlab = "Label for X-axis",ylab = "Label for Y-axis")
PLOTの基礎
X
軸とY軸の範囲の変更
plot(v1, v2, main = "TITLE", xlab = "X here",ylab = "Y here",
xlim
= c(-3.5, 3.5),
ylim
= c(-0.5, 10))
-3 -2 -1 0 1 2 3 0 2 4 6 8 10 TITLE X here Y he re -3 -2 -1 0 1 2 3 0 2 4 6 8THIS IS THE TITLE
Label for X-axis
La be l f or Y-a xi s
PLOTの基礎
応用例
plot(v1, v2, col = "blue", type = "o", lty = 2, pch = 19,
cex.lab = 1.5, lwd = 3, main = "Y=X*X", xlab = "X",
ylab="X*X", xlim=c(-3.5,3.5), ylim=c(-0.5, 10))
1変数の可視化 HISTOGRAM
Histogram
変数の分布の可視化
> dat<- read.csv("http://www.matsuka.info/data_folder/datWA01.txt") > hist(dat$h)
1変数の可視化 HISTOGRAM
Histogram
変数の分布の可視化
> hist(dat$h, breaks = 20, main = “Histogram of Height”, xlab = "Height", col = 'blue', xlim = c(140, 190))
HISTOGRAMの応用
確率密度を可視化dens<-density(dat$h); # 確率密度の算出
hist(dat$h, main = "Histogram of Height", xlab = "Height", xlim = c(140,190), probability = T)
lines(dens, lwd = 2, col = ‘red’, lty=2) #確率密度の可視化
Histogram of Height Height D en si ty 140 150 160 170 180 190 0.00 0.01 0.02 0.03 0.04
LINES
#linesplot(v1, v2, col = "blue", type = "l", pch = 19, cex.lab = 1.5,
lwd = 3, xlab = "X", ylab="f(X)", xlim=c(-3.5,3.5), ylim=c(-0.5, 10)) lines(v1, v1^3, col='red',lwd = 3)
LEGEND
#legend1変数の可視化 BOXPLOT
> boxplot(dat$h,main="Boxplot of Height", ylab="Height",
col='cyan', ylim=c(140,190))
> boxplot(dat$h,main="Boxplot of Height", xlab="Height",
col=‘ornage',
horizontal=T
)
140 150 160 170 180 190 Boxplot of Height HeightRの色の定義
BOXPLOTの応用例
boxplot(
dat$h ~ dat$gender
,
main="Distribution of Height by Gender",
ylab="Gender", xlab="Height", col=c('blue','cyan'),
ylim=c(140,190), horizontal=T)
F
M
140 150 160 170 180 190
Distribution of Height by Gender
Height
BOXPLOTの応用例
dat<-read.table("http://www.matsuka.info/data_folder/aov01.txt") boxplot(dat$h ~ dat$gender + dat$affil,
main="Distribution of Height by Gender and Affiliation",
ylab="Gender x Affiliation", xlab="Height", col=c('blue’,'cyan’,'red’,'magenta'), ylim=c(140,190),horizontal=T) F .cs M. cs F .p sy M. psy 140 150 160 170 180 190
Distribution of Height by Gender and Affiliation
Height G en de r x Af fil ia tio n
INTERACTION PLOT
interaction.plot(dat$gender,
dat$affil,
dat$h,
pch=c(20,20),
col=c("skyblue","orange"),
xlab="gender", ylab=”height",
lwd=3,type='b',cex=2,
trace.label="Affiliation")
X Y LegendINTERACTION PLOT
F .cs M. cs F .p sy M. psy 140 150 160 170 180 190Distribution of Height by Gender and Affiliation
Height G en de r x Af fil ia tio n
HISTOGRAM
で…
par(mfrow=c(1,2)) # figure
を1行2列に分割
Dist. of Height for Female Participants
Height D en si ty 140 150 160 170 180 190 0.00 0.02 0.04 0.06 0.08
Dist. of Height for Male Participants
Height D en si ty 140 150 160 170 180 190 0.00 0.02 0.04 0.06 0.08
HISTOGRAM
で…
hist(dat[dat$gender=='F',]$h, main="Dist. of Height for Female Participants", xlab="Height", xlim=c(140,190), probability=T)
dens.F = density(dat[dat$gender=='F',]$h) lines(dens.F, col='blue',lwd=2)
hist(dat[dat$gender==‘M’,]$h, main=“Dist. of Height for Male
Participants”, xlab=“Height”, xlim=c(140,190), probability=T,ylim=c(0,0.08)) dens.M = density(dat[dat$gender=='M',]$h)
HISTOGRAM
で…
Dist. of Height for Female ParticipantsHeight D en si ty 140 150 160 170 180 190 0.00 0.02 0.04 0.06 0.08
Dist. of Height for Male Participants
Height D en si ty 140 150 160 170 180 190 0.00 0.02 0.04 0.06 0.08
DENSITYのみの表示
par(mfrow=c(1,1))plot(dens.F,col='blue',lwd=2, ylab='density', xlim=c(140,190), main="Dist. of Height by gender",xlab='Height')
lines(dens.M,col='green',lwd=2)
TEXTの挿入
# 凡例: text(x,y,’TEXT’)text(157.5, 0.04, 'Female', col='blue', cex=2) text(170, 0.04,'Male', col='green', cex=2)
140 150 160 170 180 190
0.00
0.02
0.04
0.06
Dist. of Height by gender
Height
de
nsi
実習!
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 252変数の可視化 SCATTERPLOT
#
凡例: plot(x,y,…)
plot(dat$shoesize, dat$h, main="Relationship b/w shoesize and height”,
xlab = 'shoesize’, ylab='height’, pch=19, col='red’)
21 22 23 24 25 26 27 28
150
160
170
180
Relationship b/w shoesize and height
shoesize
SCATTERPLOT 相関係数の挿入
txt =
paste
("r =",
round
(cor(dat$shoesize,dat$h), 4))
> txt[1]
"r = 0.875"
a=1:3
paste("s",a) => [1] "s 1" "s 2" "s 3"
paste(”s”, a, sep=“”) => [1] "s1" "s2" "s3"
SCATTERPLOT 相関係数の挿入
txt = paste("r =", round(cor(dat$shoesize,dat$h), 4))
text(22, 175, txt, cex = 1.5)
21 22 23 24 25 26 27 28 150 160 170 180Relationship b/w shoesize and height
shoesize
height
直線の挿入
abline:
y = a + bxのラインを引く関数
abline
(h = mean(dat$h), col='blue');
abline
(v = mean(dat$shoesize), col='green');
21 22 23 24 25 26 27 28
150
160
170
180
Relationship b/w shoesize and height
shoesize height r = 0.874 mean height mean shoesize H V
直線の挿入
abline(lm(dat$h~dat$shoesize), lty=2, lwd=2)
21 22 23 24 25 26 27 28 150 160 170 180Relationship b/w shoesize and height
shoesize
height
2変数の可視化 SCATTERPLOT
plot(dat[dat$gender=='F',]$shoesize, dat[dat$gender=='F',]$h,
main="Relationship b/w shoesize and height", xlab='shoesize', ylab='height', cex.lab=1.5, pch=19, col='blue', xlim=c(20,29), ylim=c(140,190))
20 22 24 26 28 140 150 160 170 180 190
Relationship b/w shoesize and height
shoesize
height
Female Male
2変数の可視化 SCATTERPLOT
lines(dat[dat$gender=='M',]$shoesize,dat[dat$gender=='M',]$h, type = 'p', pch = 15, col = 'green')
legend("topleft", c('Female','Male'), pch =c(19,15), col = c('blue','green'), cex = 1.5)
20 22 24 26 28 140 150 160 170 180 190
Relationship b/w shoesize and height
shoesize
height
Female Male
SCATTERPLOTの応用例
plot(dat.reg, pch=20, col=c('blue')) material 100 140 180 50 100 150 200 2 4 6 8 10 100 140 180 price design 10 30 50 70 2 4 6 8 10 50 100 150 200 10 20 30 40 50 60 70 salesSCATTERPLOTの応用例
plot(dat.pca, pch = rownames(dat.pca), cex = 1.7, col = 'blue')
writing 40 50 60 70 80 90 a b c d e f g h i 40 50 60 70 80 90 a b c d e f g h i 40 50 60 70 80 90 a b c d e f g h i thesis a b c d e f g h i 40 50 60 70 80 90 a b c d e f g h i a b c de f g h i 40 50 60 70 80 90 40 50 60 70 80 90 interview