{
"cells": [
{
"cell_type": "markdown",
"id": "c47ebc4d-d172-4e70-9e16-cf6a0ba7153a",
"metadata": {},
"source": [
"# One Factor Nonparametric Tests - R"
]
},
{
"cell_type": "markdown",
"id": "4ecefac5-3aac-4309-a413-d1e62eb0fc81",
"metadata": {},
"source": [
"## Mann-Whitney _U_ test\n",
"\n",
"* **Samples:** `1`\n",
"* **Levels:** `2`\n",
"* **Between or Within Subjects:** Between\n",
"* **Reporting:** \"The median of ‘a’ was 13.26 (IQR = 5.01) and of ‘b’ was 14.98 (IQR = 6.19). This difference was statistically significant according to a Mann-Whitney U test (Z = -2.44, p < .05).\""
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c7fa707e-af25-4dab-a4f6-cddd99a5ca4e",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"A data.frame: 20 × 3\n",
"\n",
"\t | S | X | Y |
\n",
"\t | <int> | <chr> | <dbl> |
\n",
"\n",
"\n",
"\t1 | 1 | a | 14.312761 |
\n",
"\t2 | 2 | b | 16.326529 |
\n",
"\t3 | 3 | a | 8.113886 |
\n",
"\t4 | 4 | b | 14.340093 |
\n",
"\t5 | 5 | a | 13.245522 |
\n",
"\t6 | 6 | b | 17.588067 |
\n",
"\t7 | 7 | a | 8.889459 |
\n",
"\t8 | 8 | b | 10.021004 |
\n",
"\t9 | 9 | a | 13.282139 |
\n",
"\t10 | 10 | b | 10.186564 |
\n",
"\t11 | 11 | a | 14.295129 |
\n",
"\t12 | 12 | b | 9.358157 |
\n",
"\t13 | 13 | a | 8.716243 |
\n",
"\t14 | 14 | b | 10.497703 |
\n",
"\t15 | 15 | a | 15.976036 |
\n",
"\t16 | 16 | b | 15.436113 |
\n",
"\t17 | 17 | a | 13.384940 |
\n",
"\t18 | 18 | b | 15.940214 |
\n",
"\t19 | 19 | a | 15.292024 |
\n",
"\t20 | 20 | b | 11.479020 |
\n",
"\n",
"
\n"
],
"text/latex": [
"A data.frame: 20 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & S & X & Y\\\\\n",
" & & & \\\\\n",
"\\hline\n",
"\t1 & 1 & a & 14.312761\\\\\n",
"\t2 & 2 & b & 16.326529\\\\\n",
"\t3 & 3 & a & 8.113886\\\\\n",
"\t4 & 4 & b & 14.340093\\\\\n",
"\t5 & 5 & a & 13.245522\\\\\n",
"\t6 & 6 & b & 17.588067\\\\\n",
"\t7 & 7 & a & 8.889459\\\\\n",
"\t8 & 8 & b & 10.021004\\\\\n",
"\t9 & 9 & a & 13.282139\\\\\n",
"\t10 & 10 & b & 10.186564\\\\\n",
"\t11 & 11 & a & 14.295129\\\\\n",
"\t12 & 12 & b & 9.358157\\\\\n",
"\t13 & 13 & a & 8.716243\\\\\n",
"\t14 & 14 & b & 10.497703\\\\\n",
"\t15 & 15 & a & 15.976036\\\\\n",
"\t16 & 16 & b & 15.436113\\\\\n",
"\t17 & 17 & a & 13.384940\\\\\n",
"\t18 & 18 & b & 15.940214\\\\\n",
"\t19 & 19 & a & 15.292024\\\\\n",
"\t20 & 20 & b & 11.479020\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A data.frame: 20 × 3\n",
"\n",
"| | S <int> | X <chr> | Y <dbl> |\n",
"|---|---|---|---|\n",
"| 1 | 1 | a | 14.312761 |\n",
"| 2 | 2 | b | 16.326529 |\n",
"| 3 | 3 | a | 8.113886 |\n",
"| 4 | 4 | b | 14.340093 |\n",
"| 5 | 5 | a | 13.245522 |\n",
"| 6 | 6 | b | 17.588067 |\n",
"| 7 | 7 | a | 8.889459 |\n",
"| 8 | 8 | b | 10.021004 |\n",
"| 9 | 9 | a | 13.282139 |\n",
"| 10 | 10 | b | 10.186564 |\n",
"| 11 | 11 | a | 14.295129 |\n",
"| 12 | 12 | b | 9.358157 |\n",
"| 13 | 13 | a | 8.716243 |\n",
"| 14 | 14 | b | 10.497703 |\n",
"| 15 | 15 | a | 15.976036 |\n",
"| 16 | 16 | b | 15.436113 |\n",
"| 17 | 17 | a | 13.384940 |\n",
"| 18 | 18 | b | 15.940214 |\n",
"| 19 | 19 | a | 15.292024 |\n",
"| 20 | 20 | b | 11.479020 |\n",
"\n"
],
"text/plain": [
" S X Y \n",
"1 1 a 14.312761\n",
"2 2 b 16.326529\n",
"3 3 a 8.113886\n",
"4 4 b 14.340093\n",
"5 5 a 13.245522\n",
"6 6 b 17.588067\n",
"7 7 a 8.889459\n",
"8 8 b 10.021004\n",
"9 9 a 13.282139\n",
"10 10 b 10.186564\n",
"11 11 a 14.295129\n",
"12 12 b 9.358157\n",
"13 13 a 8.716243\n",
"14 14 b 10.497703\n",
"15 15 a 15.976036\n",
"16 16 b 15.436113\n",
"17 17 a 13.384940\n",
"18 18 b 15.940214\n",
"19 19 a 15.292024\n",
"20 20 b 11.479020"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Example data\n",
"# df has subjects (S), one between-Ss factor (X) w/levels (a,b), and continuous response (Y)\n",
"df <- read.csv(\"data/1F2LBs.csv\")\n",
"head(df, 20)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9c5f3020-cc55-4064-bb7d-867f7d1f37dc",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading required package: survival\n",
"\n"
]
},
{
"data": {
"text/plain": [
"\n",
"\tExact Wilcoxon-Mann-Whitney Test\n",
"\n",
"data: Y by X (a, b)\n",
"Z = -2.4394, p-value = 0.01425\n",
"alternative hypothesis: true mu is not equal to 0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"library(coin) # for wilcox_test\n",
"df$S = factor(df$S) # Subject id is nominal (unused)\n",
"df$X = factor(df$X) # X is a 2-level factor\n",
"wilcox_test(Y ~ X, data=df, distribution=\"exact\")"
]
},