0

我有一个主要是分类变量的大型数据集(问卷调查结果)。我已经使用卡方检验测试了变量之间的依赖关系。变量之间存在难以理解的依赖关系。我使用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)
4

1 回答 1

0

一种可能的解决方案:

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")
于 2013-10-17T10:51:47.957 回答