class: center, middle, inverse, title-slide .title[ # BANL 6100: Business Analytics ] .subtitle[ ## Two-Sample Inference ] .author[ ### Mehmet Balcilar
mbalcilar@newhaven.edu
] .institute[ ### Univeristy of New Haven ] .date[ ### 2023-09-28 (updated: 2024-04-10) ] --- exclude: true --- class: center, middle, sydney-blue <!-- Custom css --> <!-- From xaringancolor --> <div style = "position:fixed; visibility: hidden"> $$ \require{color} \definecolor{purple}{rgb}{0.337254901960784, 0.00392156862745098, 0.643137254901961} \definecolor{navy}{rgb}{0.0509803921568627, 0.23921568627451, 0.337254901960784} \definecolor{ruby}{rgb}{0.603921568627451, 0.145098039215686, 0.0823529411764706} \definecolor{alice}{rgb}{0.0627450980392157, 0.470588235294118, 0.584313725490196} \definecolor{daisy}{rgb}{0.92156862745098, 0.788235294117647, 0.266666666666667} \definecolor{coral}{rgb}{0.949019607843137, 0.427450980392157, 0.129411764705882} \definecolor{kelly}{rgb}{0.509803921568627, 0.576470588235294, 0.337254901960784} \definecolor{jet}{rgb}{0.0745098039215686, 0.0823529411764706, 0.0862745098039216} \definecolor{asher}{rgb}{0.333333333333333, 0.372549019607843, 0.380392156862745} \definecolor{slate}{rgb}{0.192156862745098, 0.309803921568627, 0.309803921568627} \definecolor{cranberry}{rgb}{0.901960784313726, 0.254901960784314, 0.450980392156863} \definecolor{hi}{rgb}{0.984313725490196, 0.12549019607843137, 0.12549019607843137} $$ </div> <script type="text/x-mathjax-config"> MathJax.Hub.Config({ TeX: { Macros: { purple: ["{\\color{purple}{#1}}", 1], navy: ["{\\color{navy}{#1}}", 1], ruby: ["{\\color{ruby}{#1}}", 1], alice: ["{\\color{alice}{#1}}", 1], daisy: ["{\\color{daisy}{#1}}", 1], coral: ["{\\color{coral}{#1}}", 1], kelly: ["{\\color{kelly}{#1}}", 1], jet: ["{\\color{jet}{#1}}", 1], asher: ["{\\color{asher}{#1}}", 1], slate: ["{\\color{slate}{#1}}", 1], cranberry: ["{\\color{cranberry}{#1}}", 1], hi: ["{\\color{hi}{#1}}", 1] }, loader: {load: ['[tex]/color']}, tex: {packages: {'[+]': ['color']}} } }); </script> <style> .purple {color: #5601A4;} .navy {color: #0D3D56;} .ruby {color: #9A2515;} .alice {color: #107895;} .daisy {color: #EBC944;} .coral {color: #F26D21;} .kelly {color: #829356;} .jet {color: #131516;} .asher {color: #555F61;} .slate {color: #314F4F;} .cranberry {color: #E64173;} .hi {color: #FB2020;} </style> # Comparing Two Means --- ## Two-Sample Framework Comparing two populations is one of the most common situations in statistics. These are called .hi.kelly[two-sample problems]. Can divide into groups, A and B - .purple[Example:] Women vs. Men; Econ Majors vs. Non-Econ Majors; Treated vs. Control We want to know if they differ along some measurable margin - .purple[Example:] salary, hours of homework per week, health This is different from the matched pairs set up because: - We have a separate sample for each group and we cannot match the observations --- ## Two-Sample Framework Consider two groups, A and B. You have the following information for each group:
Population/Group
Sample Mean
Standard Deviation
A
\( \bar{x}_A \)
\( \sigma_A \)
B
\( \bar{x}_B \)
\( \sigma_B \)
We use `\(\bar{x}_A\)` and `\(\bar{x}_B\)` to say something about the difference in population means, `\(\mu_A - \mu_B\)` - Construct a confidence interval for `\(\mu_A-\mu_B\)` - Test the hypothesis `\(H_0: \mu_A - \mu_B = 0\)` or $H_0: \mu_A = \mu_B $ --- ## Conditions for Two-Sample Inference We use `\(\bar{x}_A\)` and `\(\bar{x}_B\)` to say something about `\(\mu_A - \mu_B\)` - We have two SRS's from two distinct populations - The two samples are .hi.kelly[independent] of one another - We measure the same response variable for both samples - Both populations are normally distributed - In practice, it is enough the distributions have similar shapes and that the data have no strong outliers. --- ## Distribution of \\( \bar{x}_A\\) and \\(\bar{x}_B\\) If the sample mean, `\(\bar{x}_i \sim N\left(\mu_i, \frac{\sigma^2_i}{n}\right)\)` for `\(i \in A, B\)`, then: 1. `\(\bar{x}_A\)` and `\(\bar{x}_B\)` is normally distributed -- 2. `\(E[\bar{x}_A - \bar{x}_B] = E[\bar{x}_A] - E[\bar{x}_B]\)` -- 3. `\(\mathrm{Var}[\bar{x}_A - \bar{x}_B] = \mathrm{Var}[\bar{x}_A] + \mathrm{Var}[\bar{x}_B]\)` (by independence) -- To summarize: $$ \bar{x}_A - \bar{x}_B \sim N(\mu_A-\mu_B, \frac{\sigma^2_A}{n} + \frac{\sigma^2_B}{n}) $$ We will assume that variances are unequal accross the groups: `\(\sigma^2_A \ne \sigma^2_B\)`. --- ## Distribution of \\( \bar{x}_A\\) and \\(\bar{x}_B\\) Therefore, when both `\(\sigma^2\)` are known: $$ \frac{(\bar{x}_A-\bar{x}_B) - (\mu_A-\mu_B)}{\sqrt{\frac{\sigma^2_A}{n_A} + \frac{\sigma^2_B}{n_B}}} \sim N(0,1) $$ --- ## Distribution of \\( \bar{x}_A\\) and \\(\bar{x}_B\\) As we mentioned, we don't always know the population variance, `\(\sigma^2\)`. If we don't know these values, we can use the sample standard deviations `\(s_A\)` and `\(s_B\)` as estimators. The standard error for the difference in sample means is: $$ SE_{\bar{x}_A-\bar{x}_B}=\sqrt{\frac{s^2_A}{n_A}+\frac{s^2_B}{n_B}} $$ --- ## Distribution of \\( \bar{x}_A\\) and \\(\bar{x}_B\\) Since we estimate the sample standard deviations, we should use the `\(\daisy{t}\)`-distribution $$ \frac{\bar{x}_A-\bar{x}_B - (\mu_A-\mu_B)}{\sqrt{\frac{s^2_A}{n_A} + \frac{s^2_B}{n_B}}} $$ can be approximated by the `\(\hi{t}\)`-distribution, where the degrees of freedom is $$ \nu = \min\{n_A, n_B\}-1 $$ - Statistical software can be more exact, but the formulas get complicated --- ## Two-Sample Confidence Interval <div class="subheader bold alice"> \( \sigma^2 \) Known </div> A confidence interval for `\(\mu_A-\mu_B\)` with level of confidence `\(c\)`: `$$( \bar{x}_A-\bar{x}_B ) \pm z_{(1-c)/2} \cdot \sqrt{\frac{\sigma^2_A}{n_A}+\frac{\sigma^2_B}{n_B}}$$` --- ## Two-Sample Confidence Interval <div class="subheader bold alice"> \( \sigma^2 \) Known </div> Say we have two groups -- athletes and non-athletes and we're asked to construct a 95% confidence interval for the difference in GPA `\(\mu_A-\mu_{NA}\)`
Group
Sample Mean
Standard Deviation
Sample Size
Athletes
\( \bar{x}=2.8 \)
\( \sigma = 0.4 \)
15
Non-athletes
\( \bar{x}=2.9 \)
\( \sigma = 0.5 \)
25
$$ CI= (2.8-2.9) \pm z_{0.025}\cdot \sqrt{\frac{0.4^2}{15}+\frac{0.5^2}{25}} = [-0.38, 0.18] $$ --- ## Two-Sample Confidence Interval <div class="subheader bold alice"> \( \sigma^2 \) Unknown </div> Since we have to estimate `\(\sigma^2\)` for both samples, we need to use the `\(\hi{t}\)`-distribution to find the critical value: `$$(\bar{x}_A-\bar{x}_B) \pm t_{\nu, \frac{1-c}{2}} \cdot \sqrt{\frac{s^2_A}{n_A}+\frac{s^2_B}{n_B}}$$` --- ## Two-Sample Confidence Interval <div class="subheader bold alice"> \( \sigma^2 \) Unknown </div> We have 2 groups of students, and we're asked to construct 90% confidence interval for difference in test scores, `\(\mu_A-\mu_B\)`
Group
Sample Mean
Standard Deviation
Sample Size
Treated
\( \bar{x} = 76 \)
\( s = 9 \)
60
Control
\( \bar{x} = 73 \)
\( s = 5 \)
20
-- $$ CI=(76-73) \pm t_{0.05,19} \cdot \sqrt{\frac{9^2}{60}+\frac{5^2}{20}} =[0.21, 5.79] $$ --- ## Two-Sample Hypothesis Testing <div class="subheader bold alice"> \( \sigma^2 \) Known </div> Researchers are asking college graduates how old they were when they had their first job. Researchers are curious to see if students who attended state schools got jobs earlier in life than those who attended private colleges.
Group
Sample Mean
Standard Deviation
Sample Size
State Colleges
\( \bar{x}= 18.19 \)
\( \sigma = 3.8 \)
20
Private Colleges
\( \bar{x}= 20.98 \)
\( \sigma = 4.2 \)
20
Test the following hypothesis at the `\(\alpha=0.05\)` significance level: $$ H_0: \mu_A - \mu_B = 0 $$ $$ H_1: \mu_A - \mu_B <0 $$ --- ## Two-Sample Hypothesis Testing <div class="subheader bold alice"> \( \sigma^2 \) Known </div> Calculate p-value using: $$ P(\bar{x}_A-\bar{x}_B \leq 18.19-20.98 \ \vert \ \mu_A - \mu_B = 0) $$ $$ P\left(\frac{\bar{x}_A-\bar{x}_B-(\mu_A-\mu_B)}{\sqrt{\frac{\sigma_A^2}{n_A} + \frac{\sigma_B^2}{n_B}}} \leq \frac{18.19-20.98 - (0)}{\sqrt{\frac{3.8^2}{20}+\frac{4.2^2}{20}}} \right) $$ -- <br/> `\(p\)`-value `\(= P(z \leq -2.2) = 0.014 \implies \text{reject } H_0\)` because p-value `\(\leq \alpha=0.05\)` --- ## Two-Sample Hypothesis Testing <div class="subheader bold alice"> \( \sigma^2 \) Unknown </div> You want to test how attached individuals are to their friends, and whether that is different across people who volunteer for community service versus those who do not.
Group
Sample Mean
Standard Deviation
Sample Size
Service
\( \bar{x}= 105.32 \)
\( s = 14.68 \)
57
No Service
\( \bar{x}= 96.82 \)
\( s = 14.26 \)
17
Test the following hypothesis at `\(\alpha=0.01\)` level: $$ H_0: \mu_A - \mu_B = 0 $$ $$ H_1: \mu_A - \mu_B \neq 0 $$ --- ## Two-Sample Hypothesis Testing <div class="subheader bold alice"> \( \sigma^2 \) Unknown </div> $$ t = \frac{\bar{x}_S-\bar{x}_N - (\mu_A - \mu_B)}{\sqrt{\frac{s^2_S}{n_S} + \frac{s^2_N}{n_N}}} = \frac{105.32-96.82-(0)}{\sqrt{\frac{14.68^2}{57}+\frac{14.26^2}{17}}} $$ $$ \implies t = \frac{8.5}{3.9677} = 2.142 $$ Look at t-table, row with degrees freedom = 16. `\(t_{0.025,16} = 2.12\)` and `\(t_{0.01,16} = 2.58\)`, this means p-value is in between 0.025 and 0.01, .hi[BUT] it's a two-tailed test so we need to multiply these probabilities by 2: `\(0.02 < p\)`-value `\(< 0.05 \implies\)` Do not reject null at `\(\alpha = 0.01\)` --- ## Review In this lecture, we focus on making inferences about the relationship between the means of two different samples - Confidence intervals around the difference in means \\( \mu_A - \mu_B \pm \\) margin of error - Generally testing \\( H_0: \mu_A-\mu_B = 0 \\) - You'll be given sample means (\\( \bar{x} \\)), standard deviations (\\( \sigma \\) or \\( s \\)) and population size (\\( n \\)) of each sample. <br/> If you're given `\(\sigma\)`, use z-distribution If you're given `\(s\)`, use t-distribution (unless .hi[both] samples are large enough) --- ## Calculating Margin of Error with Two-Samples Variances known: `$$(\bar{x}_A-\bar{x}_B) \pm z_{\frac{1-c}{2}} \cdot \sqrt{\frac{\sigma^2_A}{n_A}+\frac{\sigma^2_B}{n_B}}$$` <br/> Variances unknown: `$$(\bar{x}_A-\bar{x}_B) \pm t_{ \frac{1-c}{2},\nu} \cdot \sqrt{\frac{s^2_A}{n_A}+\frac{s^2_B}{n_B}}$$` --- ## Calculating test-Statistic Variances known: $$ z = \frac{\bar{x}_A-\bar{x}_B-(\mu_A-\mu_B)}{\sqrt{\frac{\sigma_A^2}{n_A} + \frac{\sigma_B^2}{n_B}}} $$ <br/> Varainces unknown: $$ t = \frac{\bar{x}_A - \bar{x}_B - (\mu_A - \mu_B)}{\sqrt{\frac{s^2_A}{n_A}+\frac{s^2_B}{n_B}}}$$ --- ## Clicker Question You're given the following information about average length of careers in NFL versus MLB.
Group
Sample Mean
Standard Deviation
Sample Size
NFL
\( \bar{x}= 3.3 \)
\( s = 2.1 \)
20
MLB
\( \bar{x}= 5.6 \)
\( s = 3.5 \)
17
You want to construct a 90% confidence interval. Given this information, calculate the margin of error: <ol type = "a"> <li> 1.6 </li> <li> 1.645 </li> <li> 1.96 </li> <li> 1.69 </li> </ol> -- Solution ```r k = qt(p = .05, df = 16, lower.tail = FALSE)*sqrt(2.1^2/20+3.5^2/17) k ``` ``` ## [1] 1.693677 ``` --- ## Practice Question New research has developed a new drug designed to reduce blood pressure. In an experiment, 21 subjects were assigned randomly to the treatment group and receive the experimental drug. The other 23 subjects were assigned to the control group and received a placebo treatment. A summary of these data is:
Group
Sample Mean
Standard Deviation
Sample Size
Treatment
\( \bar{x} = 23.48 \)
\( s = 8.01 \)
21
Placebo
\( \bar{x} = 18.52 \)
\( s = 7.15 \)
13
We want to test whether there was any difference in means across these two groups: <ol type = "a"> <li>State the null and alternative hypothesis </li> <li>Calculate p-value or range of p-values </li> <li>Do you reject at \( \alpha=0.05 \) level? </li> <li>If you were incorrect in part c, what kind of error did you make? </li> </ol> --- ## Paired or Unpaired t-test? Sometimes, data is collected in pairs, for example when an intervention plan is conducted on a group of individuals * We want to compare the effect of the intervention before and after. * This .hi-kelly[paired] situation may occur when we have multiple observations of the same group. * We would like to match the observations to avoid differences which may occur due to variation between subjects. -- * We pair the observations and conduct the statistical tests on the difference. Let `\((x_{11}, x_{21}),\ldots,(x_{1n}, x_{2n})\)` a set of `\(n\)` paired observations. Define `\(d_j=x_{1j}-x_{2j}\)` -- `$$\mu_d=E(\bar{x}_1-\bar{x}_2)=\mu_1-\mu_2$$` -- Null hypothesis: `\(H_0:\mu_d=\Delta_0\)` Test statistic: `\(t=\frac{\bar{d}-\Delta_0}{s_d/\sqrt{n}}\)` Where `\(s_d\)` is the sample standard deviation of the differences. --- ## Example of two sample hypothesis testing - the [ipf data set](https://github.com/rfordatascience/tidytuesday/tree/master/data/2019/2019-10-08) ### The power lifting data set We're going to demonstrate with power lifting data. This data set comes from tidytuesday (2019-10-08), see the documentation here: https://github.com/rfordatascience/tidytuesday/tree/master/data/2019/2019-10-08 --- ## Example of two sample hypothesis testing - the [ipf data set](https://github.com/rfordatascience/tidytuesday/tree/master/data/2019/2019-10-08) What test would you use? (means paired / means unpaird / something else) for each of the following: .pull-left[ Test 1: * `\(H_0:\)` men lift higher weights than women * `\(H_1:\)` men and women lift the same weight Test 2: * `\(H_0:\)` Deadlift and squat weights are the same * `\(H_1:\)` Deadlift weights are higher than squat weights ] .pull-right[ Test 3: * `\(H_0:\)` The age of male athletes is normally distributed * `\(H_1:\)` The age of male athletes is not normally distributed Test 4: * `\(H_0:\)` The athletes age and gender are statistically independent * `\(H_1:\)` The athletes age and gender are not statistically independet ] --- ## Demonstrating tests in the ipf data set (test 1) * `\(H_0:\)` men and women lift the same weight * `\(H_1:\)` men lift higher weights than women Unpaired t-test (note that I switched `\(H_0\)` and `\(H_1\)`) .small[ ```r #https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-10-08/ipf_lifts.csv #https://search.r-project.org/CRAN/refmans/moderndive/html/ipf_lifts.html set.seed(0) ipf <- read_csv("data/ipf_lifts.csv", col_types = cols()) %>% filter(best3squat_kg > 0) %>% sample_n(1000) t.test(formula = best3squat_kg ~ sex, data = ipf, alternative = "less") ``` ``` ## ## Welch Two Sample t-test ## ## data: best3squat_kg by sex ## t = -32.558, df = 997.23, p-value < 2.2e-16 ## alternative hypothesis: true difference in means between group F and group M is less than 0 ## 95 percent confidence interval: ## -Inf -99.81944 ## sample estimates: ## mean in group F mean in group M ## 146.4611 251.5971 ``` ```r # alternative method to call the function #t.test(x = ipf$best3squat_kg[ipf$sex == "F"], y = ipf$best3squat_kg[ipf$sex == "M"], alternative = "less") ``` ] --- ## Demonstrating tests in the ipf data set (test 2) Test 2: * `\(H_0:\)` Deadlift and squat weights are the same * `\(H_1:\)` Deadlift weights are higher than squat weights Paired t-test, because each athelete is compared to himself ```r t.test(x = ipf$best3deadlift_kg, y = ipf$best3squat_kg, paired = TRUE, alternative = "greater") ``` ``` ## ## Paired t-test ## ## data: ipf$best3deadlift_kg and ipf$best3squat_kg ## t = 6.1662, df = 976, p-value = 5.113e-10 ## alternative hypothesis: true mean difference is greater than 0 ## 95 percent confidence interval: ## 4.141787 Inf ## sample estimates: ## mean difference ## 5.650502 ``` --- ## Demonstrating tests in the ipf data set (test 3 and 4) Test 3 was: * `\(H_0:\)` The age of male athletes is normally distributed * `\(H_1:\)` The age of male athletes is not normally distributed This is the goodness of fit test that we will discuss next. Test 4 was: * `\(H_0:\)` The athletes age and gender are statistically independent * `\(H_1:\)` The athletes age and gender are not statistically independet The fourth test is actually a goodness of fit test for uniform distribution. --- ## Goodness of Fit Test for Independence (Contingency Table) When we have two variables and we want to examine independence between them, it is like comparing the contingencies (combinations) of the two variables, against the uniform distribution. | Age | genderM | genderF | total | |--------|---------|---------|--------| | group1 | g1m | g1f | `\(n_{g1}\)` | | group2 | g2m | g2f | `\(n_{g2}\)` | | group3 | g3m | g3f | `\(n_{g3}\)` | | total | `\(n_M\)` | `\(n_F\)` | `\(n\)` | -- * The observed is the count within the cell, and the expected is the product of the marginal probabilities, i.e.: * The expected of Males in group2 under the null hypothesis is: `\(E_{g2m}=(n_{g2}/n)\times(n_M/n)\times n\)` * This is the case when the variables are independent, i.e., `\(P(X=x,Y=y)=P(X=x)P(Y=y)\)` --- The formula If we let `\(k\)` refer to the total number of categories, then the `\(X^2\)` statistic can be calculated as: $$ X^2 = \sum_{i=1}^k \frac{(O_i - E_i)^2}{E_i} $$ where `\(O_i\)` = observed frequency <br> `\(E_i\)` = expected frequenct if `\(X^2\)` is small, then the observed data `\(O_i\)` are very close to what the null hypothesis predicted `\(E_i\)`, so we're going to need a large `\(X^2\)` statistic in order to reject the null. -- The `\(X^2\)` is distributed as a chi-squre distribution with `\(\nu\)` degrees of freedom: $$ X^2 \sim \chi^2(\nu) $$ where `\(\nu=(r-1)(c-1)\)` degrees of freedom. Here: `\(r=\)` number of rows `\(c=\)` number of columns --- ## Independence between variable via contingency table (example) ```r ipf_new <- ipf %>% filter(!is.na(age)) %>% mutate(age_group = cut(age, breaks = c(0, 25, 35, 45, 55, 100))) %>% count(age_group, sex) %>% pivot_wider(id_cols = age_group, names_from = sex, values_from = n) %>% select(2:3) %>% as.matrix() ipf_compare_res <- chisq.test(ipf_new) ipf_compare_res ``` ``` ## ## Pearson's Chi-squared test ## ## data: ipf_new ## X-squared = 21.751, df = 4, p-value = 0.0002246 ``` --- ## Your turn (class exercise) - do mobile phones impact our health? In **pairs**, try to devise an experiment plan, that would test whether **mobile phones impact our health**. .small[ In your answer relate to the following points: * What do you consider as an "effect"? (i.e., what kind of health measure?) * How do you select and/or separate the groups participating in your experiment? * How do you neutralize other factors which might intervene with the experiment? (like selection of participants or other factors) * What would you use as a statistcal measure? * Paired or unpaired? * What sample size? * What would be the hypotheses 8-step procedure of the experiment? reminder: * Parameter -> Null hypothesis -> Altermative -> Significance -> Statistic -> Rejection criteria * Sample, computation -> Decision ] --- ## Variance of two samples (F-test) We talked about t-test when the variance is unknown and presented two cases (with equal variance and uneqal variance). How would we decide which of the two to use? -- We would like to use **a statistical test** to compare the variance. One method to compare two numbers is to divide them, i.e., `\(\sigma_1/\sigma_2\)`. -- The `\(F\)` distribution is the ratio of two independent chi-square random variables, divided by its number of degrees of freedom, i.e.: `$$F=\frac{W/u}{Y/v}$$` --- # Illustration of the `\(F\)` distribution .small[ `$$F=\frac{W/u}{Y/v}, \quad\mu=v/(v-2), \quad (\text{ for }v>2), \quad \sigma^2=\frac{2v^2(u+v-2)}{u(v-2)^2(v-4)}$$` ] .tiny[ ```r f_dist <- crossing(f = seq(0, 6, by = 0.1), df1 = c(1, 5, 10, 20), df2 = c(1, 5, 10, 20)) %>% mutate(f_dense = pmap_dbl(.l = list(f, df1, df2), .f = df)) %>% mutate(v = paste0("v=", df2), u = paste0("u=", df1)) %>% mutate_at(vars(v,u), fct_inorder) ggplot(f_dist, aes(x = f, y = f_dense)) + geom_line() + facet_grid(rows = vars(u), cols = vars(v)) + theme_bw() + guides(color = guide_legend("df")) ``` <img src="12-Comparing-Two-Means_files/figure-html/example F distribution-1.png" style="display: block; margin: auto;" /> ] --- ## Statistical hypothesis test for variance equality Assuming two independent populations with normal distribution, then `\(F=\frac{s_1^2/\sigma_1^2}{s_2^2/\sigma_2^2}\)` has an `\(F\)` distribution with `\(u=n_1-1, v=n_2-1\)` degrees of freedom. Null hypothesis: `\(H_0: \sigma_1^2=\sigma_2^2\)` Test statistic: `\(F = \frac{s_1^2}{s_2^2}\)` Alternative hypothesis (rejection criteria): * `\(H_1:\sigma_1^2\neq\sigma_2^2 \qquad(F>F_{1-\alpha/2, n_1-1, n_2-1} \text{ or } f<F_{\alpha/2,n_1-1, n_2-1})\)` * `\(H_1:\sigma_1^2>\sigma_2^2 \qquad(F>F_{1-\alpha,n_1-1, n_2-1})\)` * `\(H_1:\sigma_1^2<\sigma_2^2 \qquad(F<F_{\alpha,n_1-1, n_2-1})\)` --- ## Example for F statistic variance test ```r ipf %>% group_by(sex) %>% summarize_at(vars(contains("best3")), ~{(sd(., na.rm = T))^2}) ``` ``` ## # A tibble: 2 × 4 ## sex best3squat_kg best3bench_kg best3deadlift_kg ## <chr> <dbl> <dbl> <dbl> ## 1 F 1400. 723. 854. ## 2 M 4184. 2542. 2668. ``` It's pretty clear that males have a much higher variance, lets test this in an F test. ```r var.test(formula = best3squat_kg ~ sex, data = ipf, ratio = 1, alternative = "less") ``` ``` ## ## F test to compare two variances ## ## data: best3squat_kg by sex ## F = 0.3347, num df = 359, denom df = 639, p-value < 2.2e-16 ## alternative hypothesis: true ratio of variances is less than 1 ## 95 percent confidence interval: ## 0.0000000 0.3910184 ## sample estimates: ## ratio of variances ## 0.3347011 ``` --- exclude: true