iris_demo

takahashi — Jun 16, 2012, 5:01 PM

## レポート書きましょう。

# ライブラリ読み込み
library(plyr)
library(ggplot2)
library(reshape2)
library(grid)

# データ読み込み
data(iris)

# データ確認
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

# 種ごとの平均
ddply(iris, .(Species), colwise(mean))
     Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1     setosa        5.006       3.428        1.462       0.246
2 versicolor        5.936       2.770        4.260       1.326
3  virginica        6.588       2.974        5.552       2.026
# 種ごとの分散
ddply(iris, .(Species), colwise(var))
     Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1     setosa       0.1242     0.14369      0.03016     0.01111
2 versicolor       0.2664     0.09847      0.22082     0.03911
3  virginica       0.4043     0.10400      0.30459     0.07543

# 分散分析
for (v in names(iris)[1:4]) {
  cat("\n---", v, "---\n")
  print(summary(aov(as.formula(paste(v, "~ Species")), data = iris)))
}

--- Sepal.Length ---
             Df Sum Sq Mean Sq F value Pr(>F)    
Species       2   63.2   31.61     119 <2e-16 ***
Residuals   147   39.0    0.27                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

--- Sepal.Width ---
             Df Sum Sq Mean Sq F value Pr(>F)    
Species       2   11.3    5.67    49.2 <2e-16 ***
Residuals   147   17.0    0.12                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

--- Petal.Length ---
             Df Sum Sq Mean Sq F value Pr(>F)    
Species       2    437   218.6    1180 <2e-16 ***
Residuals   147     27     0.2                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

--- Petal.Width ---
             Df Sum Sq Mean Sq F value Pr(>F)    
Species       2   80.4    40.2     960 <2e-16 ***
Residuals   147    6.2     0.0                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

# 種ごとの平均プロット(SDエラーバー付き)

ggplot(melt(iris, id = "Species"), aes(variable, value, colour = Species)) + 
  stat_summary(fun.data = mean_sdl, mult = 1)

plot of chunk unnamed-chunk-1


# 種ごとの箱ひげ図
ggplot(melt(iris, id = "Species"), aes(variable, value, colour = Species)) + 
  stat_boxplot()

plot of chunk unnamed-chunk-1