W3 - Testing Means - Choose Your Test

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

W3 - Means: Choose and perform the correct test

(Recap unit 503 + Units 540-541)

Step 0: Choose your test:

How many samples (groups) do I have ?

- 1 sample (one group): Do t-test one sample (Unit 503)


- 2 samples (two groups): (Unit 540)
 If the two samples are paired: Look at CODES W3 to understand
 1st compute the differences
 2nd now you have 1 sample, so follow the codes for 1 sample t-test
o If the two samples are not paired (so they are independent): Ask yourself if there is
equal variance or not? How ?
 Based on the text/question
 By doing the rule of thumb in R (See codes W3) -> optional
 1st: Find the standard deviations of each group
var (biggest )
 2nd : Calculate rule of thumb:
var ( smallest )
 3rd : Interpret:
var (biggest )
o If <2 → Equal variance
var ( smallest )
var (biggest )
o If <2 → Not Equal variance
var ( smallest )
 If equal variance -> Do t-test two sample
(also called independent sample t-test)
 If not equal variance -> Do Welch t-test
- More than 2 samples (groups) -> Anova output from lm(Unit 541)

Step 1: Hypothesis: we assume always two sided so:

1.1 - One sample: Example research: Is the health of students significantly different than 6.5/10

- HO: µ = 6.5 (Read and get it from the question)


- H1 : µ ≠ 6.5

1.2 - Two samples:

1st Example: The average health differs between Dutch and non-Dutch students: YES equal variance.
2nd Example: The average health differs between males and females: NOT equal variance

- HO: µmale −µfemale =0 same as ( µ male =µ female ) µ dutch−µnon−dutch=0


- H1: µmale −µfemale ≠ 0 same as (µmale ≠ µfemale )∨µdutc h−µnon−dutc h ≠ 0
OR
- HO: β 1=0(where β1 represents the difference between means)
- H1: β 1 ≠ 0

1.3 - More than 2 samples: The average exam results differs between groups of nationality (1,2,3)
- HO: µdutc h=µgerman =µspanis hOR HO: β 1=β 2=β 3=0
- H1: µdutc h ≠ µgerman ≠ µspanis hOR: β 1 ≠ β 2 ≠ β 3 ≠ 0

Step 2: Compute the tests using R + Step 3: Interpret the outputs

2.1 - One sample -> t.test(… , …, …)

- T-test + P-Value + confidence interval in 1 code:


- IN R: t.test(… , … , …) including 3 arguments within brakets
o 1st: data_name$variable_name
o 2nd: mu = population mean (in that example µ(health) = 6.5)
o 3rd : conf.level = 0.95 (or whatever confidence interval you would like)
- For the example we did above the code would look like:
o t.test(data540$health,mu = 6.5, conf.level = 0.95)
o R automatically does a 95%, so the last part is optional.

Output:
One Sample t-test

data: data540$health
t = 2.7681, df = 49, p-value = 0.007936
alternative hypothesis: true mean is not equal to 6.5
95 percent confidence interval:
6.678663 7.625337
sample estimates:
mean of x
7.152

Step 3: Interpretation:

- Do you reject the null hypothesis? Answer I two ways:


- 1st: Using the P-Value: P-Value = 0.007 (0,07%) <0.05 -> Reject H0
- 2nd: Using the confidence interval: 6.5 not between 6.67 and 7.62 -> Reject H0
- So what do you conclude? The health status from students significantly different from 6.5
- Discuss the confidence interval: We are 95% confidence that the average health of students
is between 6.67 and 7.6
- What is the average health of the students ? 7.152

Extra (Optional): You can also use a linear model to obtain the same interpretations

Call:
lm(formula = health - 6.5 ~ 1, data = data540)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.6520 0.2355 2.768 0.00794 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.666 on 49 degrees of freedom

**Note that :
- The b-coefficient/estimate = 0.652 represents the difference between sample mean and
population mean: 7.152 (sample) – 6.5 (population) = 0.652
- T-value and P-Values are the same as the ones from the t-test, so you can also answer the
hypothesis using this lm method

2.2 Two samples -> t.test(… , … , …)

