One Sample Tests of Proportion - R
Contents
One Sample Tests of Proportion - R#
Binomial Test#
Samples:
1
Response Categories:
2
Exact?: Yes, use with
N≤200
Reporting: “Out of 60 outcomes, 19 were ‘x’ and 41 were ‘y’. A two-sided exact binomial test indicated that these proportions were statistically significantly different from chance (p < .05)”
# Example data
# df is a long-format data table w/columns for subject (S) and 2-category outcome (Y)
df <- read.csv("data/0F0LBs_binomial.csv")
head(df, 20)
S | Y | |
---|---|---|
<int> | <chr> | |
1 | 1 | y |
2 | 2 | y |
3 | 3 | x |
4 | 4 | y |
5 | 5 | y |
6 | 6 | x |
7 | 7 | y |
8 | 8 | x |
9 | 9 | y |
10 | 10 | y |
11 | 11 | x |
12 | 12 | y |
13 | 13 | y |
14 | 14 | y |
15 | 15 | y |
16 | 16 | x |
17 | 17 | x |
18 | 18 | y |
19 | 19 | y |
20 | 20 | x |
df$S = factor(df$S) # Subject id is nominal (unused)
df$Y = factor(df$Y) # Y is an outcome of 2 categories
xt = xtabs( ~ Y, data=df) # make counts
binom.test(xt, p=0.5, alternative="two.sided")
Exact binomial test
data: xt
number of successes = 19, number of trials = 60, p-value = 0.006218
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.2025755 0.4495597
sample estimates:
probability of success
0.3166667
Multinomial Test#
Samples:
1
Response Categories:
≥2
Exact?: Yes, use with
N≤200
Reporting: “Out of 60 outcomes, 17 were ‘x’ and 8 were ‘y’, and 35 were ‘z’. An exact multinomial test indicated that these proportions were statistically significantly different from chance (p < .0001)”
# Example data
# df is a long-format data table w/columns for subject (S) and N-category outcome (Y)
df <- read.csv("data/0F0LBs_multinomial.csv")
head(df, 20)
S | Y | |
---|---|---|
<int> | <chr> | |
1 | 1 | z |
2 | 2 | z |
3 | 3 | z |
4 | 4 | x |
5 | 5 | z |
6 | 6 | z |
7 | 7 | z |
8 | 8 | z |
9 | 9 | z |
10 | 10 | z |
11 | 11 | z |
12 | 12 | x |
13 | 13 | z |
14 | 14 | y |
15 | 15 | z |
16 | 16 | y |
17 | 17 | x |
18 | 18 | z |
19 | 19 | z |
20 | 20 | z |
# install.packages("XNomial")
library(XNomial) # import for xmulti
df$S = factor(df$S) # Subject id is nominal (unused)
df$Y = factor(df$Y) # Y is an outcome of ≥2 categories
xt = xtabs( ~ Y, data=df) # make counts
xmulti(xt, rep(1/length(xt), length(xt)), statName="Prob")
P value (Prob) = 8.756e-05
# This can also be shortened using the RVAideMemoire library
# install.packages("RVAideMemoire")
# on Ubuntu you may have trouble installing, see: TODO
library(RVAideMemoire)
multinomial.test(df$Y)
*** Package RVAideMemoire v 0.9-81-2 ***
Exact multinomial test
data: $(df,Y)
p-value = 8.756e-05
One-Sample Pearson Chi-Squared Test#
Samples:
1
Response Categories:
≥2
Exact?: No, use with
N>200
Reporting: “Out of 60 outcomes, 17 were ‘x’, 8 were ‘y’, and 35 were ‘z’. A one-sample Pearson Chi-Squared test indicated that these proportions were statistically significantly different from chance (χ2 (2, N=60) = 18.90, p < .0001).”
# Example data
# df is a long-format data table w/columns for subject (S) and N-category outcome (Y)
df <- read.csv("data/0F0LBs_multinomial.csv")
head(df, 20)
S | Y | |
---|---|---|
<int> | <chr> | |
1 | 1 | z |
2 | 2 | z |
3 | 3 | z |
4 | 4 | x |
5 | 5 | z |
6 | 6 | z |
7 | 7 | z |
8 | 8 | z |
9 | 9 | z |
10 | 10 | z |
11 | 11 | z |
12 | 12 | x |
13 | 13 | z |
14 | 14 | y |
15 | 15 | z |
16 | 16 | y |
17 | 17 | x |
18 | 18 | z |
19 | 19 | z |
20 | 20 | z |
df$S = factor(df$S) # Subject id is nominal (unused)
df$Y = factor(df$Y) # Y is an outcome of ≥2 categories
xt = xtabs( ~ Y, data=df) # make counts
chisq.test(xt)
Chi-squared test for given probabilities
data: xt
X-squared = 18.9, df = 2, p-value = 7.869e-05