3 ( ) R 3 1 61, 2016/4/7( ), 4/14( ), 4/21( ) 1 1 2 1 2.1 R, ( )................ 2 2.2 ggm............................ 3 2.3,................ 4 2.4...................................... 6 2.5 1 ( ).................... 7 2.6...................................... 8 2.7 2 ( )................. 8 2.8................................ 10 2.9............................... 14 3 14 1,,.,,.,,. 2.,, 2 (p. 10). 1, 6 356, matsui@mist.i.u-tokyo.ac.jp; TA. http://www.sr3.t.u-tokyo.ac.jp/matsui/2016s/2016s jikken3.html 1
2.1 R, ( ), http://cran.md.tsukuba.ac.jp/, Download R for Windows base. R (2014-4-17 ) R-3.1.0-win.exe, 2.,.,.,. R > 3*5 # [1] 15 > c(19,76)+c(11,13) # [1] 30 89 > x <- c(3,1,4,1,5,9) # > 1:4 # [1] 1 2 3 4 > 1:4+1 # (1:4)+1 [1] 2 3 4 5 > f <- function(x,y){ sqrt(x^2+y^2) } # > f(3,4) [1] 5 R,. 1....,. 2. R...,., browser()., R, URL. http://cse.naro.affrc.go.jp/takezawa/r-tips/r.html 2 PC R 2
1. R R web 3 sum, prod, cumsum abs, log, sqrt mean, sd, min, median, max scale combine, c list, matrix, array %*% t rbind, cbind diag cor det or apply solve read.table, write.table plot, pairs EPS dev.copy2eps which pnorm, rnorm ls, rm browser for, while, repeat 2.2 ggm, ggm (Graphical Gaussian Models). 1. R ( Japan (Tsukuba) ). 2. ggm. 3. package ggm successfully unpacked and MD5 sums checked. graph RBGL graph R : graph > source("http://bioconductor.org/bioclite.r") > bioclite("graph") RBGL 3 3
RBGL > source("http://bioconductor.org/bioclite.r") > bioclite("rbgl").,., R 1.,,,. > library(ggm) 2.3,,.,,,.., ggm marks,.. > data(marks) > marks mechanics vectors algebra analysis statistics 1 77 82 67 67 81 2 63 78 80 70 81 3 75 73 71 66 81 4 55 72 63 70 68... ( )... 87 5 26 15 20 20 88 0 40 21 9 14 88 5, 5, 88.,., (X t,i ) 1 t n,1 i p (n = 88, p = 5). X 2,3 = 80. summary (min), 1 (1st Qu.), = 2 (median), (mean), 3 (3rd Qu.), (max)., X i = 1 n 4 n t=1 X t,i
, 25%, 50%, 75%. > summary(marks) mechanics vectors algebra analysis statistics Min. : 0.00 Min. : 9.00 Min. :15.00 Min. : 9.00 Min. : 9.00 1st Qu.:30.00 1st Qu.:42.00 1st Qu.:45.00 1st Qu.:35.75 1st Qu.:31.00 Median :41.50 Median :51.00 Median :50.00 Median :49.00 Median :40.00 Mean :38.97 Mean :50.59 Mean :50.60 Mean :46.68 Mean :42.31 3rd Qu.:49.25 3rd Qu.:60.00 3rd Qu.:57.25 3rd Qu.:57.00 3rd Qu.:51.50 Max. :77.00 Max. :82.00 Max. :80.00 Max. :70.00 Max. :81.00 4. (r i,j ) r i,j = s i,j si,i s j,j, s i,j = 1 n (X t,i n X i )(X t,j X j ) t=1 > cor(marks) mechanics vectors algebra analysis statistics mechanics 1.0000000 0.5526975 0.5462281 0.4096365 0.3894430 vectors 0.5526975 1.0000000 0.6096447 0.4850813 0.4364487 algebra 0.5462281 0.6096447 1.0000000 0.7108059 0.6647357 analysis 0.4096365 0.4850813 0.7108059 1.0000000 0.6071743 statistics 0.3894430 0.4364487 0.6647357 0.6071743 1.0000000 > round(cor(marks),3) # 3 mechanics vectors algebra analysis statistics mechanics 1.000 0.553 0.546 0.410 0.389 vectors 0.553 1.000 0.610 0.485 0.436 algebra 0.546 0.610 1.000 0.711 0.665 analysis 0.410 0.485 0.711 1.000 0.607 statistics 0.389 0.436 0.665 0.607 1.000 i j,,. R,., ( ) 1. marks,, mechanics statistics., 2,., vectors mechanics statistics,. 4,. 5
20 60 10 30 50 70 mechanics 0 20 60 20 60 vectors algebra 20 40 60 80 10 30 50 70 analysis statistics 10 40 70 0 20 60 20 40 60 80 10 40 70 1:. > pairs(marks), 2,.,,,.,, 2. 2.4 G, V E V V., (= G = (V, E)). V = {a, b, c, d}, E = {(a, b), (a, c), (b, d)} ggm., drawgraph adjust TRUE,.. > amat <- UG(~ a*b + a*c + b*d) > amat a b c d a 0 1 1 0 b 1 0 0 1 c 1 0 0 0 d 0 1 0 0 > drawgraph(amat,adjust=false) d a c b 6
2.5 1 ( ),, ( ),,., web (1997),., R = (r i,j ), R 1 = (r i,j )., p i,j = r i,j r i,i r j,j (i j), 1 (i = j) (1), i j. P = (p i,j )., ( ) 1.,.,, ( )., S1 = mechanics, S2 = vectors, S3 = algebra, S4 = analysis, S5 = statistics. 1: ( ) ( ). S1 S2 S3 S4 S5 S3 0.546 0.61 1 S4 0.41 0.485 0.711 1 S5 0.389 0.436 0.665 0.607 1 S1 S2 S3 S4 S5 S2 0.328 S3 0.229 0.282 S4-0.001 0.078 0.432 S5 0.026 0.02 0.357 0.253 2. 2, R, P. I cor2par <- function(r){ X <- solve(r) p <- nrow(r) P <- matrix(0,p,p) dimnames(p) <- dimnames(r) for(i in (1:p)){ for(j in (1:p)){ if(i!= j) P[i,j] <- -X[i,j]/sqrt(X[i,i]*X[j,j]) if(i == j) P[i,j] <- 1 }} P } 7
II cor2par <- function(r){ X <- solve(r) d <- sqrt(diag(x)) P <- -X / (d %*% t(d)) diag(p) <- 1 P } 2.6, 4 8.,,,, 47 4. StatLib http://e-stat.go.jp/ http://lib.stat.cmu.edu/datasets/ web. : "math" "phys" "chem" "eng" "1" 30 40 50 60 "2" 20 0 10 70 "3" 40 60 90 45 "4" 30 60 20 70 "5" 40 50 20 70 "6" 50 70 30 80 mark4.txt R...,, : > X <- read.table("mark4.txt") 3.,. 2.9. ( Word ) 2.7 2 ( ) i j p i,j, 8
i j, i j.. 1 ( ).., i j,, p i,j., i j p i,j = 0., p i,j = 0, i j,.,, analysis mechanics (p 41 = 0.001) 0.,, vectors, algebra, statistics, analysis mechanics.,, 5.,.. 2.. 1,, 10 4.,, 2.., 2. 2 1,,. 2: ( ) ( ) S1 S2 S3 S4 S5 S3 0.546 0.61 1 S4 0.388 0.433 0.711 1 S5 0.363 0.405 0.665 0.607 1 S1 S2 S3 S4 S5 S2 0.331 S3 0.235 0.327 S4 0 0 0.451 S5 0 0 0.364 0.256,. (1997) ( [2]). 2 ( )., a, b, s, a b s (a b s, )., s, a b. 5,.,. 9
2:., a = {vectors}, b = {statistics}, s = {algebra}, 2 2., algebra, vectors statistics (mechanics analysis )., a = {mechanics,vectors}, b = {analysis,statistics}, s = {algebra}, 2., mechanics vectors algebra. 4. a = {mechanics}, b = {statistics}, s = {vectors,analysis} a b s 2.8 2.7 (, 1 2 )., web.,,,., R, n ( n = 88). AIC ( ) G, G M = M(G), P = P (G),.., G, G,,., AIC (Akaike s Information Criterion) ( ), AIC = 2 ( ) 2 ( ) + ( ) 10
., AIC 0 ( web )., ggm fitcongraph. 2, AIC. fitcongraph, AIC > options(digits=3) # 3 > X <- marks # > n <- nrow(x); p <- ncol(x) # > R <- cor(x) # > amat <- matrix(1,p,p)-diag(p); #, # amat <- UG(~a*b*c*d*e). > dimnames(amat) <- dimnames(r) # > amat[4,1] <- amat[1,4] <- 0 # (4,1) > amat[4,2] <- amat[2,4] <- 0 # (4,2) > amat[5,1] <- amat[1,5] <- 0 # (5,1) > amat[5,2] <- amat[2,5] <- 0 # (5,2) > amat # mechanics vectors algebra analysis statistics mechanics 0 1 1 0 0 vectors 1 0 1 0 0 algebra 1 1 0 1 1 analysis 0 0 1 0 1 statistics 0 0 1 1 0 > f <- fitcongraph(amat,r,n) # > f # $Shat # mechanics vectors algebra analysis statistics mechanics 1.000 0.553 0.546 0.388 0.363 vectors 0.553 1.000 0.610 0.433 0.405 algebra 0.546 0.610 1.000 0.711 0.665 analysis 0.388 0.433 0.711 1.000 0.607 statistics 0.363 0.405 0.665 0.607 1.000 $dev # [1] 0.9 $df # [1] 4 $it # [1] 2 > f$dev # [1] 0.9 > aic <- f$dev - 2*f$df # AIC > aic [1] -7.1 11
. 1. G ( ). M = R. AIC 0 AIC. 2. M (R ) P = (p i,j ) ( 2 ). 3. G (i, j), p i,j (i, j), G. 4. G M = M(G) AIC = AIC(G) (fitcongraph fitcongraph M R ). 5. AIC 2. AIC,. 5. 3 G (i, j), p ij (i, j),. G amat., select.ij <- function(p,amat){ p <- nrow(p); minabsp <- Inf for(i in (2:p)){ for(j in (1:(i-1))){ if(amat[i,j] == 1 && abs(p[i,j]) < minabsp){ minabsp <- abs(p[i,j]); i0 <- i; j0 <- j }}} c(i0,j0) },. 1,. M 0, P 0. M 0 S1 S2 S3 S4 S5 S3 0.546 0.61 1 S4 0.41 0.485 0.711 1 S5 0.389 0.436 0.665 0.607 1 P 0 S1 S2 S3 S4 S5 S2 0.328 S3 0.229 0.282 S4-0.001 0.078 0.432 S5 0.026 0.02 0.357 0.253 P 0 AIC 0 AIC 0 = 0. P 0, (4,1) 0.001. (4,1). fitcongraph, AIC, 12
M 1 S1 S2 S3 S4 S5 S3 0.546 0.61 1 S4 0.41 0.485 0.711 1 S5 0.389 0.436 0.665 0.607 1 P 1 S1 S2 S3 S4 S5 S2 0.328 S3 0.229 0.282 S4 0 0.078 0.432 S5 0.025 0.02 0.357 0.253. AIC 1 AIC 0. AIC 1 = 2 P 1, (5,2) 0.02 (P 0 P 1 )., (5,2), M 2 S1 S2 S3 S4 S5 S3 0.546 0.61 1 S4 0.411 0.485 0.711 1 S5 0.389 0.426 0.665 0.607 1 P 2 S1 S2 S3 S4 S5 S2 0.329 S3 0.225 0.289 S4 0 0.082 0.428 S5 0.032 0 0.362 0.254 AIC 2 = 3.96. AIC 2 AIC 1,., P 2 (5, 1) (0.032), M 3 S1 S2 S3 S4 S5 S3 0.546 0.61 1 S4 0.406 0.485 0.711 1 S5 0.368 0.419 0.665 0.607 1 P 3 S1 S2 S3 S4 S5 S2 0.33 S3 0.24 0.286 S4 0 0.085 0.424 S5 0 0 0.372 0.255 AIC 3 = 5.86 P 3 (4, 2) (0.085), M 4 S1 S2 S3 S4 S5 S3 0.546 0.61 1 S4 0.388 0.433 0.711 1 S5 0.363 0.405 0.665 0.607 1 P 4 S1 S2 S3 S4 S5 S2 0.331 S3 0.235 0.327 S4 0 0 0.451 S5 0 0 0.364 0.256 AIC 4 = 7.1 P 4 (3, 1) (0.235), M 5 S1 S2 S3 S4 S5 S3 0.337 0.61 1 S4 0.24 0.433 0.711 1 S5 0.224 0.405 0.665 0.607 1 P 5 S1 S2 S3 S4 S5 S2 0.465 S3 0 0.391 S4 0 0 0.464 S5 0 0 0.374 0.256 AIC 5 = 0.208 13
. AIC 5 > AIC 4, P 4. 2. 6.,,, P 0 P 1,. web.) 2.9 3, 2.3, 2.8. ( ) 3 4 (zip )., PDF, or Word. (2.3 ). (2.3 ). (2.8 ), AIC..... matsui@mist.i.u-tokyo.ac.jp 2016/4/28( ).,. 3,. [1] (2004), The R Book R,. [2] (1997),,. 14