+ - 0:00:00
Notes for current slide
Notes for next slide

Parametric: Mean in Two Groups

Aly Lamuri
Indonesia Medical Education and Research Institute

1 / 26

Overview

  • Mean difference
  • One sample T-Test
  • Unpaired T-Test
  • Paired T-Test
  • Effect size
1 / 26

Concept recall

(1)xx¯sN(0,1)

(2)X¯dN(μ,σn)

1 / 26
  • Scaling and centering data following a normal distribution results in a standardized normal distribution (Z-distribution)
  • Central limit theorem: sample mean from any distribution will follow normal distribution

Concept recall

(1)xx¯sN(0,1)

(2)X¯dN(μ,σn)

With known μ and σ, we can make a direct comparison

1 / 26
  • Scaling and centering data following a normal distribution results in a standardized normal distribution (Z-distribution)
  • Central limit theorem: sample mean from any distribution will follow normal distribution

Ideal: by knowing the parameter μ and σ, we can directly compare our sample mean to its corresponding population

Concept recall

(1)xx¯sN(0,1)

(2)X¯dN(μ,σn)

With known μ and σ, we can make a direct comparison

But...

1 / 26
  • Scaling and centering data following a normal distribution results in a standardized normal distribution (Z-distribution)
  • Central limit theorem: sample mean from any distribution will follow normal distribution

Ideal: by knowing the parameter μ and σ, we can directly compare our sample mean to its corresponding population

Concept recall

(1)xx¯sN(0,1)

(2)X¯dN(μ,σn)

With known μ and σ, we can make a direct comparison

But...

How if we don't know μ?

1 / 26
  • Scaling and centering data following a normal distribution results in a standardized normal distribution (Z-distribution)
  • Central limit theorem: sample mean from any distribution will follow normal distribution

Ideal: by knowing the parameter μ and σ, we can directly compare our sample mean to its corresponding population

Solution: use its statistics x¯ as an estimate

Mean difference

  • Simply x¯μ0
  • But there bound to be errors in our samples
  • What do we do?
  • An adjustment to standard error
2 / 26
  • One-sample mean difference test: compare mean from our data to previously reported mean in the population

Mean difference

(Standard Error)SE=σnz=x¯μ0SE(One-sample Test)=x¯μ0σ/n

2 / 26
  • μ0 is the expected (hypothesized) mean, often it is something we acquire from previous publications
  • σ is the known standard deviation in population

Mean difference

(Standard Error)SE=σnz=x¯μ0SE(One-sample Test)=x¯μ0σ/n



How do we get the p-value?

2 / 26
  • μ0 is the expected (hypothesized) mean, often it is something we acquire from previous publications
  • σ is the known standard deviation in population

Mean difference

(Standard Error)SE=σnz=x¯μ0SE(One-sample Test)=x¯μ0σ/n



How do we get the p-value?

(Hint: Z-statistics follows the Z-distribution)

2 / 26
  • μ0 is the expected (hypothesized) mean, often it is something we acquire from previous publications
  • σ is the known standard deviation in population

To get the p-value, we calculate acquired z statistics as a quantile of the Z-distribution

Example, please?

In a population of third-year electrical engineering students, we know the average final score of a particular course is 70. In measuring students' comprehension, UKRIDA has established a standardized examination with a standard deviation of 10. We are interested to see whether students registered to this year course have different average, where 18 students averagely scored 75 on the final exam.

H0:x¯=μ0Ha:x¯μ0

3 / 26

Example, please?

In a population of third-year electrical engineering students, we know the average final score of a particular course is 70. In measuring students' comprehension, UKRIDA has established a standardized examination with a standard deviation of 10. We are interested to see whether students registered to this year course have different average, where 18 students averagely scored 75 on the final exam.

SE=1018=2.36z=75702.36=2.12

3 / 26

Where does it located in Z-distribution?

3 / 26

Where is it relative to the significance value?

3 / 26

What is the p-value?

  • Recall the hypothesis Ha:x¯μ0
  • We need to conduct a two-tailed test to determine the p-value
  • First we find the cummulative probability of z which satisfies:

