Linguistic Data: Quantitative Analysis and Visualisation: linguistic theory
my_sample <- c(80, 90, 100, 100, 130)
mu0 <- 125
mean(my_sample)
[1] 100
\[H_0\colon \mu > \mu_0\]
t.test(my_sample, mu = mu0, alternative = 'greater')
One Sample t-test
data: my_sample
t = -2.9881, df = 4, p-value = 0.9798
alternative hypothesis: true mean is greater than 125
95 percent confidence interval:
82.16369 Inf
sample estimates:
mean of x
100
x <- seq(-2.9881, 5, 0.01)
curve(dt(t, df=4), from = -5, to=5, xname = 't')
# add shading
polygon(c(x[1], x, x[length(x)]),
c(0, dt(x, df=4), 0), col="steelblue")
With this alternative, p-value is “probability to get sample mean that is equal to sample mean we actually obtained (100) or more (i.e. ”more favoring alternative”), provided that null hypothesis holds (i.e. population mean = 125)“.
Find p-value for alternative = ‘less’
\[H_1\colon \mu < \mu_0\]
We can do it without function t.test. It should be equal
to the opposite area:
x <- seq(-5, -2.9881, 0.01)
curve(dt(t, df=4), from = -5, to=5, xname='t')
# add shading
polygon(c(x[1], x, x[length(x)]),
c(0, dt(x, df=4), 0), col="steelblue")
The first area (0.9798) is probability that \(\bar x\) is larger than 100 provided that null hypothesis holds. The second area (we are trying te find) is probability that \(\bar x\) is less than 100 provided than null hypothesis holds. As \(\bar x\) either larger or less than 100 (it is almost impossible that it’s equal to 100 exactly), then sum of probabilities should be 1. So, p-value for alternative “less” is
1 - 0.9798
Let’s check it:
t.test(my_sample, mu = mu0, alternative = 'less')
One Sample t-test
data: my_sample
t = -2.9881, df = 4, p-value = 0.02021
alternative hypothesis: true mean is less than 125
95 percent confidence interval:
-Inf 117.8363
sample estimates:
mean of x
100
Exact hit!
Find p-value for alternative = ‘two.sided’
Now we have:
\[H_1\colon \mu \ne \mu_0\] Again,
we can do it without t.test, then check.
x <- seq(-5, -2.9881, 0.01)
curve(dt(t, df=4), from = -5, to=5, xname='t')
# add shading
polygon(c(x[1], x, x[length(x)]),
c(0, dt(x, df=4), 0), col="steelblue")
# add shading symmetrical
polygon(-c(x[1], x, x[length(x)]),
c(0, dt(-x, df=4), 0), col="steelblue")
Now we can reject null hypothesis either if \(\bar x\) is too small or if \(\bar x\) is too large. Thus p-value is an area of symmetric figure shown on the picture. And it’s twice as large as p-value for alternative “less”.
0.02021 * 2
[1] 0.04042
Let’s check
t.test(my_sample, mu = mu0, alternative = 'two.sided')
One Sample t-test
data: my_sample
t = -2.9881, df = 4, p-value = 0.04042
alternative hypothesis: true mean is not equal to 125
95 percent confidence interval:
76.77059 123.22941
sample estimates:
mean of x
100
x <- c(3, 2, 5, 1, 7) # treatment group
y <- c(4, 5, 3, 5, 7, 8) # control group
t.test(x, y, alternative = 'less')
Welch Two Sample t-test
data: x and y
t = -1.3149, df = 7.4904, p-value = 0.1137
alternative hypothesis: true difference in means is less than 0
95 percent confidence interval:
-Inf 0.7398083
sample estimates:
mean of x mean of y
3.600000 5.333333
t.test(x, y, alternative = 'two.sided')
Welch Two Sample t-test
data: x and y
t = -1.3149, df = 7.4904, p-value = 0.2274
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-4.809596 1.342930
sample estimates:
mean of x mean of y
3.600000 5.333333
t.test(rep(x, 3), rep(y, 3), alternative = 'less')
Welch Two Sample t-test
data: rep(x, 3) and rep(y, 3)
t = -2.448, df = 26.335, p-value = 0.01067
alternative hypothesis: true difference in means is less than 0
95 percent confidence interval:
-Inf -0.526204
sample estimates:
mean of x mean of y
3.600000 5.333333
Let’s use data from thesis “Vowel duration and aspiration effects in Icelandic” by Stefano Coretta, University of York, 2016.
dat <- read.csv("https://bit.ly/39Fr0gD")
dat
Get values of time variable where roundness == “round”
round_times <- dat[dat$roundness == "round", "time"]
unrounded_times <- dat[dat$roundness == "unrounded", "time"]
round_times[1:20]
[1] 438.02832 440.61933 443.07138 132.67144 137.56550
[6] 143.38381 45.93431 49.32262 51.80259 309.72030
[11] 312.01393 314.33173 119.19929 122.31784 125.55692
[16] 336.11847 338.59150 341.18800 64.25130 69.46999
mean(round_times)
[1] 295.8432
sd(round_times)
[1] 196.1767
length(round_times)
[1] 300
mean(unrounded_times)
[1] 309.2343
sd(unrounded_times)
[1] 181.4159
length(unrounded_times)
[1] 506
Use t.test to test a hypothesis and give an answer: can
we say that there is a statistically significant difference between
round and unrounded times?
t.test(round_times, unrounded_times)
Welch Two Sample t-test
data: round_times and unrounded_times
t = -0.9631, df = 589.36, p-value = 0.3359
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-40.69893 13.91676
sample estimates:
mean of x mean of y
295.8432 309.2343
p-value = 0.33 > 0.05 => do not reject H0
This can be made easier:
t.test(time ~ roundness, data=dat)
Welch Two Sample t-test
data: time by roundness
t = -0.9631, df = 589.36, p-value = 0.3359
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-40.69893 13.91676
sample estimates:
mean in group round mean in group unrounded
295.8432 309.2343
Let’s repeat with different variable:
t.test(vowel.dur ~ roundness, data=dat)
Welch Two Sample t-test
data: vowel.dur by roundness
t = 4.3548, df = 554.3, p-value = 1.587e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
4.463876 11.799644
sample estimates:
mean in group round mean in group unrounded
91.79000 83.65824
Now we have very small p-value (\(\texttt{1.587e-05} = 1.587\cdot 10^{-5}=0.00001587\), that’s smaller than 0.05 — therefore, reject null hypothesis, i.e. we have statistically significant difference.
boxplot(vowel.dur ~ roundness, data=dat)