1

調査パッケージを使用して変数をループする際に問題が発生しました。調査の重みとともにデータフレームに収集した変数のサブセットがあり、カイ2乗検定を実行したいとします。多重検定の問題を念頭に置いて、私はまだすべてのユニークな組み合わせをテストしたいと思います。これは通常、Rでは比較的簡単であり、ここに良い例があります。

残念ながら、これは調査パッケージでは難しくなります。これは、アイテムをデザインオブジェクトに含める必要があり、最も重要なこととして、データセットのインデックス作成がサポートされていないためです(少なくとも私が知る限り)。上記の例をsvychisqに適合させようとしましたが、すべての戦略が失敗しました。

誰かがここで似たようなことをしたことに気づきましたが、ほとんどの変数は修正されています。誰でも関数(おそらくこの答えに似たもの)を作成できますが、svychisq関数を使用できますか?残念ながら、オンラインで利用できるカテゴリ変数と複雑な設計がたくさんあるデータセットについては知りません。デモンストレーションの目的で、関数のヘルプファイルに示されているようにdata(api)でdclus1を使用して、最初の10個の変数をループすることができると思います。

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

どんな助けでも大歓迎です。

更新:私が実際にやろうとしているのは、変数名の指定を避け、代わりに変数の組み合わせのベクトルを与えることです。例えば

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

1 に答える 1

4
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]]
于 2012-11-15T21:30:08.983 に答える