P(Z2.12 | μ,σ):ZN(0,1)

3 / 26

What is the p-value?

  • Recall the hypothesis Ha:x¯μ0
  • We need to conduct a two-tailed test to determine the p-value
  • First we find the cummulative probability of z which satisfies:

P(Z2.12 | μ,σ):ZN(0,1)

  • Subtract P(Z=z) from 1
  • Multiply by 2
3 / 26

What is the p-value?

  • Recall the hypothesis Ha:x¯μ0
  • We need to conduct a two-tailed test to determine the p-value
  • First we find the cummulative probability of z which satisfies:

P(Z2.12 | μ,σ):ZN(0,1)

  • Subtract P(Z=z) from 1
  • Multiply by 2
2 * {1 - pnorm(2.12, 0, 1)}
## [1] 0.034
3 / 26

What do we learn?

  • Z-test requires the data to follow normal distribution
  • We do not need to know the parameter μ We can hypothesize the value
  • But we need the parameter σ to correctly compute z
4 / 26

Any hidden message?

  • Yes!
  • Z-test assumes normality
  • So we need to perform goodness of fit or normality test
4 / 26

Any hidden message?

  • Yes!
  • Z-test assumes normality
  • So we need to perform goodness of fit or normality test

What if we do not know σ?

4 / 26

Any hidden message?

  • Yes!
  • Z-test assumes normality
  • So we need to perform goodness of fit or normality test

What if we do not know σ?

We are unable to use the Z-distribution

4 / 26

Any hidden message?

  • Yes!
  • Z-test assumes normality
  • So we need to perform goodness of fit or normality test

What if we do not know σ?

We are unable to use the Z-distribution

Solution: use Student's T-distribution

4 / 26

Student's T-distribution

(Notation)Let Xtν(PDF)P(X=x)=Γ(ν+12)νπ Γ(ν2)(1+x2ν)ν=n1Let Ttν(Relationship)T=ZνV

5 / 26

Student's T-distribution

(Notation)Let Xtν(PDF)P(X=x)=Γ(ν+12)νπ Γ(ν2)(1+x2ν)ν=n1Let Ttν(Relationship)T=ZνV

  • T: T-distribution with ν degree of freedom
  • Z: A standardized normal distribution
  • V: A χ2 distribution with ν degree of freedom
5 / 26
  • Importance: Use in parametric mean difference between two groups
  • Student's T-distribution only depends on 1 parameter: ν degree of freedom
  • Degree of freedom is total subjects subtracted by 1

Student's T-distribution

5 / 26

Overview

  • Mean difference
  • One sample T-Test
  • Unpaired T-Test
  • Paired T-Test
  • Effect size
6 / 26

One sample T-Test

  • Similar to one sample Z-test
  • Applied to data with unknown σ
  • Use s as an estimate of the population parameter

t=x¯μs/n

7 / 26

Example, please?

set.seed(1)
x <- rnorm(20, mean=120, sd=20)
summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 75.7 112.3 127.2 123.8 135.2 151.9
sd(x)
## [1] 18.3
8 / 26

Example, please?

set.seed(1)
x <- rnorm(20, mean=120, sd=20)
  • We let xN(120,20)
  • But our x¯ is 123.81
  • With an s of 18.265
  • And a ν of 19
  • Does our sample differ from the population?
8 / 26

Example, please?

set.seed(1)
x <- rnorm(20, mean=120, sd=20)

Formulate the hypothesis

H0:x¯=120Ha:x¯120

8 / 26

Example, please?

set.seed(1)
x <- rnorm(20, mean=120, sd=20)

Determine the t-statistics

t=x¯μs/n

t <- {{mean(x) - 120} / {sd(x) / sqrt(20)}} %T>% print()
## [1] 0.933
8 / 26

Locate t statistics in the T-distribution

8 / 26

P-value in a one-tailed test

1 - pt(t, df=19)
## [1] 0.181
8 / 26

P-value in a two-tailed test

2 * {1 - pt(t, df=19)}
## [1] 0.363
8 / 26

How is the result in R?

