R 中的 agnes() 如何处理因子列?
How does agnes() in R treat factor columns?
我是 R 的新手,我还没有在任何地方看到过这个讨论,所以我对我的结果只有 95% 的信心。
阅读 agnes()
文档,我在输入 "matrix or data frame, each row corresponds to an observation, and each column corresponds to a variable. All variables must be numeric."
中看到
我一直在处理包含来自 144 个国家/地区的调查回复的大型数据集。我创建了一个数据框,其中包含国家名称列和代表该国家(每个国家一行)的标准化数字变量(范围 0-1)平均值的几列。我将该数据框用作 agnes()
的输入,我注意到生成的树状图按字母顺序显示了国家/地区。
代码:
Calculate the average value for each numeric variable for each country.
wm <- aggregate(wd2[!names(wd2) %in% c("Country")], list(Country=wd2$Country), mean)
Create dendrogram.
w_dendc <- agnes(wm,method="complete",diss = FALSE, stand = FALSE)
pltree(w_dendw,labels = wm$Country)
这毫无意义,所以我搜索了文档并找到了上面的引述。它看起来确实像 agnes()
将国家名称转换为数值 1-144。因为这是迄今为止最大的距离度量,它压倒了其他变量并导致按字母顺序排列的结果。
所以我再次尝试省略“国家/地区”列,并简单地使用“国家/地区”作为 pltree()
中的标签源。当我这样做时,树状图有一些非常有趣的特征,这些国家似乎是根据地理邻近度和 educational/economic 成就的某种组合进行分组的。
Create dendrogram, leaving out the Country column (first column).
w_dendc <- agnes(wm[,-1],method="complete",diss = FALSE, stand = FALSE)
pltree(w_dendw,labels = wm$Country)
我只是想验证一下我对 agnes()
的解释是否正确以及像这样使用因子变量。
谢谢!
agnes()
函数使用 data.matrix()
将数据转换为数值数据。解决方案是按照您的建议从分析中排除国家/地区列。
我是 R 的新手,我还没有在任何地方看到过这个讨论,所以我对我的结果只有 95% 的信心。
阅读 agnes()
文档,我在输入 "matrix or data frame, each row corresponds to an observation, and each column corresponds to a variable. All variables must be numeric."
我一直在处理包含来自 144 个国家/地区的调查回复的大型数据集。我创建了一个数据框,其中包含国家名称列和代表该国家(每个国家一行)的标准化数字变量(范围 0-1)平均值的几列。我将该数据框用作 agnes()
的输入,我注意到生成的树状图按字母顺序显示了国家/地区。
代码:
Calculate the average value for each numeric variable for each country.
wm <- aggregate(wd2[!names(wd2) %in% c("Country")], list(Country=wd2$Country), mean)
Create dendrogram.
w_dendc <- agnes(wm,method="complete",diss = FALSE, stand = FALSE)
pltree(w_dendw,labels = wm$Country)
这毫无意义,所以我搜索了文档并找到了上面的引述。它看起来确实像 agnes()
将国家名称转换为数值 1-144。因为这是迄今为止最大的距离度量,它压倒了其他变量并导致按字母顺序排列的结果。
所以我再次尝试省略“国家/地区”列,并简单地使用“国家/地区”作为 pltree()
中的标签源。当我这样做时,树状图有一些非常有趣的特征,这些国家似乎是根据地理邻近度和 educational/economic 成就的某种组合进行分组的。
Create dendrogram, leaving out the Country column (first column).
w_dendc <- agnes(wm[,-1],method="complete",diss = FALSE, stand = FALSE)
pltree(w_dendw,labels = wm$Country)
我只是想验证一下我对 agnes()
的解释是否正确以及像这样使用因子变量。
谢谢!
agnes()
函数使用 data.matrix()
将数据转换为数值数据。解决方案是按照您的建议从分析中排除国家/地区列。