の カイ二乗検定 独立性は、2 つの変数のカテゴリ間に関連があるかどうかを評価します。確率変数には基本的に 2 種類があり、数値データとカテゴリデータの 2 種類のデータが生成されます。で R プログラミング言語 カイ二乗統計は、カテゴリ変数の分布が互いに異なるかどうかを調査するために使用されます。カイ二乗検定は、2 つ (またはそれ以上) の独立したグループ間のカテゴリカル応答の集計またはカウントを比較するときにも役立ちます。
npmキャッシュクリーン
R プログラミング言語では、カイ二乗検定の実行に使用される関数は次のとおりです。 chisq.test()> 。
構文:
chisq.test(データ)
パラメーター:
データ : data は、テーブル内の変数のカウント値を含むテーブルです。
調査データを取得します。 MASS> 学生に対して実施されたアンケートのデータを表すライブラリ。
R
# load the MASS package> library>(MASS)> print>(>str>(survey))> |
>
>
出力:
'data.frame': 237 obs. of 12 variables: $ Sex : Factor w/ 2 levels 'Female','Male': 1 2 2 2 2 1 2 1 2 2 ... $ Wr.Hnd: num 18.5 19.5 18 18.8 20 18 17.7 17 20 18.5 ... $ NW.Hnd: num 18 20.5 13.3 18.9 20 17.7 17.7 17.3 19.5 18.5 ... $ W.Hnd : Factor w/ 2 levels 'Left','Right': 2 1 2 2 2 2 2 2 2 2 ... $ Fold : Factor w/ 3 levels 'L on R','Neither',..: 3 3 1 3 2 1 1 3 3 3 ... $ Pulse : int 92 104 87 NA 35 64 83 74 72 90 ... $ Clap : Factor w/ 3 levels 'Left','Neither',..: 1 1 2 2 3 3 3 3 3 3 ... $ Exer : Factor w/ 3 levels 'Freq','None',..: 3 2 2 2 3 3 1 1 3 3 ... $ Smoke : Factor w/ 4 levels 'Heavy','Never',..: 2 4 3 2 2 2 2 2 2 2 ... $ Height: num 173 178 NA 160 165 ... $ M.I : Factor w/ 2 levels 'Imperial','Metric': 2 1 NA 2 2 1 1 2 2 2 ... $ Age : num 18.2 17.6 16.9 20.3 23.7 ... NULL>
上記の結果は、データセットにカテゴリ変数とみなせる多くの因子変数があることを示しています。私たちのモデルでは、変数を考慮します。 エクサー そして 煙 .Smoke 列は生徒の喫煙習慣を記録し、Exer 列は生徒の運動レベルを記録します。 私たちの目的は、学生の喫煙習慣が運動レベルと無関係であるかどうかの仮説を有意水準 0.05 で検証することです。
R
Javaサーバーページ
# Create a data frame from the main data set.> stu_data =>data.frame>(survey$Smoke,survey$Exer)> # Create a contingency table with the needed variables.> stu_data =>table>(survey$Smoke,survey$Exer)> > print>(stu_data)> |
>
>
出力:
Freq None Some Heavy 7 1 3 Never 87 18 84 Occas 12 3 4 Regul 9 1 7>
そして最後に、 chisq.test()> 分割表 stu_data への関数。
R
# applying chisq.test() function> print>(>chisq.test>(stu_data))> |
>
>
出力:
Pearson's Chi-squared test data: stu_data X-squared = 5.4885, df = 6, p-value = 0.4828>
p 値 0.4828 は 0.05 より大きいため、喫煙習慣は生徒の運動レベルとは無関係であり、したがって 2 つの変数間には相関関係が弱いかまったくないと結論付けられます。完全な R コードを以下に示します。
要約すると、R を使用してカイ 2 乗検定を実行するのは非常に簡単であると言えます。このタスクは次のように実行できます。 chisq.test()> Rの関数。
カイ二乗検定データを視覚化する
R
# Load required library> library>(MASS)> # Print structure of the survey dataset> print>(>str>(survey))> # Create a data frame for smoking and exercise columns> stu_data <->data.frame>(survey$Smoke, survey$Exer)> stu_data <->table>(survey$Smoke, survey$Exer)> # Print the table> print>(stu_data)> # Perform the Chi-Square Test> chi_result <->chisq.test>(stu_data)> print>(chi_result)> # Visualize the data with a bar plot> barplot>(stu_data, beside =>TRUE>, col =>c>(>'lightblue'>,>'lightgreen'>),> >main =>'Smoking Habits vs Exercise Levels'>,> >xlab =>'Exercise Level'>, ylab =>'Number of Students'>)> # Add legend separately> legend>(>'center'>, legend =>rownames>(stu_data), fill =>c>(>'lightblue'>,>'lightgreen'>))> |
>
>
出力:

R のカイ二乗検定
このコードでは、MASS>ライブラリを使用して、喫煙習慣と運動レベルの関係に焦点を当て、「調査」データセットに対してカイ二乗検定を実施します。
分割表を作成し、統計検定を実行し、棒グラフを使用してデータを視覚化します。凡例は左上隅に個別に追加され、異なる喫煙習慣を明確な色で区別します。
このコードは、データセット内の喫煙行動と運動習慣との関連性を調査し、伝達することを目的としています。
Javaの拡張ループ