t.test(x, mu=120)
##
## One Sample t-test
##
## data: x
## t = 0.9, df = 19, p-value = 0.4
## alternative hypothesis: true mean is not equal to 120
## 95 percent confidence interval:
## 115 132
## sample estimates:
## mean of x
## 124
8 / 26

How is the result in R?

t.test(x, mu=120)
##
## One Sample t-test
##
## data: x
## t = 0.9, df = 19, p-value = 0.4
## alternative hypothesis: true mean is not equal to 120
## 95 percent confidence interval:
## 115 132
## sample estimates:
## mean of x
## 124

Conclusion

  • Fail to reject H0
  • x¯=μ0=120
8 / 26

Overview

  • Mean difference
  • One sample T-Test
  • Unpaired T-Test
  • Paired T-Test
  • Effect size
9 / 26

Unpaired T-Test

  • T-Test conducted on two sample groups
  • Aims to determine significance on the mean difference
  • Types: Student's and Welch's T-Test
  • Assumes normality or a large sample size
  • Also robust in non-normal data, unless highly-skewed or have outliers
10 / 26
  • Problem with robustness: we often use simulation with specified parameters
  • But we don't know such parameters in real-world data
  • It's more acceptable when we don't violate the normality assumption

Unpaired T-Test

  • T-Test conducted on two sample groups
  • Aims to determine significance on the mean difference
  • Types: Student's and Welch's T-Test
  • Assumes normality or a large sample size
  • Also robust in non-normal data, unless highly-skewed or have outliers

H0:x¯1x¯2=dHa:x¯1x¯2dd=μ1μ2=0

10 / 26
  • Problem with robustness: we often use simulation with specified parameters
  • But we don't know such parameters in real-world data
  • It's more acceptable when we don't violate the normality assumption

In a unique case, we may find d0

