Rで多重対応分析
背景
アンケート結果等のカテゴリカル集計データを要約するために、多重対応分析を試してみた。
手法の特徴
複数のカテゴリカルな変数を分析対象とし、変数の水準間にある関係を調べる。
多重対応分析では3変数以上を扱い、「カテゴリカル変数への主成分分析」と言われることが多い。
サンプルコード
お茶を飲む習慣に対するアンケート結果から、大まかな傾向分類ができないかを検討してみた。
多重対応分析ができるRのパッケージには様々あるが、今回はFactoMineRを使用した。
以下の項目によって、どのような傾向が観測されるかを評価した。
"Tea":対象者のID
"How":どんなお茶を
"how":ミルクやレモンを入れるか
"sugar":砂糖を入れるか
"where":どこで飲むか
"always":どのくらいの頻度か
# load packages
require(FactoMineR)
require(ggplot2)
# load data tea
data(tea)
# select these columns
newtea = tea[, c("Tea", "How", "how", "sugar", "where", "always")]
# take a peek
head(newtea)
# output
#A data.frame: 6 × 6
#Tea How how sugar where always
#<fct> <fct> <fct> <fct> <fct> <fct>
#1 black alone tea bag sugar chain store Not.always
#2 black milk tea bag No.sugar chain store Not.always
#3 Earl Grey alone tea bag No.sugar chain store Not.always
#4 Earl Grey alone tea bag sugar chain store Not.always
#5 Earl Grey alone tea bag No.sugar chain store always
#6 Earl Grey alone tea bag No.sugar chain store Not.always
# apply MCA
mca1 = MCA(newtea, graph = FALSE)
# list of results
mca1
# scree plot
fviz_screeplot(mca1, addlabels = TRUE, ylim = c(0, 45))
# data frame with variable coordinates
mca1_vars_df = data.frame(mca1$var$coord, Variable = rep(names(cats), cats))
# data frame with observation coordinates
mca1_obs_df = data.frame(mca1$ind$coord)
# plot of variable categories
ggplot(data=mca1_vars_df,
aes(x = Dim.1, y = Dim.2, label = rownames(mca1_vars_df))) +
geom_hline(yintercept = 0, colour = "gray70") +
geom_vline(xintercept = 0, colour = "gray70") +
geom_text(aes(colour=Variable)) +
ggtitle("MCA plot of variables using R package FactoMineR")
出力結果と考察例
スクリープロットを作成、5個の軸によって60%程度が説明できるような状態である。
(可能であれば、より傾向をクリアにする軸の取り方を探索してみたかった)
描画結果と考察例
例えば、下図であれば以下のような行動特性を表現していることになる。
・chain store+tea shopで飲む人はtea bag+unpackagedでお茶を飲んでいる
・一方ではtea shopだけで飲む人はunpackagedのお茶を飲んでいる。(確かに凝っている人だったらそんな感じだろうか)
・また、飲む頻度や砂糖を入れるかの違いに起因して、特段クラスタの違いは生じていない。
対象者のオブザーベーションを表現したい場合、mca1_obs_dfを引数に取る形で、同様にプロットする。
以下、geom_density2dを用いることで等高線を描くことができる。
# MCA plot of observations and categories
ggplot(data = mca1_obs_df, aes(x = Dim.1, y = Dim.2)) +
geom_hline(yintercept = 0, colour = "gray70") +
geom_vline(xintercept = 0, colour = "gray70") +
geom_point(colour = "gray50", alpha = 0.7) +
geom_density2d(colour = "gray80") +
geom_text(data = mca1_vars_df,
aes(x = Dim.1, y = Dim.2,
label = rownames(mca1_vars_df), colour = Variable)) +
ggtitle("MCA plot of variables using R package FactoMineR") +
scale_colour_discrete(name = "Variable")
参考リンク
Qiita内の関連記事
多変量解析メモ: 多重対応分析(Multiple Correspondence Analysis, MCA)
Author And Source
この問題について(Rで多重対応分析), 我々は、より多くの情報をここで見つけました https://qiita.com/s_nkjm/items/5fab7401a1d026d975cd著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .