R 在调查包中循环 [英] R Looping through in survey package

查看:37
本文介绍了R 在调查包中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用调查包遍历变量时遇到问题.假设我有一个与调查权重一起收集到数据框中的变量子集,我想进行卡方检验.考虑到多次测试的问题,我仍然想测试所有独特的组合.这在 R 中通常相对简单,这里有一个很好的例子 here.

I'm having problems looping through variables using the survey package. Let's say I have a subset of variables I collect into a dataframe together with the survey weight and I want to carry out chi-square tests. Bearing in mind the problems with multiple testing, I would still like to test all unique combinations. This is normally relatively straightforward in R, and there's a good example here.

不幸的是,这在调查包中变得更加困难,因为项目需要在设计对象中,最重要的是不支持数据集索引(至少据我所知).我已经尝试将上面提到的示例改编为 svychisq,但我所有的策略都失败了.

Unfortunately this become harder in the survey package because items need to be in the design object, and most importantly dataset indexing is not supported (at least as far as I know). I've tried adapting the example mentioned above to svychisq, but all my strategies have failed.

我注意到有人在这里做了类似的事情,但大多数变量是固定的.任何人都可以创建一个函数(类似于这个 答案可能)但使用 svychisq 函数?不幸的是,我不知道在线提供具有大量分类变量和复杂设计的数据集.出于演示的目的,我认为可以在 data(api) 中使用 dclus1,如函数帮助文件中所示,并尝试遍历前 10 个变量

I've noticed that someone has done something similar here, but most of the variables are fixed. Would anyone be able to create a function (something similar to this answer maybe) but using the svychisq function? Unfortunately I don't know of datasets with lots of categorical variables and complex design available online. For the purposes of demonstration I suppose one could use dclus1 in data(api) as shown in the function help file and attempt to loop through the first 10 variables

library(survey)
data(api)
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
svychisq(~sch.wide+stype, dclus1)

任何帮助将不胜感激.

更新:我真正想做的是避免指定变量名称,而是提供变量组合向量.例如

UPDATE: What I'm really trying to do is avoiding specifying the variable names and give a vector of variables combinations instead. e.g.

MyChi2tests <- apply( combn(colnames(apiclus1[,c(2,16:17)]),2), 2, function(z) paste(z, collapse = '+')) 

推荐答案

library(survey)
data(api)
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)

# run a simple example svychisq() function
svychisq( ~sch.wide+stype , dclus1 )

# create a function that requires a character string (containing the variables)
# and the design object and runs the svychisq() 
# on the contents of that character string's columns
scsloop <- function( vars , design ){ svychisq( as.formula( paste0( "~" , vars ) ) , design ) }

# test it out
scsloop( "sch.wide+stype" , dclus1 )
scsloop( "sch.wide+comp.imp" , dclus1 )

# either create a character vector to run it multiple times
cols.to.chisq <- c( "sch.wide" , "comp.imp" , "stype" )

# or identify them based on column number, if you prefer
cols.to.chisq <- names( apiclus1 )[ c( 2 , 16 , 17 ) ]


# find every combination of that vector, taken two at a time
combos <- combn( cols.to.chisq , 2 )

# separate them by +
col.combos <- paste( combos[ 1 , ] , combos[ 2 , ] , sep = "+" )

# run it on each of those character strings (print to screen and save to list)
( x <- lapply( col.combos , scsloop , dclus1 ) )

# just for kicks, print everything to the screen
col.combos[1] ; x[[1]]
col.combos[2] ; x[[2]]
col.combos[3] ; x[[3]]

这篇关于R 在调查包中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