金子邦彦研究室データベースデータサイエンス演習(R システムを使用)(スライド資料とプログラム例)(全13回)

データサイエンス演習(R システムを使用)(スライド資料とプログラム例)(全13回)

大学で使用した自作の資料等を,手直しの上公開している. クリエイティブ・コモンズ BY NC SA.

目次

  1. rd-1. R システムと RStudio [PDF], [パワーポイント], [HTML]
  2. rd-2. ヒストグラム,散布図,折れ線グラフ,要約統計量 [PDF], [パワーポイント], [HTML]
  3. rd-3. 機械学習による自動分類 [PDF], [パワーポイント], [スライドHTML]
  4. rd-4. 平均と分散 [PDF], [パワーポイント], [スライドHTML]
  5. rd-5. t 検定 [PDF], [パワーポイント], [スライドHTML]
  6. rd-6. 相関,相関係数 [PDF], [パワーポイント], [スライドHTML]
  7. rd-7. 次元削減,主成分分析 [PDF], [パワーポイント], [スライドHTML]
  8. rd-8. クラスタリング [PDF], [パワーポイント], [スライドHTML]
  9. rd-9. テーブルデータ処理,並べ替え(ソート)・集計・集約 [PDF], [パワーポイント], [スライドHTML]
  10. rd-10. オブジェクトのセーブとロード [PDF], [パワーポイント], [スライドHTML]
  11. rd-11. リレーショナルデータベースとの連携 [PDF], [パワーポイント], [スライドHTML]
  12. rd-12. CSV ファイルのインポート,エクスポート [PDF], [パワーポイント], [スライドHTML]
  13. rd-13. 正規分布 [PDF], [パワーポイント], [スライドHTML]

Windows での R システムのインストール: 別ページで説明している.

