金子邦彦研究室プログラミングR のプログラム例R システムで多次元尺度法を行う(R システム,stats を使用)

R システムで多次元尺度法を行う(R システム,stats を使用)

距離行列をもとに、2次元あるいは3次元の空間中の座標値を求める

関連する外部ページ

R システムの CRAN の URL: https://cran.r-project.org/

前準備

R システムのインストール

R システムの CRAN の URL: https://cran.r-project.org/

stats パッケージのインストール

R システムで,次のコマンドを実行し,インストールする.

このとき「Secure CRAN mirrors」のような,ミラーサイトの選択画面が出たときは「Japan」のものを選ぶ.

install.packages("stats") 

この操作でインストールが行われる. R システムのパッケージのインストールについては、 必要に応じて「R システムでのパッケージのインストール」のページを参考にしてください.

多次元尺度法(R システム,stats パッケージを使用)

距離行列データ eurodist (R システムに組み込み済み)を使用

  1. eurodist の読み込み
    data(eurodist)
    
  2. 多次元尺度法の実行,確認
    plot(cmdscale(eurodist, k=2)) 
    text(cmdscale(eurodist, k=2), labels(eurodist)) 
    

データから距離行列を求め,多次元尺度法を実行

http://www.stat.duke.edu/~mw/data-sets/ts_data/gdp

