# Lecture_35
Author: Zheyuan Wu
Date: 2023-04-17

#### Zheyuan Wu

#### 2023-04-17

### Comparing Two Means in R

Suppose we have the following data from two independent samples:

Sample 1 20.0 12.4 23.6 19.5 33.5 11.7 13.6 18.2 11.8 21.8

Sample 2 16.8 21.7 31.5 22.1 23.8 17.1 20.0 17.1

First, read the sample into two vectors x1 and x2:

    x1 <- c(20.0, 12.4, 23.6, 19.5, 33.5, 11.7, 13.6, 18.2, 11.8, 21.8)
    x2 <- c(16.8, 21.7, 31.5, 22.1, 23.8, 17.1, 20.0, 17.1)

To test the following hypotheses

\[ H_0 : \mu_1 − \mu_2 = 2 \\ H_a : \mu_1 − \mu_2 < 2 \]

using the equal variances test use the following R command:

    t.test(x1, x2, mu = 2, alternative = "less", var.equal = T)

    ##
    ##  Two Sample t-test
    ##
    ## data:  x1 and x2
    ## t = -1.621, df = 16, p-value = 0.06227
    ## alternative hypothesis: true difference in means is less than 2
    ## 95 percent confidence interval:
    ##      -Inf 2.358326
    ## sample estimates:
    ## mean of x mean of y
    ##   18.6100   21.2625

or classical T test

Two Sample t-test

To test the following hypotheses

\[ H_0 : \mu_1 − \mu_2 = 0 \\ H_a : \mu_1 − \mu_2 = 0 \] using the unequal variances test use the following R command:

    t.test(x1, x2, mu = 0, alternative = "two.sided")

    ##
    ##  Welch Two Sample t-test
    ##
    ## data:  x1 and x2
    ## t = -0.9594, df = 15.873, p-value = 0.3517
    ## alternative hypothesis: true difference in means is not equal to 0
    ## 95 percent confidence interval:
    ##  -8.517329  3.212329
    ## sample estimates:
    ## mean of x mean of y
    ##   18.6100   21.2625

Note: Using `alternative = "two.sided"` also provides a confidence interval. To change the confidence level to 99%, add the option `conf.level = 0.99`.