本週作業
從資料庫中的 涉入度、互動、品牌形象、信任 四個變項依據 分群技術(可以選擇集群分析-連續量尺、潛在類別分析-二分變項)進行分群的動作(要命名),然後檢驗性別x此分群 對購買意願的具交互作用的二因子變異數分析,呈現其分析結果、報表與圖示。
#分群使用 kmeans 分群
分群 > 集群分析 組內同質,組間異質
windows()
plot(Data[,34:37])
涉入度、互動、品牌形象、信任 四個變項
#分幾群?
fviz_nbclust(A,
kmeans,
nstart = 25,
method = ‘gap_stat’,
nboot = 50)+
labs(subtitle = ‘Gap statistic method’)
##建議分一群
fviz_nbclust(A,
kmeans,
method = ‘wss’)+
geom_vline(xintercept = 4,linetype =2)+
labs(subtitle = ‘Gap statistic method’)
#Elbow method 建議分四群
fviz_nbclust(A,
kmeans,
method = ‘silhouette’)+
labs(subtitle = ‘Gap statistic method’)
#Eilhouette method 建議分兩群
#Compute diana() 計算距離
res.diana <- diana(A,) #stand = TRUE 不標準化)#Plot the dendrogram
library(factoextra)windows()
fviz_dend(res.diana,cex = 0.5,
k = 2, #Cut in 2 groups
palette = ‘jco’# color palette
)
綜合以上,分為2群做接續分析。
set_clu <- 2
km.res <- kmeans(A,centers = set_clu ,iter.max = 25 ,nstart = 1)
#Print the results
#檢驗性別X此分群
#對購買意願的具交互作用的二因子變異數分析
#整理資料
Data$sex <- ifelse(Data$g1==1,’Boy’,’Girl’)
Data$cluster <- km.res$cluster
HW <-Data[,c(34:40)]
# 根據分群平均值,進行描述性命名
HW$cluster <- ifelse(Data$cluster==1,’謹慎消費者’,’忠誠消費者’)
根據每個群集的平均數值,觀察命名,
cluster 1命名為’謹慎消費者’;cluster 2命名為’忠誠消費者’
table_two <-group_by(HW,cluster,sex) %>%
summarise(
count = n(),
mean = mean(購買意願,na.rm =TRUE),
sd = sd(購買意願 , na.rm =TRUE),
var = var(購買意願 , na.rm =TRUE)
)
table_two
two <- aov(購買意願 ~ cluster*sex,data=HW)
summary(two)
Df Sum Sq Mean Sq F value Pr(>F)
#cluster:sex 1 2.13 2.13 2.718 0.1001
#交互作用沒有顯著,沒中效
平均數摘要表
model.tables(two, type=’means’,se = TRUE)
###交互作用不顯著,直接做個別的事後比較
TukeyHSD(two,which = ‘cluster’)
TukeyHSD(two,which = ‘sex’)
###假設檢驗
normal distribution
library(car)
windows()
qqPlot(two$residuals,
id = FALSE ) #remove point identification
圖形來看,符合常態性假設。
shapiro.test(two$residuals)
#W = 0.99405, p-value = 0.1393
#不拒絕虛無假設,為常態
#Homogeneity of variances
windows()
plot(two, which = 3)leveneTest(two)
#Levene’s Test for Homogeneity of Variance (center = median)
# Df F value Pr(>F)
#group 3 2.0882 0.1013
# 380
#變異數同值