
{
"cell_type": "markdown",
"id": "d2e9942f-eb49-4a79-b244-cdcc21b5a691",
"metadata": {},
"source": [
"## Wilcoxon Signed-Rank Test\n",
"\n",
"* **Samples:** `1`\n",
"* **Levels:** `2`\n",
"* **Between or Within Subjects:** Within\n",
"* **Reporting:** \"The median of ‘a’ was 11.92 (IQR = 2.35) and of ‘b’ was 10.74 (IQR = 2.29). This difference was statistically significant according to a Wilcoxon signed-rank test (Z = 2.11, p < .05).\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3a2f543b-d522-482d-905e-410d6fea0cb2",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"A data.frame: 20 × 3\n",
"\n",
"\t | S | X | Y |
\n",
"\t | <int> | <chr> | <dbl> |
\n",
"\n",
"\n",
"\t1 | 1 | a | 11.479751 |
\n",
"\t2 | 1 | b | 10.860802 |
\n",
"\t3 | 2 | a | 13.763133 |
\n",
"\t4 | 2 | b | 9.609735 |
\n",
"\t5 | 3 | a | 12.058958 |
\n",
"\t6 | 3 | b | 9.229486 |
\n",
"\t7 | 4 | a | 12.173675 |
\n",
"\t8 | 4 | b | 12.112978 |
\n",
"\t9 | 5 | a | 10.017150 |
\n",
"\t10 | 5 | b | 10.366385 |
\n",
"\t11 | 6 | a | 12.709974 |
\n",
"\t12 | 6 | b | 11.641991 |
\n",
"\t13 | 7 | a | 11.254307 |
\n",
"\t14 | 7 | b | 9.541414 |
\n",
"\t15 | 8 | a | 13.498748 |
\n",
"\t16 | 8 | b | 8.973836 |
\n",
"\t17 | 9 | a | 13.015273 |
\n",
"\t18 | 9 | b | 8.015089 |
\n",
"\t19 | 10 | a | 13.549128 |
\n",
"\t20 | 10 | b | 9.171968 |
\n",
"\n",
"
\n"
],
"text/latex": [
"A data.frame: 20 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & S & X & Y\\\\\n",
" & & & \\\\\n",
"\\hline\n",
"\t1 & 1 & a & 11.479751\\\\\n",
"\t2 & 1 & b & 10.860802\\\\\n",
"\t3 & 2 & a & 13.763133\\\\\n",
"\t4 & 2 & b & 9.609735\\\\\n",
"\t5 & 3 & a & 12.058958\\\\\n",
"\t6 & 3 & b & 9.229486\\\\\n",
"\t7 & 4 & a & 12.173675\\\\\n",
"\t8 & 4 & b & 12.112978\\\\\n",
"\t9 & 5 & a & 10.017150\\\\\n",
"\t10 & 5 & b & 10.366385\\\\\n",
"\t11 & 6 & a & 12.709974\\\\\n",
"\t12 & 6 & b & 11.641991\\\\\n",
"\t13 & 7 & a & 11.254307\\\\\n",
"\t14 & 7 & b & 9.541414\\\\\n",
"\t15 & 8 & a & 13.498748\\\\\n",
"\t16 & 8 & b & 8.973836\\\\\n",
"\t17 & 9 & a & 13.015273\\\\\n",
"\t18 & 9 & b & 8.015089\\\\\n",
"\t19 & 10 & a & 13.549128\\\\\n",
"\t20 & 10 & b & 9.171968\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A data.frame: 20 × 3\n",
"\n",
"| | S <int> | X <chr> | Y <dbl> |\n",
"|---|---|---|---|\n",
"| 1 | 1 | a | 11.479751 |\n",
"| 2 | 1 | b | 10.860802 |\n",
"| 3 | 2 | a | 13.763133 |\n",
"| 4 | 2 | b | 9.609735 |\n",
"| 5 | 3 | a | 12.058958 |\n",
"| 6 | 3 | b | 9.229486 |\n",
"| 7 | 4 | a | 12.173675 |\n",
"| 8 | 4 | b | 12.112978 |\n",
"| 9 | 5 | a | 10.017150 |\n",
"| 10 | 5 | b | 10.366385 |\n",
"| 11 | 6 | a | 12.709974 |\n",
"| 12 | 6 | b | 11.641991 |\n",
"| 13 | 7 | a | 11.254307 |\n",
"| 14 | 7 | b | 9.541414 |\n",
"| 15 | 8 | a | 13.498748 |\n",
"| 16 | 8 | b | 8.973836 |\n",
"| 17 | 9 | a | 13.015273 |\n",
"| 18 | 9 | b | 8.015089 |\n",
"| 19 | 10 | a | 13.549128 |\n",
"| 20 | 10 | b | 9.171968 |\n",
"\n"
],
"text/plain": [
" S X Y \n",
"1 1 a 11.479751\n",
"2 1 b 10.860802\n",
"3 2 a 13.763133\n",
"4 2 b 9.609735\n",
"5 3 a 12.058958\n",
"6 3 b 9.229486\n",
"7 4 a 12.173675\n",
"8 4 b 12.112978\n",
"9 5 a 10.017150\n",
"10 5 b 10.366385\n",
"11 6 a 12.709974\n",
"12 6 b 11.641991\n",
"13 7 a 11.254307\n",
"14 7 b 9.541414\n",
"15 8 a 13.498748\n",
"16 8 b 8.973836\n",
"17 9 a 13.015273\n",
"18 9 b 8.015089\n",
"19 10 a 13.549128\n",
"20 10 b 9.171968"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Example data\n",
"# df has subjects (S), one within-Ss factor (X) w/levels (a,b), and continuous response (Y)\n",
"df <- read.csv(\"data/1F2LWs.csv\")\n",
"head(df, 20)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c15ab99f-a1f9-434e-b9c9-d783f10c44ee",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"\tExact Wilcoxon-Pratt Signed-Rank Test\n",
"\n",
"data: y by x (pos, neg) \n",
"\t stratified by block\n",
"Z = 2.1083, p-value = 0.03454\n",
"alternative hypothesis: true mu is not equal to 0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"library(coin)\n",
"df$S = factor(df$S) # Subject id is nominal\n",
"df$X = factor(df$X) # X is a 2-level factor\n",
"wilcoxsign_test(Y ~ X | S, data=df, distribution=\"exact\")"
]
},