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)
A data.frame: 20 × 2
SY
<int><chr>
1 1y
2 2y
3 3x
4 4y
5 5y
6 6x
7 7y
8 8x
9 9y
1010y
1111x
1212y
1313y
1414y
1515y
1616x
1717x
1818y
1919y
2020x
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)
A data.frame: 20 × 2
SY
<int><chr>
1 1z
2 2z
3 3z
4 4x
5 5z
6 6z
7 7z
8 8z
9 9z
1010z
1111z
1212x
1313z
1414y
1515z
1616y
1717x
1818z
1919z
2020z
# 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)
A data.frame: 20 × 2
SY
<int><chr>
1 1z
2 2z
3 3z
4 4x
5 5z
6 6z
7 7z
8 8z
9 9z
1010z
1111z
1212x
1313z
1414y
1515z
1616y
1717x
1818z
1919z
2020z
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