2017-09-10 8 views
1

EDIT:今や再生可能なコード/データです。R変数はコンテキストインループに応じて異なって評価されます

データフレーム内の複数の変数に対してカイ2乗検定を実行しようとしています。

npkデータセットを使用した:

単一の変数、N適切な結果を生成します。あなたがtable()の出力がchisq.test()が利用できる形式である見ることができるように

npk %>% 
    group_by(yield, N) %>% 
    select(yield, N) %>% 
    table() %>% 
    print() %>% 
    chisq.test() 

 N 
    yield 0 1 
    44.2 1 0 
    45.5 1 0 
    46.8 1 0 
    48.8 1 1 
    49.5 1 0 
    49.8 0 1 
    51.5 1 0 
    52 0 1 
    53.2 1 0 
    55 1 0 
    55.5 1 0 
    55.8 0 1 
    56 2 0 
    57 0 1 
    57.2 0 1 
    58.5 0 1 
    59 0 1 
    59.8 0 1 
    62 0 1 
    62.8 1 1 
    69.5 0 1 

    Pearson's Chi-squared test 

    data: . 
    X-squared = 20, df = 20, p-value = 0.4579 

私は特定の変数私のテーブルの出力を変更し、カイ二乗検定を実行することはできませんに呼びかけについてループものを使用して複数のテストを試してみてください。

ループが通ることのリストを作成します。

test_ordinal_variables <- noquote(names(npk[2:4])) 
test_ordinal_variables 

エラーコードでループを:(1:あなたが1を使用する場合は、明確にするため1、エラーが繰り返される:3)

for (i in 1:1){ 
    npk %>% 
    group_by(yield, test_ordinal_variables[i]) %>% 
    select(yield, test_ordinal_variables[i]) %>% 
    table() %>% 
    print() %>% 
    chisq.test() 
} 

何らかの理由で012について

Adding missing grouping variables: `test_ordinal_variables[i]` 
, , N = 0 

         yield 
test_ordinal_variables[i] 44.2 45.5 46.8 48.8 49.5 49.8 51.5 52 53.2 55 55.5 55.8 56 57 57.2 58.5 59 59.8 62 
         N 1 1 1 1 1 0 1 0 1 1 1 0 2 0 0 0 0 0 0 
         yield 
test_ordinal_variables[i] 62.8 69.5 
         N 1 0 

, , N = 1 

         yield 
test_ordinal_variables[i] 44.2 45.5 46.8 48.8 49.5 49.8 51.5 52 53.2 55 55.5 55.8 56 57 57.2 58.5 59 59.8 62 
         N 0 0 0 1 0 1 0 1 0 0 0 1 0 1 1 1 1 1 1 
         yield 
test_ordinal_variables[i] 62.8 69.5 
         N 1 1 

:出力は明らかにchisq.test()は解釈できないというテーブルを示しますは、ループ内にあるときに期待していたことを完全には評価していません。 「欠落しているグルーピング変数を追加しています」というエラーが表示されていますが、変数を追加するのではなく式を評価しただけではうまくいくと思います。

これは私が期待しているように独自に評価します。

> test_ordinal_variables[1] 
[1] N 

なぜ、ループ中に同じことをしないのですか?

+1

あなたは 'group_by'と' SELECT'の開閉parenethesesが欠落しています。 – Parfait

+0

ありがとう、これは私が上記の質問の現在の状態に到達するのを助けました。これはまったく異なっています。 –

+0

問題を再現できるようにデータ(列数行)を入力してください。 'dput()'やランダムなデータを示唆する[here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)を参照してください。投稿されたデータが問題を再現するのに十分であることを確認してください。 – Parfait

答えて

0

あなたはdplyrチェーン方式にダイナミック、引用された変数を渡しているのでgroup_by_()select_()が相手のバージョンを強調考えます。そしての収入がに動的に渡されていないので、symbol()に変換して処理してください。

for (i in names(npk[2:4])){  
    npk %>% 
     group_by_(as.symbol("yield"), i) %>% 
     select_(as.symbol("yield"), i) %>% 
     table() %>% 
     print() %>% 
     chisq.test() %>% 
     print()  
} 

出力

 N 
yield 0 1 
    44.2 1 0 
    45.5 1 0 
    46.8 1 0 
    48.8 1 1 
    49.5 1 0 
    49.8 0 1 
    51.5 1 0 
    52 0 1 
    53.2 1 0 
    55 1 0 
    55.5 1 0 
    55.8 0 1 
    56 2 0 
    57 0 1 
    57.2 0 1 
    58.5 0 1 
    59 0 1 
    59.8 0 1 
    62 0 1 
    62.8 1 1 
    69.5 0 1 

    Pearson's Chi-squared test 

data: . 
X-squared = 20, df = 20, p-value = 0.4579 

     P 
yield 0 1 
    44.2 0 1 
    45.5 1 0 
    46.8 1 0 
    48.8 0 2 
    49.5 0 1 
    49.8 1 0 
    51.5 1 0 
    52 0 1 
    53.2 0 1 
    55 1 0 
    55.5 1 0 
    55.8 0 1 
    56 1 1 
    57 1 0 
    57.2 1 0 
    58.5 0 1 
    59 0 1 
    59.8 1 0 
    62 1 0 
    62.8 0 2 
    69.5 1 0 

    Pearson's Chi-squared test 

data: . 
X-squared = 22, df = 20, p-value = 0.3405 

     K 
yield 0 1 
    44.2 1 0 
    45.5 0 1 
    46.8 1 0 
    48.8 0 2 
    49.5 0 1 
    49.8 0 1 
    51.5 1 0 
    52 1 0 
    53.2 0 1 
    55 0 1 
    55.5 0 1 
    55.8 0 1 
    56 2 0 
    57 0 1 
    57.2 0 1 
    58.5 0 1 
    59 1 0 
    59.8 1 0 
    62 1 0 
    62.8 2 0 
    69.5 1 0 

    Pearson's Chi-squared test 

data: . 
X-squared = 24, df = 20, p-value = 0.2424 

Warning messages: 
1: In chisq.test(.) : Chi-squared approximation may be incorrect 
2: In chisq.test(.) : Chi-squared approximation may be incorrect 
3: In chisq.test(.) : Chi-squared approximation may be incorrect 
+0

ありがとう! 私は[このAdvanced Rのウェブサイト](http://adv-r.had.co.nz/Computing-on-the-language.html)を正しく読んでいて、私は[referentially transparent](http: /adv-r.had.co.nz/Computing-on-the-language.html#nse-downsides)の 'group_by()'と 'select()'のバージョンでは、非透過的に評価されていました。 'group_by_()'と 'select_()'バージョンではこれを修正していますが、変数 'yeild'を 'as.symbol()'関数で非透過的に評価するように強制する必要があります。 また、「名前(npk [2:4])」を先頭に移動すると、読みやすくなります。 :) –

関連する問題