
{
"cell_type": "markdown",
"id": "25806650-9ed5-4ed3-9cda-a90dbe9d48a0",
"metadata": {},
"source": [
"## Kruskal-Wallis Test\n",
"\n",
"* **Samples:** `1`\n",
"* **Levels:** `≥2`\n",
"* **Between or Within Subjects:** Between\n",
"* **Reporting:** \"The median of ‘a’ was 13.96 (IQR = 5.98), of ‘b’ was 11.50 (IQR = 3.49), and of ‘c’ was 9.62 (IQR = 1.61). These differences were statistically significant according to a Kruskal-Wallis test (χ2(2, N=60) = 17.77, p < .001).\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "706e0599-ef89-4d84-9020-979dcd3197d1",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"A data.frame: 20 × 3\n",
"\n",
"\t | S | X | Y |
\n",
"\t | <int> | <chr> | <dbl> |
\n",
"\n",
"\n",
"\t1 | 1 | a | 16.906062 |
\n",
"\t2 | 2 | b | 11.583856 |
\n",
"\t3 | 3 | c | 9.821207 |
\n",
"\t4 | 4 | a | 9.983235 |
\n",
"\t5 | 5 | b | 12.259763 |
\n",
"\t6 | 6 | c | 13.224217 |
\n",
"\t7 | 7 | a | 16.168297 |
\n",
"\t8 | 8 | b | 12.346154 |
\n",
"\t9 | 9 | c | 10.083903 |
\n",
"\t10 | 10 | a | 12.522738 |
\n",
"\t11 | 11 | b | 10.437293 |
\n",
"\t12 | 12 | c | 7.849846 |
\n",
"\t13 | 13 | a | 14.581631 |
\n",
"\t14 | 14 | b | 12.862453 |
\n",
"\t15 | 15 | c | 9.060727 |
\n",
"\t16 | 16 | a | 9.713943 |
\n",
"\t17 | 17 | b | 11.409560 |
\n",
"\t18 | 18 | c | 9.709308 |
\n",
"\t19 | 19 | a | 16.478419 |
\n",
"\t20 | 20 | b | 7.699005 |
\n",
"\n",
"
\n"
],
"text/latex": [
"A data.frame: 20 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & S & X & Y\\\\\n",
" & & & \\\\\n",
"\\hline\n",
"\t1 & 1 & a & 16.906062\\\\\n",
"\t2 & 2 & b & 11.583856\\\\\n",
"\t3 & 3 & c & 9.821207\\\\\n",
"\t4 & 4 & a & 9.983235\\\\\n",
"\t5 & 5 & b & 12.259763\\\\\n",
"\t6 & 6 & c & 13.224217\\\\\n",
"\t7 & 7 & a & 16.168297\\\\\n",
"\t8 & 8 & b & 12.346154\\\\\n",
"\t9 & 9 & c & 10.083903\\\\\n",
"\t10 & 10 & a & 12.522738\\\\\n",
"\t11 & 11 & b & 10.437293\\\\\n",
"\t12 & 12 & c & 7.849846\\\\\n",
"\t13 & 13 & a & 14.581631\\\\\n",
"\t14 & 14 & b & 12.862453\\\\\n",
"\t15 & 15 & c & 9.060727\\\\\n",
"\t16 & 16 & a & 9.713943\\\\\n",
"\t17 & 17 & b & 11.409560\\\\\n",
"\t18 & 18 & c & 9.709308\\\\\n",
"\t19 & 19 & a & 16.478419\\\\\n",
"\t20 & 20 & b & 7.699005\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A data.frame: 20 × 3\n",
"\n",
"| | S <int> | X <chr> | Y <dbl> |\n",
"|---|---|---|---|\n",
"| 1 | 1 | a | 16.906062 |\n",
"| 2 | 2 | b | 11.583856 |\n",
"| 3 | 3 | c | 9.821207 |\n",
"| 4 | 4 | a | 9.983235 |\n",
"| 5 | 5 | b | 12.259763 |\n",
"| 6 | 6 | c | 13.224217 |\n",
"| 7 | 7 | a | 16.168297 |\n",
"| 8 | 8 | b | 12.346154 |\n",
"| 9 | 9 | c | 10.083903 |\n",
"| 10 | 10 | a | 12.522738 |\n",
"| 11 | 11 | b | 10.437293 |\n",
"| 12 | 12 | c | 7.849846 |\n",
"| 13 | 13 | a | 14.581631 |\n",
"| 14 | 14 | b | 12.862453 |\n",
"| 15 | 15 | c | 9.060727 |\n",
"| 16 | 16 | a | 9.713943 |\n",
"| 17 | 17 | b | 11.409560 |\n",
"| 18 | 18 | c | 9.709308 |\n",
"| 19 | 19 | a | 16.478419 |\n",
"| 20 | 20 | b | 7.699005 |\n",
"\n"
],
"text/plain": [
" S X Y \n",
"1 1 a 16.906062\n",
"2 2 b 11.583856\n",
"3 3 c 9.821207\n",
"4 4 a 9.983235\n",
"5 5 b 12.259763\n",
"6 6 c 13.224217\n",
"7 7 a 16.168297\n",
"8 8 b 12.346154\n",
"9 9 c 10.083903\n",
"10 10 a 12.522738\n",
"11 11 b 10.437293\n",
"12 12 c 7.849846\n",
"13 13 a 14.581631\n",
"14 14 b 12.862453\n",
"15 15 c 9.060727\n",
"16 16 a 9.713943\n",
"17 17 b 11.409560\n",
"18 18 c 9.709308\n",
"19 19 a 16.478419\n",
"20 20 b 7.699005"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Example data\n",
"# df has subjects (S), one between-Ss factor (X) w/levels (a,b,c), and continuous response (Y)\n",
"df <- read.csv(\"data/1F3LBs.csv\")\n",
"head(df, 20)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1dbae43d-9bef-4d67-a568-2cb35ec5ac17",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"\tAsymptotic Kruskal-Wallis Test\n",
"\n",
"data: Y by X (a, b, c)\n",
"chi-squared = 17.767, df = 2, p-value = 0.0001387\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"library(coin)\n",
"df$S = factor(df$S) # Subject id is nominal (unused)\n",
"df$X = factor(df$X) # X is a 3-level factor\n",
"kruskal_test(Y ~ X, data=df, distribution=\"asymptotic\")"
]
},