Per capita annual GDP for several countries during 1950-1983 (first row is 1950, last is 1983)

  1. 次のデータを、ファイル名 /tmp/gdp に保存する
    YEAR   AUSTRIA    CANADA      FRANCE      GERMANY    GREECE      ITALY      SWEDEN      UK         USA 
    1950  0.027523   3.651109   10.652861   5.725433   18.423605   0.799001   17.072701   1.033571   4.470303
    1951  0.029406   3.734242   11.186672   6.256754   19.86624    0.829484   17.445339   1.060015   4.734335
    1952  0.029357   3.932222   11.480235   6.70308    19.750938   0.859817   17.011088   1.104598   4.826502
    1953  0.030603   4.019939   11.688318   7.256435   22.217731   0.916962   18.063728   1.152221   4.981746
    1954  0.033678   3.860731   12.092329   7.72644    22.690231   0.942153   19.031748   1.191948   4.79081
    1955  0.037379   4.118041   12.561118   8.570349   24.180141   0.996193   19.471495   1.227778   5.032075
    1956  0.039888   4.364902   13.194351   9.076571   26.030506   1.034456   19.972933   1.242018   5.052481
    1957  0.042246   4.339562   13.843744   9.45931    27.510994   1.081672   20.300936   1.260623   5.056624
    1958  0.043644   4.312412   14.090829   9.665697   28.514622   1.12602    20.645313   1.257567   4.94959
    1959  0.04471    4.382468   14.370371   10.259906  29.260595   1.189722   21.60813    1.355527   5.154055
    1960  0.048402   4.388408   15.202785   10.608815  30.270325   1.272361   22.353876   1.355527   5.160474
    1961  0.050809   4.433521   15.87347   11.032132   33.36092    1.367904   23.50532    1.38902    5.160474
    1962  0.051825   4.645716   16.031708   11.384714  33.671993   1.443632   24.377281   1.38912    5.193368
    1963  0.053638   4.812286   17.221306   11.611703  36.945164   1.514033   25.505919   1.43379    5.401093
    1964  0.056334   5.024269   18.156282   12.266443  39.855228   1.544253   27.066692   1.495286   5.540345
    1965  0.0576     5.267964   18.84903   12.813883   43.389545   1.582865   27.916861   1.520258   5.743346
    1966  0.060427   5.534567   19.66807   13.016213   45.699326   1.666571   28.267708   1.54362    6.013005
    1967  0.06178    5.622673   20.430552   12.964814  47.640087   1.77485    29.047148   1.574283   6.400414
    1968  0.064213   5.845109   21.144066   13.730252  50.669716   1.879574   29.944136   1.632249   6.620659
    1969  0.068004   6.062678   22.442293   14.665157  55.482616   1.991844   31.150602   1.649105   6.725011
    1970  0.072594   6.135059   23.514891   15.392277  59.759922   2.073722   32.497948   1.682107   6.637999
    1971  0.076331   6.480482   24.554897   15.720841  63.738987   2.093974   32.216473   1.720178   6.744059
    1972  0.080461   6.786958   25.777374   16.197464  68.946114   2.145735   32.648853   1.756078   7.055923
    1973  0.08423    7.215567   26.945887   16.907173  73.663345   2.280728   33.686001   1.8913     7.368125
    1974  0.087683   7.383474   27.632845   16.97702   70.721825   2.360058   35.004044   1.862496   7.207006
    1975  0.086589   7.337132   27.558758   16.72403   74.304443   2.261467   35.140991   1.844774   7.06805
    1976  0.092116   7.652321   28.808256   17.6721    77.985801   2.383081   35.404119   1.912678   7.396588
    1977  0.095514   7.765014   29.497534   18.195684  79.430229   2.429625   34.391346   1.938803   7.697297
    1978  0.09698    7.960485   30.475645   18.798212  83.294701   2.473979   35.241451   1.999929   7.954296
    1979  0.101718   8.179226   31.415031   19.640699  85.300865   2.587674   37.065807   2.047829   8.10547
    1980  0.104779   8.160562   31.583988   19.935295  85.949501   2.682996   37.607349   2.000467   7.974804
    1981  0.104412   8.382785   31.479906   19.903635  84.903244   2.683531   37.435204   1.975381   8.164851
    1982  0.105347   7.923615   31.034103   19.723139  84.274193   2.665435   37.72337    2.013859   7.843721
    1983  0.107894   8.065477   32.095989   19.985983  84.016556   2.825328   38.665154   2.07901    7.999103
    
  2. R のプログラム
    # read data
    gdp <- read.table("/tmp/gdp")
    t <- gdp[-1,]
    a1 <- as.ts(t$V2)
    a2 <- as.ts(t$V3)
    a3 <- as.ts(t$V4)
    a4 <- as.ts(t$V5)
    a5 <- as.ts(t$V6)
    a6 <- as.ts(t$V7)
    a7 <- as.ts(t$V8)
    a8 <- as.ts(t$V9)
    a9 <- as.ts(t$V10)
    
    # scatter plot
    plot(a1, col=1, xlim=c(0,35), ylim=c(0,100))
    par(new=T)
    plot(a2, col=2, xlim=c(0,35), ylim=c(0,100))
    par(new=T)
    plot(a3, col=3, xlim=c(0,35), ylim=c(0,100))
    par(new=T)
    plot(a4, col=4, xlim=c(0,35), ylim=c(0,100))
    par(new=T)
    plot(a5, col=5, xlim=c(0,35), ylim=c(0,100))
    par(new=T)
    plot(a6, col=6, xlim=c(0,35), ylim=c(0,100))
    par(new=T)
    plot(a7, col=7, xlim=c(0,35), ylim=c(0,100))
    par(new=T)
    plot(a8, col=8, xlim=c(0,35), ylim=c(0,100))
    par(new=T)
    plot(a9, col=9, xlim=c(0,35), ylim=c(0,100))
    
    par(new=F)
    spectrum(a1, col=1, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    par(new=T)
    spectrum(a2, col=2, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    par(new=T)
    spectrum(a3, col=3, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    par(new=T)
    spectrum(a4, col=4, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    par(new=T)
    spectrum(a5, col=5, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    par(new=T)
    spectrum(a6, col=6, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    par(new=T)
    spectrum(a7, col=7, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    par(new=T)
    spectrum(a8, col=8, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    par(new=T)
    spectrum(a9, col=9, xlim=c(0,0.5), ylim=c(0.000001, 1), method="pgram")
    
    # distance matrix
    s <- matrix(1:162, 9, 18)
    s[1,] <- spectrum(a1, method="pgram")$spec
    s[2,] <- spectrum(a2, method="pgram")$spec
    s[3,] <- spectrum(a3, method="pgram")$spec
    s[4,] <- spectrum(a4, method="pgram")$spec
    s[5,] <- spectrum(a5, method="pgram")$spec
    s[6,] <- spectrum(a6, method="pgram")$spec
    s[7,] <- spectrum(a7, method="pgram")$spec
    s[8,] <- spectrum(a8, method="pgram")$spec
    s[9,] <- spectrum(a9, method="pgram")$spec
    
    # MDS 
    d <- matrix(1:81, 9, 9) 
    for (i in 1:9)
      for (j in 1:9)
        d[i,j] <- ( 1 / cor(s[i,], s[j,]) ) - 1
    
    
    plot( cmdscale(d), col=1:9 )
    text( cmdscale(d),  c("AUSTRIA", "CANADA", "FRANCE", "GERMANY", "GREECE", "ITALY", "SWEDEN", "UK", "USA"), col=1:9 )
    
    dist(cmdscale(d), diag=TRUE, upper=TRUE)
    cor(as.vector(as.matrix(dist(cmdscale(d), diag=TRUE, upper=TRUE))), as.vector(as.matrix(d)))