- Step 0: Check equality of variances: See R code to see how I use the rule of thumb method or
be able to understand it from the text.

- T-test code already includes the confidence interval


- IN R: t.test(… , … , …) including 3 arguments within brakets
o 1st: dependent variable: the one you are measuring
o 2nd: Independent variable: the one that defines the groups
o 3rd: data = Your_data_name
o 4th : var.equal = TRUE var.equal = FALSE: Depending on the equality of variances.

- For the examples above the code would look like:


o Example 1: t.test(health ~ dutch_dummy, data = data540, var.equal = TRUE)
o Example 2: t.test(health ~ gender, data = data540, var.equal = FALSE)

Output: (If you follow the codes from Teams, you might see differences as the data is random)

Ex1 : Yes equal variances -> Two sample t-test Ex2: Not Equal variances -> Welch t-test

data: health by dutch_dummy data: health by gender


t = 0.69694, df = 48, p-value = 0.4892 t = -0.89397, df = 39.718, p-value = 0.3767
alternative hypothesis: true difference in means alternative hypothesis: true difference in means
between group 0 and group 1 is not equal to 0 between group female and group male is not
95 percent confidence interval: equal to 0
-0.7283482 1.5011553 95 percent confidence interval:
sample estimates: -1.3985946 0.5409023
mean in group 0 mean in group 1 sample estimates:
7.244737 6.858333 mean in group female mean in group male
6.946154 7.375000

Step 3: Interpretation: Reject or not H0? Conclusion?

Example 1 (dutch): Yes equal variances Example 2(gender): Not Equal variances
- P-Value >0.05 we don’t reject Ho - P-Value >0.05 we don’t reject Ho
- 0 within 95% C.I so we don’t reject H0 - 0 within 95% C.I so we don’t reject H0

Conclusion (using P-Value): Based on our Conclusion (using P-Value): Based on our
sample, we don’t have enough evidence to say sample, we don’t have enough evidence to say
that the average health between dutch and that the average health between female and
non-dutch are different. non-male are different.

Conclusion (using C.I): The values of the Conclusion (using C.I): The values of the
confidence interval represents the differences confidence interval represents the differences
in the means between dutch and non-dutch in the means between female and male.

Extra (Optional): You can also use a linear model to obtain the same interpretations

Example 1 (dutch) Example 2(gender):

Output: Output:
Call: Call:
lm(formula = health ~ dutch_dummy, data = data540) lm(formula = health ~ gender_numerical, data = data540)

Residuals: Residuals:
Min 1Q Median 3Q Max Min 1Q Median 3Q Max
-3.8447 -1.1333 0.2053 1.1553 3.1417 -3.8750 -1.1462 0.2394 1.0250 2.6250

Coefficients: Coefficients:
Estimate Std. Error t value Pr(>|t|) Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.2447 0.2716 26.673 <2e-16 *** (Intercept) 6.9462 0.3272 21.228 <2e-16 ***
dutch_dummy -0.3864 0.5544 -0.697 0.489 gender_numerical 0.4288 0.4723 0.908 0.368
--- ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.674 on 48 degrees of freedom Residual standard error: 1.669 on 48 degrees of freedom
Multiple R-squared: 0.01002, Adjusted R-squared: - Multiple R-squared: 0.01689, Adjusted R-squared: -0.003596
0.01061 F-statistic: 0.8244 on 1 and 48 DF, p-value: 0.3684
F-statistic: 0.4857 on 1 and 48 DF, p-value: 0.4892

Interpretation: Interpretation:

- P-value is the same - P-value is almost the same (R does


- T-value is the same but in the opposite assume equal variance)
sign (this is because R computed the - T-value is the same but in the opposite
differences between dutch and non- sign (this is because R computed the
dutch the other way around) differences genders the other way
around)
Note that The linear equation would be
Y= 7.2447 - 0.3864X(dutch) where: Linear equation: Y = 6.9462 + 0.4288X(gender)
In our data female = 0, male is 1
X is a dummy so
When x=0 (non-dutch) For females (x=0)
 y = 7.2447 - 0.3864*0 = 7.2447  Y = 6.9462 + 0.4288*0 = 6.9462
