我有一个主要是分类变量的大型数据集(调查问卷结果)。我已经使用卡方检验检验了变量之间的相关性。变量之间存在难以理解的依赖关系。我使用了CHAID包中的chaid()函数来检测交互,并为每个变量分离(我希望是)这些依赖项的底层结构。通常发生的情况是,卡方检验将揭示变量的大量依赖关系(比如10-20),而chaid函数会将其简化为更容易理解的东西(比如3-5)。我想要做的是提取那些在chaid()结果中显示为相关的变量的名称。
chaid()输出采用constparty对象的形式。我的问题是如何提取与此类对象中的节点相关联的变量名。
下面是一个自包含的代码示例:
library(evtree) # for the ContraceptiveChoice dataset
library(CHAID)
library(vcd)
library(MASS)
data("ContraceptiveChoice")
longform = formula(contraceptive_method_used ~ wifes_education +
husbands_education + wifes_religion + wife_now_working +
husbands_occupation + standard_of_living_index + media_exposure)
z = chaid(longform, data = ContraceptiveChoice)
# plot(z)
z
# This is the part I want to do programatically
shortform = formula(contraceptive_method_used ~ wifes_education + husbands_occupation)
# The thing I want is a programatic way to extract 'shortform' from 'z'
# Examples of use of 'shortfom'
loglm(shortform, data = ContraceptiveChoice)发布于 2013-10-17 18:51:48
一种可能的解决方案:
nn <- nodeapply(z)
n.names= names(unlist(nn[[1]]))
ext <- unlist(sapply(n.names, function(x) grep("split.varid.", x, value=T)))
ext <- gsub("kids.split.varid.", "", ext)
ext <- gsub("split.varid.", "", ext)
dep.var <- as.character(terms(z)[1][[2]]) # get the dependent variable
plus = paste(ext, collapse=" + ")
mul = paste(ext, collapse=" * ")
shortform <- as.formula(paste (dep.var, plus, sep = " ~ "))
satform <- as.formula(paste (dep.var, mul, sep = " ~ "))
mosaic(shortform, data = ContraceptiveChoice)
#stp <- step(glm(satform, data=ContraceptiveChoice, family=binomial), direction="both")https://stackoverflow.com/questions/19398056
复制相似问题