資料

  1. rd-1. R システムと RStudio [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/ZPW1QK-2022-02-25-120228

  2. rd-2. ヒストグラム,散布図,折れ線グラフ,要約統計量 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/ZX4JNZ-2022-02-25-120327

  3. rd-3. 機械学習による自動分類 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/5D2845-2022-02-25-120358

  4. rd-4. 平均と分散 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/KW47Q5-2022-02-25-120436

  5. rd-5. t 検定 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/ZR6RR5-2022-02-25-120503

  6. rd-6. 相関,相関係数 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/52NPVZ-2022-02-25-120530

  7. rd-7. 次元削減,主成分分析 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/Z8MPXZ-2022-02-25-120606

  8. rd-8. クラスタリング [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/53EW25-2022-02-25-120630

  9. rd-9. テーブルデータ処理,並べ替え(ソート)・集計・集約 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/ZQ9WYZ-2022-02-25-120657

  10. rd-10. オブジェクトのセーブとロード [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/57JP7Z-2022-02-25-120733

  11. rd-11. リレーショナルデータベースとの連携 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/KJ8DGK-2022-02-25-120800

  12. rd-12. CSV ファイルのインポート,エクスポート [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/K4JGQ5-2022-02-25-120828

  13. rd-13. 正規分布 [PDF], [パワーポイント]

    ドクセルの URL: https://www.docswell.com/s/6674398749/ZMVDXZ-2022-02-25-120900

動画及びソースコード(上の資料と同じ内容)(動画は,全13回のうち8回分を準備)

  1. rd-1. R システムと RStudio [PDF], [パワーポイント]

    YouTube 動画: https://www.youtube.com/watch?v=o5y1-sslpuQ

  2. rd-2. ヒストグラム,散布図,折れ線グラフ,要約統計量 [PDF], [パワーポイント]

    YouTube 動画: https://www.youtube.com/watch?v=QGg7JCPMZcI

  3. rd-3. 機械学習による自動分類 [PDF], [パワーポイント]

    YouTube 動画: https://www.youtube.com/watch?v=iZtPtATSnYc

    プログラム1

    install.packages("ggplot2")
    install.packages("dplyr")
    install.packages("klaR")
    

    プログラム2

    library(ggplot2)
    ggplot(iris, aes(x=Sepal.Length)) + 
      geom_point( aes(y=Sepal.Width, colour=Species), size=3 ) + 
      theme_bw() 
    

    プログラム3

    library(ggplot2)
    ggplot(iris, aes(x=Petal.Length)) + 
      geom_point( aes(y=Petal.Width, colour=Species), size=3 ) + 
      theme_bw() 
    

    プログラム4

    library(dplyr)
    library(klaR)
    d <- tbl_df(iris[c(3,4,5)])
    partimat(Species~., data=d, method="lda")
    
  4. rd-4. 平均と分散 [PDF], [パワーポイント]

    プログラム1

    x <- round( rnorm(1000000, mean=100, sd=20) )
    x[floor( runif(5, 1, 1000000+1) )]
    x[floor( runif(5, 1, 1000000+1) )]
    x[floor( runif(5, 1, 1000000+1) )]
    

    プログラム2

    x <- round( rnorm(1000000, mean=100, sd=20) )
    m <- numeric(20)
    v <- numeric(20)
    for (i in 1:20) {
      s <- x[floor( runif(5, 1, 1000000+1) )]
      m[i] <- mean(s)
      v[i] <- var(s)
    }
    print(m)
    print(v)
    

    プログラム3

    x <- round( rnorm(1000000, mean=100, sd=20) )
    m <- numeric(20)
    v <- numeric(20)
    for (i in 1:20) {
      s <- x[floor( runif(5, 1, 1000000+1) )]
      m[i] <- mean(s)
      v[i] <- var(s)
    }
    for (i in 1:20) { print( mean(m[1:i]) ) }
    for (i in 1:20) { print( mean(v[1:i]) ) }
    

    YouTube 動画: https://www.youtube.com/watch?v=qUdpcMUdzts

  5. rd-5. t 検定 [PDF], [パワーポイント]

    YouTube 動画: https://www.youtube.com/watch?v=YXHKjzEKjkI

    t.test( c(128, 104, 124, 85, 120), c(100, 106, 89, 89, 105), var.equal=F )
    

    t.test( c(128, 104, 124, 85, 120), c(180, 190, 189, 131, 130, 150), var.equal=F )
    
  6. rd-6. 相関,相関係数[PDF], [パワーポイント]

    YouTube 動画: https://www.youtube.com/watch?v=RQRvQN_V0pU

    x <- rnorm(100000, mean=5, sd=5) 
    y <- rnorm(100000, mean=5, sd=5) 
    d7 <- data.frame( xx=x[floor( runif(100, 1, 100000+1) )], 
      yy=y[floor( runif(100, 1, 100000+1) )] )
    d7$yy <- d7$yy + (d7$xx - d7$yy) * 0.6
    library(ggplot2)
    ggplot(d7, aes(x=xx)) + 
      geom_point( aes(y=yy), size=3 ) + theme_bw() 
    cor(d7$xx, d7$yy)
    

    x2 <- rnorm(100000, mean=5, sd=5) 
    y2 <- rnorm(100000, mean=5, sd=0.1) 
    d10 <- data.frame( xx=x2[floor( runif(100, 1, 100000+1) )], 
      yy=y2[floor( runif(100, 1, 100000+1) )] )
    d10$yy <- 0.1 * d10$xx + d10$yy
    library(ggplot2)
    ggplot(d10, aes(x=xx)) + 
      geom_point( aes(y=yy), size=3 ) + xlim(-5, 15) + ylim(-5, 15) +
      theme_bw()
    cor(d10$xx, d10$yy)
    

    x2 <- rnorm(100000, mean=5, sd=5) 
    y2 <- rnorm(100000, mean=5, sd=0.1) 
    d11 <- data.frame( xx=x2[floor( runif(100, 1, 100000+1) )], 
      yy=y2[floor( runif(100, 1, 100000+1) )] )
    d11$yy <- 0.4 * d11$xx + d11$yy
    library(ggplot2)
    ggplot(d11, aes(x=xx)) + 
      geom_point( aes(y=yy), size=3 ) + xlim(-5, 15) + ylim(-5, 15) +
      theme_bw()
    cor(d11$xx, d11$yy)
    
  7. rd-7. 次元削減,主成分分析 [PDF], [パワーポイント]

    YouTube 動画: https://www.youtube.com/watch?v=5f5QrGBePO0

    前準備
    install.packages("ggplot2")
    install.packages("pcaPP")
    
    
    主成分分析
    x <- rnorm(100000, mean=5, sd=5) 
    y <- rnorm(100000, mean=5, sd=5)
    n <- floor( runif(100, 1, 100000+1) ) 
    d8 <- data.frame( xx=x[n],  yy=y[n] )
    d8$yy <- d8$yy - (d8$xx + d8$yy) * 0.6
    library(ggplot2)
    ggplot(d8, aes(x=xx)) + 
      geom_point( aes(y=yy), size=3 ) + theme_bw() 
    a <- prcomp(d8)
    print(a$rotation)
    
    主成分分析(データの合成の部分を変更)
    x <- rnorm(100000, mean=5, sd=5) 
    y <- rnorm(100000, mean=5, sd=5) 
    n <- floor( runif(100, 1, 100000+1) ) 
    d9 <- data.frame( xx=x[n],  yy=y[n] )
    d9$yy <- d9$yy + (d9$xx - d9$yy) * 0.8
    library(ggplot2)
    ggplot(d9, aes(x=xx)) + 
      geom_point( aes(y=yy), size=3 ) + theme_bw() 
    a <- prcomp(d9)
    print(a$rotation)
    
    主成分分析は外れ値に弱い
    x <- rnorm(100000, mean=5, sd=5) 
    y <- rnorm(100000, mean=5, sd=5) 
    n <- floor( runif(100, 1, 100000+1) ) 
    d9 <- data.frame( xx=x[n],  yy=y[n] )
    d9$yy <- d9$yy + (d9$xx - d9$yy) * 0.8
    d10 <- data.frame( xx=rnorm(10, mean=-20, sd=1), 
      yy=rnorm(10, mean=5, sd = 1) )
    d11 <- rbind( d9, d10 )
    library(ggplot2)
    ggplot(d11, aes(x=xx)) + 
      geom_point( aes(y=yy), size=3 ) + theme_bw() 
    a <- prcomp(d11)
    print(a$rotation)
    
    pcaPP パッケージを用いて robust PCA23:03 2021/05/17
    x <- rnorm(100000, mean=5, sd=5) 
    y <- rnorm(100000, mean=5, sd=5) 
    n <- floor( runif(100, 1, 100000+1) ) 
    d9 <- data.frame( xx=x[n],  yy=y[n] )
    d9$yy <- d9$yy + (d9$xx - d9$yy) * 0.8
    d10 <- data.frame( xx=rnorm(10, mean=-20, sd=1), 
      yy=rnorm(10, mean=5, sd = 1) )
    d11 <- rbind( d9, d10 )
    library(ggplot2)
    ggplot(d11, aes(x=xx)) + 
      geom_point( aes(y=yy), size=3 ) + theme_bw() 
    library(pcaPP)
    a2 <- PCAgrid(d11)
    print(a2$loadings)
    
  8. rd-8. クラスタリング [PDF], [パワーポイント]
  9. rd-9. テーブルデータ処理,並べ替え(ソート)・集計・集約 [PDF], [パワーポイント]
  10. rd-10. オブジェクトのセーブとロード [PDF], [パワーポイント]
  11. rd-11. リレーショナルデータベースとの連携 [PDF], [パワーポイント]
  12. rd-12. CSV ファイルのインポート,エクスポート [PDF], [パワーポイント]

    YouTube 動画: https://www.youtube.com/watch?v=hsHkgfjAHNU

    前準備
    install.packages("dplyr") 
    
    データフレームを CSV ファイルにエクスポート
    library(dplyr)
    d <- data_frame(
        name=c("apple", "rose", "rose", "tomato"), 
        color=c("red", "white", "pink", "red"), 
        price=c(100, 400, 200, 40) )
    write.csv(d, file="C:/hoge/hoge.csv")
    
    ※ 「C:/hoge」は作業用のディレクトリ.
    前もって作成しておくこと.
    
    データフレームに CSV ファイルからインポート
    library(dplyr)
    m <- read.csv(file="C:/hoge/bar.csv")
    print(m)