纵向数据的多重对应分析

时间:2017-11-22 21:45:14

标签: r weighted-average longitudinal correspondence-analysis

我想探讨一个分类变量的两种形态随时间变化的概况,与一组给定的其他分类变量有关。我在下面粘贴了这样一个数据集的可重现的例子。

set.seed(90114)
V1<-sample(rep(c("a", "A"), 100))
V2<-sample(rep(c("a", "A", "b", "B"), 50))
V3<-sample(rep(c("F", "M", "I"), 67), 200)
V4<-sample(rep(c("C", "R"), 100))
V5<-sample(rep(c(1970, 1980, 1990, 2000, 2010), 40))
data<-data.frame(V1, V2, V3, V4, V5)

为了探索这种模态的行为,我决定使用多重对应分析(包FactoMineR)。为了解释随时间的变化,一种可能性是将数据集分成5个子样本,这些子样本代表不同的V5级别,然后在每个子集上运行MCA。其余的分析包括比较不同双层的模态位置。但是,如果原始数据集太小,这种做法并非没有问题。在这种情况下,尺寸可能会翻转或更糟,活动变量的位置可能会从一个图变为另一个图。

为了避免这个问题,一种解决方案可能是稳定所有子集中活动变量的位置,然后预测补充变量的坐标,允许后者随时间移动。我在某处读到,可以通过计算找到该模态的个体坐标的加权平均值来获得模态的坐标。因此,找到1970年的模态的坐标将归结为计算该模态的1970子集中的个体坐标的加权平均值。但是,我不知道这是否是常见做法,如果是,我只是不知道如何实施这样的计算。我粘贴了剩下的代码,以便您可以看到问题。

data.mca<-MCA(data[, -5], quali.sup=1, graph=F)

# Retrieve the coordinates of the first and second dimension

DIM1<-data.mca$ind$coord[, 1]
DIM2<-data.mca$ind$coord[, 2]

# Append the coordinates to the original dataframe

data1<-data.frame(data, DIM1, DIM2)

# Split the data into 5 clusters according to V5 ("year")

data1.split<-split(data1, data1$V5)
data1.split<-lapply(data1.split, function(x) x=x[, -5]) # to remove the fifth column with the years, no longer needed
seventies<-as.data.frame(data1.split[1])
eightties<-as.data.frame(data1.split[2])
# ...

a.1970<-seventies[seventies$X1970.V1=="a",]
A.1970<-seventies[seventies$X1970.V1=="A",]

# The idea, then, is to find the coordinates of the modalities "a" and "A" by computing the weighted mean of their respective indivuduals for each subset. The arithmetic mean would yield

# a.1970.DIM1<-mean(a.1970$X1970.DIM1) # 0.0818
# a.1970.DIM2<-mean(a.1970$X1970.DIM2) # 0.1104

# and so on for the other levels of V5.

我事先感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我们可以简单地通过FactoMineR中row.w返回的值来加权坐标的平均值。考虑到MCA的扩张,重心的坐标值应除以维数特征值的平方根。

DIM1<-data.mca$ind$coord[, 1]
DIM2<-data.mca$ind$coord[, 2]
WEIGHT<-data.mca$call$row.w
data1<-data.frame(data, WEIGHT, DIM1, DIM2)

# Splitting the dataset according to values of V1

v1_a<-data1[data1$V1=="a",]
v1_A<-data1[data1$V1=="A",]

# Computing the weighted average of the coordinates of Dim1 and Dim2 for the first category of V1

V1_a_Dim1<-sum(v1_a$WEIGHT*v1_a$DIM1)/100 # -0.0248
v1_a_Dim2<-sum(v1_a$WEIGHT*v1_a$DIM2)/100 # -0.0382

# Account for the dilatation of the dimensions...

V1_a_Dim1/sqrt(data.mca$eig[1,1])
[1] -0.03923839
v1_a_Dim2/sqrt(data.mca$eig[2,1])
[1] -0.06338353

# ... which is the same as the following:

categories<-data.mca$quali.sup$coord[, 1:2]
categories
#            Dim 1       Dim 2
# V1_a -0.03923839 -0.06338353
# V1_A  0.03923839  0.06338353

这可以根据V5或任何其他分类变量应用于数据的不同分区。

相关问题