
{
"cell_type": "markdown",
"id": "1206a00e-bcf4-4bf4-a32b-0faa303a61a9",
"metadata": {},
"source": [
"## Friedman Test\n",
"\n",
"* **Samples:** `1`\n",
"* **Levels:** `≥2`\n",
"* **Between or Within Subjects:** Within\n",
"* **Reporting:** \"The median of ‘a’ was 8.37 (IQR = 1.71), of ‘b’ was 8.97 (IQR = 2.21), and of ‘c’ was 9.97 (IQR = 3.56). These differences were statistically significant according to a Friedman test (χ2(2, N=60) = 7.90, p < .05).\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "07166157-2dbf-4ab4-94fc-0d778ebae062",
"metadata": {
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"A data.frame: 20 × 3\n",
"\n",
"\t | S | X | Y |
\n",
"\t | <int> | <chr> | <dbl> |
\n",
"\n",
"\n",
"\t1 | 1 | a | 8.133783 |
\n",
"\t2 | 1 | b | 8.862093 |
\n",
"\t3 | 1 | c | 8.941638 |
\n",
"\t4 | 2 | a | 8.737678 |
\n",
"\t5 | 2 | b | 9.969701 |
\n",
"\t6 | 2 | c | 12.963951 |
\n",
"\t7 | 3 | a | 7.952228 |
\n",
"\t8 | 3 | b | 7.105104 |
\n",
"\t9 | 3 | c | 9.303468 |
\n",
"\t10 | 4 | a | 11.941388 |
\n",
"\t11 | 4 | b | 8.251948 |
\n",
"\t12 | 4 | c | 9.893744 |
\n",
"\t13 | 5 | a | 8.847477 |
\n",
"\t14 | 5 | b | 7.753521 |
\n",
"\t15 | 5 | c | 12.373811 |
\n",
"\t16 | 6 | a | 8.096942 |
\n",
"\t17 | 6 | b | 10.440534 |
\n",
"\t18 | 6 | c | 8.758131 |
\n",
"\t19 | 7 | a | 9.574017 |
\n",
"\t20 | 7 | b | 9.172273 |
\n",
"\n",
"
\n"
],
"text/latex": [
"A data.frame: 20 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & S & X & Y\\\\\n",
" & & & \\\\\n",
"\\hline\n",
"\t1 & 1 & a & 8.133783\\\\\n",
"\t2 & 1 & b & 8.862093\\\\\n",
"\t3 & 1 & c & 8.941638\\\\\n",
"\t4 & 2 & a & 8.737678\\\\\n",
"\t5 & 2 & b & 9.969701\\\\\n",
"\t6 & 2 & c & 12.963951\\\\\n",
"\t7 & 3 & a & 7.952228\\\\\n",
"\t8 & 3 & b & 7.105104\\\\\n",
"\t9 & 3 & c & 9.303468\\\\\n",
"\t10 & 4 & a & 11.941388\\\\\n",
"\t11 & 4 & b & 8.251948\\\\\n",
"\t12 & 4 & c & 9.893744\\\\\n",
"\t13 & 5 & a & 8.847477\\\\\n",
"\t14 & 5 & b & 7.753521\\\\\n",
"\t15 & 5 & c & 12.373811\\\\\n",
"\t16 & 6 & a & 8.096942\\\\\n",
"\t17 & 6 & b & 10.440534\\\\\n",
"\t18 & 6 & c & 8.758131\\\\\n",
"\t19 & 7 & a & 9.574017\\\\\n",
"\t20 & 7 & b & 9.172273\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A data.frame: 20 × 3\n",
"\n",
"| | S <int> | X <chr> | Y <dbl> |\n",
"|---|---|---|---|\n",
"| 1 | 1 | a | 8.133783 |\n",
"| 2 | 1 | b | 8.862093 |\n",
"| 3 | 1 | c | 8.941638 |\n",
"| 4 | 2 | a | 8.737678 |\n",
"| 5 | 2 | b | 9.969701 |\n",
"| 6 | 2 | c | 12.963951 |\n",
"| 7 | 3 | a | 7.952228 |\n",
"| 8 | 3 | b | 7.105104 |\n",
"| 9 | 3 | c | 9.303468 |\n",
"| 10 | 4 | a | 11.941388 |\n",
"| 11 | 4 | b | 8.251948 |\n",
"| 12 | 4 | c | 9.893744 |\n",
"| 13 | 5 | a | 8.847477 |\n",
"| 14 | 5 | b | 7.753521 |\n",
"| 15 | 5 | c | 12.373811 |\n",
"| 16 | 6 | a | 8.096942 |\n",
"| 17 | 6 | b | 10.440534 |\n",
"| 18 | 6 | c | 8.758131 |\n",
"| 19 | 7 | a | 9.574017 |\n",
"| 20 | 7 | b | 9.172273 |\n",
"\n"
],
"text/plain": [
" S X Y \n",
"1 1 a 8.133783\n",
"2 1 b 8.862093\n",
"3 1 c 8.941638\n",
"4 2 a 8.737678\n",
"5 2 b 9.969701\n",
"6 2 c 12.963951\n",
"7 3 a 7.952228\n",
"8 3 b 7.105104\n",
"9 3 c 9.303468\n",
"10 4 a 11.941388\n",
"11 4 b 8.251948\n",
"12 4 c 9.893744\n",
"13 5 a 8.847477\n",
"14 5 b 7.753521\n",
"15 5 c 12.373811\n",
"16 6 a 8.096942\n",
"17 6 b 10.440534\n",
"18 6 c 8.758131\n",
"19 7 a 9.574017\n",
"20 7 b 9.172273"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Example data\n",
"# df has subjects (S), one within-Ss factor (X) w/levels (a,b,c), and continuous response (Y)\n",
"df <- read.csv(\"data/1F3LWs.csv\")\n",
"head(df, 20)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "803163d4-bb34-4d2a-8565-b71977ae830b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"\tAsymptotic Friedman Test\n",
"\n",
"data: Y by X (a, b, c) \n",
"\t stratified by S\n",
"chi-squared = 7.9, df = 2, p-value = 0.01925\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"library(coin)\n",
"df$S = factor(df$S) # Subject id is nominal\n",
"df$X = factor(df$X) # X is a 3-level factor\n",
"friedman_test(Y ~ X | S, data=df, distribution=\"asymptotic\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "4.2.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}