library(Rcmdr)
## Loading required package: splines
## Loading required package: RcmdrMisc
## Loading required package: car
## Loading required package: carData
## Loading required package: sandwich
## Loading required package: effects
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## コマンダーの GUI はインタラクティブなセッションでしか起動しません.
##
## Attaching package: 'Rcmdr'
## The following object is masked from 'package:car':
##
## Confint
#irisデータ読み込み
data(iris)
#三次元散布図描画
library(rgl, pos=17)
library(nlme, pos=18)
library(mgcv, pos=18)
## This is mgcv 1.8-24. For overview type 'help("mgcv-package")'.
scatter3d(Sepal.Length~Petal.Length+Petal.Width|Species, data=iris,
surface=FALSE, residuals=TRUE, parallel=FALSE,
bg="white", axis.scales=TRUE, grid=TRUE, ellipsoid=FALSE)
#【1】クラスター分析
#Iris setosa (1) の4変量データ
iris[1, 1:4]
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
#Iris versicolor (51) の4変量データ
iris[51, 1:4]
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 51 7 3.2 4.7 1.4
#偏差ベクトル
iris[1, 1:4]-iris[51, 1:4]
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 -1.9 0.3 -3.3 -1.2
#マンハッタン距離(偏差絶対値和)
sum(abs(iris[1, 1:4]-iris[51, 1:4]))
## [1] 6.7
#平方ユークリッド距離(偏差平方和)
sum((iris[1, 1:4]-iris[51, 1:4])^2)
## [1] 16.03
#ユークリッド距離(偏差平方和平方根)
sqrt(sum((iris[1, 1:4]-iris[51, 1:4])^2))
## [1] 4.003748
#階層的クラスター分析によるデンドログラム描画
#[ユークリッド距離に基づく群平均法=UPGMA]
HClust.2 <- hclust(dist(model.matrix(~-1 +
Petal.Length+Petal.Width+Sepal.Length+Sepal.Width, iris)) ,
method= "average")
plot(HClust.2, main= "Cluster Dendrogram for Solution HClust.2",
xlab= "Observation Number in Data Set iris",
sub="Method=average; Distance=euclidian")
#【2】主成分分析
#相関係数行列に基づく主成分計算
local({.PC <- princomp(~Petal.Length+Petal.Width+Sepal.Length+Sepal.Width,
cor=TRUE, data=iris)
cat("\nComponent loadings:\n")
print(unclass(loadings(.PC)))
cat("\nComponent variances:\n")
print(.PC$sd^2)
cat("\n")
print(summary(.PC))
screeplot(.PC)
iris <<- within(iris,
{
PC4 <- .PC$scores[,4]
PC3 <- .PC$scores[,3]
PC2 <- .PC$scores[,2]
PC1 <- .PC$scores[,1]
})
})
##
## Component loadings:
## Comp.1 Comp.2 Comp.3 Comp.4
## Petal.Length 0.5804131 0.02449161 0.1421264 0.8014492
## Petal.Width 0.5648565 0.06694199 0.6342727 -0.5235971
## Sepal.Length 0.5210659 0.37741762 -0.7195664 -0.2612863
## Sepal.Width -0.2693474 0.92329566 0.2443818 0.1235096
##
## Component variances:
## Comp.1 Comp.2 Comp.3 Comp.4
## 2.91849782 0.91403047 0.14675688 0.02071484
##
## Importance of components:
## Comp.1 Comp.2 Comp.3 Comp.4
## Standard deviation 1.7083611 0.9560494 0.38308860 0.143926497
## Proportion of Variance 0.7296245 0.2285076 0.03668922 0.005178709
## Cumulative Proportion 0.7296245 0.9581321 0.99482129 1.000000000
#元データの散布図行列
scatterplotMatrix(~Petal.Length+Petal.Width+Sepal.Length+Sepal.Width |
Species, regLine=FALSE, smooth=FALSE,
diagonal=list(method="density"), by.groups=TRUE, data=iris)
#主成分スコアの散布図行列
scatterplotMatrix(~PC1+PC2+PC3+PC4 | Species,
regLine=FALSE, smooth=FALSE, diagonal=list(method="density"),
by.groups=TRUE, data=iris)
#第1主成分と第2主成分の二次元散布図
scatterplot(PC2~PC1 | Species, regLine=FALSE, smooth=FALSE,
boxplots=FALSE, by.groups=TRUE, data=iris)