This represents the mean health of non-dutch  Average health for females is 6.9462

When x = 1 (yes dutch) For males (x=1)


 y = 7.2447 - 0.3864*1 = 6.8583  Y = 6.9462 + 0.4288*1 = 7.375
This represents the mean health of dutch  Average health for males is 7.375

- The b-coefficient/estimate = -0.3864 - The b-coefficient/estimate = 0.4288


represents the difference between the represents the difference between the
means: 6.8583 – 7.2447 = -0.3864 means: 7.375 – 6.9462= 0.4288
2.3 more than 2 samples -> F test (via lm or via anova)

- HO: µdutc h=µgerman =µspanis hOR β 1=β 2=β 3=0


- H1: µdutc h ≠ µgerman ≠ µspanis hOR H1: β 1 ≠ β 2 ≠ β 3 ≠ 0
- In this example we call them group 1,2 and 3

Codes:

- 1st: lm(Dep_var_name ~ Ind_var_name, data = data_name)


- 2nd : Check the model using summary(lm_name)
- 3rd : Check the Anova table using anova(lm_name)
- See R codes

Output using lm() and summary(lm)

Call:
lm(formula = happy ~ group, data = data541)

Residuals:
Min 1Q Median 3Q Max
-2.0000 -0.5800 -0.2433 0.5333 2.1000

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.8667 0.8068 1.074 0.3107
groupGroup 2 3.0533 1.0205 2.992 0.0152 *
groupGroup 3 2.7333 1.0673 2.561 0.0306 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.397 on 9 degrees of freedom


Multiple R-squared: 0.5233, Adjusted R-squared: 0.4174
F-statistic: 4.941 on 2 and 9 DF, p-value: 0.03564

Interpretations:

- With the second part of the first output (F-test and associated P-Value) you can reject or not
H0 and say whether the averages between groups are different

Remember: HO: µdutc h=µgerman =µspanis h.

In our example P-Value = 0.03 < 0.05 so we can reject H0 (the means are the same) so we can say
that the means are statistically different.
- With the first part of the output you can calculate the means for each group and you can
compare their means pairwise with the reference category one.

To see if there is a significant difference between group 2 and group 1(the reference) you look at the
P-Value of group 2 = 0.0152 < 0.05 so there is a significant difference between these 2 groups. You
can do the same to see whether there is a significant difference between group 3 and group1.

What if I want to see the difference between group 2 and 3 ? So I far I can’t compare them since the
reference category is group 1, so I need to do something: Look next:

What If I want another group as reference, let’s say group 3?

- 1st: Create dummy variables for all groups:

Using the Pipe %>%:


data <- data %>%
mutate(group1 = ifelse(data$group == "Group 1", 1, 0),
group2 = ifelse(data$group == "Group 2", 1, 0),
group3 = ifelse(data$group == "Group 3", 1, 0))

Without pipe:
data123$group1 = ifelse(data$group == "Group 1", 1, 0)
data123$group2 = ifelse(data$group == "Group 2", 1, 0)
data123$group3 = ifelse(data$group == "Group 3", 1, 0)

- 2nd: Run the following code:


o *** Remember we want group 3 as reference so we don’t include it in the code.
o This way R will automatically put is as reference

Using the Pipe %>%


model2 <- data %>%
lm(happy ~ group1 + group2, .)
summary(model2)

Without the pipe


model2 <- lm(happy ~ group1 + group2, data = data)
summary(model2)

- *** Note data = data (this second data is the name of your dataset)

Output:
summary(model2)
##
## Call:
## lm(formula = happy ~ group1 + group2, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.0000 -0.5800 -0.2433 0.5333 2.1000
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.6000 0.6987 5.152 0.000601 ***
## group1 -2.7333 1.0673 -2.561 0.030636 *
## group2 0.3200 0.9374 0.341 0.740662
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.397 on 9 degrees of freedom
## Multiple R-squared: 0.5233, Adjusted R-squared: 0.4174
## F-statistic: 4.941 on 2 and 9 DF, p-value: 0.03564

Interpretation: you can see that there is NOT significant difference between group 2 and group 3(the
reference) since the P-Value is = 0.074 > 0.05

You might also like