Student's T-Test

  • T-Test with a pooled variance
  • Requires: Equal variance in two samples (tested with Levene's test)
  • Uses pooled variance compute statistics and the degree of freedom
11 / 26

Student's T-Test

  • T-Test with a pooled variance
  • Requires: Equal variance in two samples (tested with Levene's test)
  • Uses pooled variance compute statistics and the degree of freedom

(Statistics)t=x¯1x¯2dsp1n1+1n2(Pooled variance)sp=(n11)s12+(n21)s22ν(Degree of freedom)ν=n1+n22

11 / 26

Student's T-Test

  • T-Test with a pooled variance
  • Requires: Equal variance in two samples (tested with Levene's test)
  • Uses pooled variance compute statistics and the degree of freedom

(Statistics)t=x¯1x¯2dsp1n1+1n2(Pooled variance)sp=(n11)s12+(n21)s22ν(Degree of freedom)ν=n1+n22

Often, our data violate the equal variance assumption

Solution: Welch's T-Test

11 / 26

Welch's T-Test

(Statistics)t=x¯1x¯2ds12n1+s22n2(Degree of freedom)ν=(n11)(n21)(n21)C2+(1C2)(n11)C=s12n1s12n1s22n2

12 / 26

Welch's T-Test

(Statistics)t=x¯1x¯2ds12n1+s22n2(Degree of freedom)ν=(n11)(n21)(n21)C2+(1C2)(n11)C=s12n1s12n1s22n2

For the record:

  • Still assumes normality!
  • Not-normally distributed data still accepted if the sample size is large enough
  • How large is large enough? Re-check previous discussions on the CLT
12 / 26

Example, please?

Suppose we are collecting data on body height. Our population of interest will be students registered in UKRIDA, where we categorize sex as female and male. We acquire a normally distributed data from both sexes, where:

  • femaleN(155,15)
  • maleN(170,12)

We have a sample of 25 females and 30 males, and would like conduct a hypothesis test on mean difference.

13 / 26

Example, please?

set.seed(5)
tbl <- data.frame(
"height" = c(rnorm(30, 170, 8), rnorm(25, 155, 16)),
"sex" = c(rep("male", 30), rep("female", 25))
)
tapply(tbl$height, tbl$sex, summary)
## $female
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 123 146 154 158 173 190
##
## $male
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 152 165 168 170 177 184
tapply(tbl$height, tbl$sex, sd)
## female male
## 17.98 7.93
13 / 26

Example, please?

13 / 26

Does it follow the normal distribution?

Example, please?

tapply(tbl$height, tbl$sex, shapiro.test)
## $female
##
## Shapiro-Wilk normality test
##
## data: X[[i]]
## W = 1, p-value = 0.7
##
##
## $male
##
## Shapiro-Wilk normality test
##
## data: X[[i]]
## W = 1, p-value = 0.2
13 / 26

Yes, each group follows a normal distribution

Example, please?

car::leveneTest(tbl$height ~ tbl$sex)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 14.3 0.00039 ***
## 53
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
14 / 26

Example, please?

car::leveneTest(tbl$height ~ tbl$sex)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 14.3 0.00039 ***
## 53
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Levene's test suggests heterogenous variance (hint: significant p-value)

14 / 26

Example, please?

t.test(height ~ sex, data=tbl, var.equal=FALSE)
##
## Welch Two Sample t-test
##
## data: height by sex
## t = -3, df = 32, p-value = 0.004
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -19.87 -4.07
## sample estimates:
## mean in group female mean in group male
## 158 170

Perform Welch's T-Test since sampled variances are not equal

15 / 26

Example, please?

t.test(height ~ sex, data=tbl, var.equal=TRUE)
##
## Two Sample t-test
##
## data: height by sex
## t = -3, df = 53, p-value = 0.002
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -19.27 -4.67
## sample estimates:
## mean in group female mean in group male
## 158 170

Student's T-Test, to demonstrate type-I error inflation (hint: look at the p-value)

16 / 26
  • The p-value is much lower compared to Welch's T-Test
  • A low p-value does not mean a bad outcome per se, but we need to be cautious
  • Especially when we have violation on required assumptions
  • Always be wary of statistical error!

Example, please?

17 / 26
  • Visualizing your findings is important
  • Both figures give similar information, but conveyed in different fashions

Overview

  • Mean difference
  • One sample T-Test
  • Unpaired T-Test
  • Paired T-Test
  • Effect size
18 / 26

Paired T-Test

  • Previous analysis implicitly assumes independence
  • Some observations may violate such an assumption
  • Cases:
    • Difference between multiple measurements
    • Probability events where each instance influence another
19 / 26

Measuring mean differences

  • Two-sample T-Test does not take into account difference within subjects
  • In fact, it is a violation of its stringent assumption
  • To measure difference within same subjects, we need to reduce the complexity
20 / 26

Measuring mean differences

  • Suppose μ1 and μ2 represent measurement in t1 and t2
  • Both measures represent same subject (within comparison)
  • We can calculate the difference between both samples:

μd=μ1μ2

20 / 26

Measuring mean differences

  • Now we only need to take into account one sample differences
  • We hypothesize:

H0:μd=0Ha:μd0

20 / 26

Measuring mean differences

  • Now we only need to take into account one sample differences
  • We hypothesize:

H0:μd=0Ha:μd0

  • Sounds familiar?
  • Because it is!
  • We can apply one-sample T-Test to μd
20 / 26

Example, please?

In the current investigation, we are looking for the effect of a certain anti-hipertensive drug. First we measure the blood pressure baseline, then prescribe the drug to all subjects. Then, we re-measure the blood pressure after one month. Each subject has a unique identifier, so we can specify mean differences within paired samples. Suppose we have the following scenario in 30 sampled subjects:

  • X1N(140,12)
  • X2N(130,17)
21 / 26

Example, please?

Set our hypotheses:

H0:x¯d=0Ha:x¯d0

21 / 26

Example, please?

set.seed(1)
tbl <- data.frame(
"bp" = c(rnorm(30, 140, 12), rnorm(30, 133, 17)),
"time" = c(rep("Before", 30), rep("After", 30)) %>%
factor(levels=c("Before", "After"))
)
# Measure the mean of mean difference
md <- with(tbl, bp[time=="Before"] - bp[time=="After"])
# Calculate t-statistics
t <- {mean(md)} / {sd(md) / sqrt(30)} %T>% print()
## [1] 3.12
# Obtain p-value for a two-sided test
2 * {1 - pt(t, df=29)}
## [1] 0.076
21 / 26

Example, please?

# Comparison to built-in one-sample T-Test
t.test(md, mu=0)
##
## One Sample t-test
##
## data: md
## t = 2, df = 29, p-value = 0.08
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -0.64 12.10
## sample estimates:
## mean of x
## 5.73
21 / 26

Example, please?

# Comparison to built-in paired T-Test
t.test(bp ~ time, data=tbl, paired=TRUE)
##
## Paired t-test
##
## data: bp by time
## t = 2, df = 29, p-value = 0.08
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.64 12.10
## sample estimates:
## mean of the differences
## 5.73
21 / 26

Choosing an appropriate statistical test

  • Both T-Test and Z-Test assume normality
22 / 26

Choosing an appropriate statistical test

  • Both T-Test and Z-Test assume normality
  • One-sample test:
    • Known σ use Z-Test
    • Unknown σ use one-sample T-Test
22 / 26

Choosing an appropriate statistical test

  • Both T-Test and Z-Test assume normality
  • One-sample test:
    • Known σ use Z-Test
    • Unknown σ use one-sample T-Test
  • Two-sample test do Levene's test
    • Equal variance: Student's method (pooled variance)
    • Unequal variance: Welch's method
22 / 26

Choosing an appropriate statistical test

  • Both T-Test and Z-Test assume normality
  • One-sample test:
    • Known σ use Z-Test
    • Unknown σ use one-sample T-Test
  • Two-sample test do Levene's test
    • Equal variance: Student's method (pooled variance)
    • Unequal variance: Welch's method
  • Paired T-Test: Basically one-sample T-Test on sampled differences
22 / 26

Overview

  • Mean difference
  • One sample T-Test
  • Unpaired T-Test
  • Paired T-Test
  • Effect size
23 / 26

Effect size

  • Concept recall: statistical power
  • Related topics: sample size and significance level

(Cohen's D)d=x¯1x¯2sp(Pooled SD)sp=(s12+s22)2

24 / 26

Example, please?

set.seed(1)
tbl <- data.frame(
"bp" = c(rnorm(30, 140, 12), rnorm(30, 133, 17)),
"time" = c(rep("Before", 30), rep("After", 30)) %>%
factor(levels=c("Before", "After"))
)
# Measure the mean of mean difference
md <- with(tbl, bp[time=="Before"] - bp[time=="After"])
# Calculate t-statistics
t <- {mean(md)} / {sd(md) / sqrt(30)} %T>% print()
## [1] 3.12
# Obtain p-value for a two-sided test
2 * {1 - pt(t, df=29)}
## [1] 0.076
25 / 26

Example, please?

# Calculate pooled standard deviation
sp <- sqrt({with(tbl,
tapply(bp, time, var, simplify=FALSE)) %>% {do.call(add, .)}
} / 2) %T>% print()
## [1] 12.4
# Measure Cohen's distance
{with(tbl,
tapply(bp, time, mean, simplify=FALSE)) %>% {do.call(subtract, .)}
} / sp
## [1] 0.464
25 / 26

Example, please?

# Calculate power using the `psych` package
d <- psych::cohen.d(tbl ~ time) %T>% print()
## Call: psych::cohen.d(x = tbl ~ time)
## Cohen d statistic of difference between two means
## lower effect upper
## bp -0.99 -0.47 0.05
##
## Multivariate (Mahalanobis) distance between groups
## [1] 0.47
## r equivalent of difference between two means
## bp
## -0.23
25 / 26

Example, please?

# Power analysis using previous information
pwr::pwr.t.test(n=30, d=d$cohen.d[[2]], sig.level=0.05, type="paired")
##
## Paired t test power calculation
##
## n = 30
## d = 0.472
## sig.level = 0.05
## power = 0.704
## alternative = two.sided
##
## NOTE: n is number of *pairs*
26 / 26

Query?

26 / 26

Overview

  • Mean difference
  • One sample T-Test
  • Unpaired T-Test
  • Paired T-Test
  • Effect size
1 / 26
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow