WEBVTT 00:00:01.667 --> 00:00:04.142 Hi. In this video we are going to continue 00:00:04.142 --> 00:00:05.759 to talk about inference. 00:00:05.759 --> 00:00:09.446 But now we're going to be talking about how you can conduct hypothesis 00:00:09.446 --> 00:00:12.427 tests in R. 00:00:12.427 --> 00:00:15.660 So the general hypothesis testing procedure 00:00:15.761 --> 00:00:19.651 is we always state hypotheses about your parameter. 00:00:20.308 --> 00:00:23.288 We collect some data. 00:00:24.147 --> 00:00:27.128 We construct a test statistic. 00:00:29.249 --> 00:00:33.594 We then apply a decision rule so we can either 00:00:34.604 --> 00:00:37.585 do that through a critical value 00:00:37.736 --> 00:00:41.222 or with p-values or like a critical region, excuse me. 00:00:41.222 --> 00:00:43.849 Or with p-values. 00:00:43.849 --> 00:00:45.668 And then we will draw 00:00:45.668 --> 00:00:48.648 conclusions in context. 00:00:49.254 --> 00:00:52.689 So the first research question we're going to talk about 00:00:52.689 --> 00:00:56.983 today is we're going to continue using the idea of iris flowers. 00:00:57.438 --> 00:00:59.358 And see like we're interested in one. 00:00:59.358 --> 00:01:03.601 And try to hypothesize that we think that the average 00:01:03.955 --> 00:01:07.188 petal length for iris flowers 00:01:08.350 --> 00:01:11.330 is four centimeters. So, 00:01:12.543 --> 00:01:15.523 our null hypothesis would be 00:01:16.938 --> 00:01:19.514 that average, 00:01:19.514 --> 00:01:22.798 petal length is equal to four centimeters. 00:01:23.101 --> 00:01:27.092 And our alternative will be average 00:01:27.294 --> 00:01:31.487 petal length is not equal to four centimeters. 00:01:32.245 --> 00:01:33.962 Okay. 00:01:33.962 --> 00:01:35.630 The data we are going to use 00:01:35.630 --> 00:01:38.913 is the iris petal length data. 00:01:39.115 --> 00:01:41.136 So it's from the iris dataset. 00:01:41.136 --> 00:01:43.763 And this is the petal length and variable. 00:01:43.763 --> 00:01:47.400 Just to kind of remind us, it is just 150 00:01:47.400 --> 00:01:50.381 observations of different irises. 00:01:51.644 --> 00:01:54.624 To construct our test statistic 00:01:55.129 --> 00:01:58.716 we will first need an xbar value, 00:02:00.030 --> 00:02:02.202 which we can find by taking 00:02:02.202 --> 00:02:05.182 the mean of our sample. 00:02:06.344 --> 00:02:09.375 So the mean of the iris of petal length. 00:02:11.901 --> 00:02:14.781 Which will be 3.758. 00:02:14.781 --> 00:02:19.327 We also are going to need the hypothesized value 00:02:19.327 --> 00:02:24.076 that we are wanting to hypothesize, which is four centimeters. 00:02:24.076 --> 00:02:28.673 So I'm going to just call that mu because that's the parameter of interest. 00:02:28.673 --> 00:02:30.579 We're going to say it's equal to four. 00:02:31.906 --> 00:02:33.119 We also need to know 00:02:33.119 --> 00:02:36.756 the sample standard deviation, s. 00:02:37.110 --> 00:02:40.090 And so you can get that by doing this standard deviation 00:02:40.343 --> 00:02:43.323 of the variable. 00:02:43.677 --> 00:02:46.657 That value is 1.765. 00:02:47.112 --> 00:02:49.789 And then we also need to know the number of observations. 00:02:49.789 --> 00:02:53.023 So, n. So we will reduce the length function. 00:02:53.427 --> 00:02:57.115 And then I'll count how many observations are in your data set 00:02:57.115 --> 00:03:00.146 which is 50. 00:03:00.449 --> 00:03:02.419 Now once we have all of those 00:03:02.419 --> 00:03:06.107 individual pieces we can build the test statistic. 00:03:06.359 --> 00:03:10.805 Since we are doing a hypothesis test for a mean, we will be constructing 00:03:10.805 --> 00:03:15.907 what is known as like a t, a test statistic for a t-distribution. 00:03:16.816 --> 00:03:19.797 So I'm going to call it t-test stat. 00:03:21.060 --> 00:03:22.575 And how we create 00:03:22.575 --> 00:03:26.617 that is we do xbar minus mu in the numerator 00:03:27.476 --> 00:03:31.820 divided by I'm just gonna put this in parentheses 00:03:31.820 --> 00:03:36.821 as well. S divided by the square root of n. 00:03:37.529 --> 00:03:41.014 So thankfully we have all of these pieces already xbar, mu, 00:03:41.014 --> 00:03:44.803 s, and n. S, n, and xbar all come from the data. 00:03:45.409 --> 00:03:48.845 Mu is the value we specified in our null hypothesis. 00:03:49.350 --> 00:03:52.532 And this will compute our test statistic for us, 00:03:53.997 --> 00:03:54.907 which is 00:03:54.907 --> 00:04:00.918 -1.67897. So. 00:04:05.516 --> 00:04:07.435 Here we go. 00:04:07.435 --> 00:04:09.203 So our next step 00:04:09.203 --> 00:04:12.184 is to apply a decision rule. 00:04:16.175 --> 00:04:19.155 So we have two different ways we can do that. We'll- 00:04:19.762 --> 00:04:21.530 We will use a significance level 00:04:21.530 --> 00:04:25.015 or an alpha of 0.05. 00:04:25.369 --> 00:04:28.400 So I'm just going to go ahead and set that. 00:04:31.633 --> 00:04:32.694 And then if we want to 00:04:32.694 --> 00:04:35.675 calculate a rejection region, 00:04:35.686 --> 00:04:38.706 because there's two different kinds of decision rules we can do. 00:04:38.706 --> 00:04:40.120 Rejection region. 00:04:40.120 --> 00:04:43.101 We can find which critical value 00:04:43.202 --> 00:04:47.496 will give us a tail probability of 0.0 uh- 00:04:48.456 --> 00:04:50.880 Or since we're doing a two sided hypothesis test, 00:04:50.880 --> 00:04:53.911 we'll do our alpha divided by two. 00:04:54.065 --> 00:04:55.225 I'll kind of show you. 00:04:55.225 --> 00:04:58.256 So our rejection region 00:04:58.559 --> 00:05:01.893 is we're going to try, we're going to find the critical value 00:05:01.893 --> 00:05:07.198 that, fits the t-distribution, where the probability in the tail 00:05:08.612 --> 00:05:11.037 is equal to alpha over two. 00:05:11.037 --> 00:05:14.725 Because we're doing a two sided interval hypothesis test. 00:05:15.533 --> 00:05:19.878 Our degrees of freedom is needed for the t-test, which is n minus one. 00:05:20.837 --> 00:05:27.152 And since we are, our test statistic with a negative value, 00:05:27.405 --> 00:05:30.537 meaning that it's on the left side of the, 00:05:31.295 --> 00:05:34.225 of the mean on the curve, 00:05:34.225 --> 00:05:37.710 we will go ahead and say lower.tail equals true. 00:05:38.165 --> 00:05:43.116 Because we want the lower tailed like or the smaller the tail end probability. 00:05:43.469 --> 00:05:48.319 If this is a positive number 1.67 we would then do lower.tail 00:05:48.370 --> 00:05:51.906 equals false because we want the upper tail. 00:05:53.118 --> 00:05:56.048 We want kind of the extremes. 00:05:56.048 --> 00:05:59.938 So anything from where our test statistic is and more extreme. 00:06:00.999 --> 00:06:03.121 So what this will tell us 00:06:03.121 --> 00:06:06.152 is our, oh, alpha not found. 00:06:06.152 --> 00:06:08.627 I forgot to run that line. There we go. 00:06:10.650 --> 00:06:11.406 Okay. 00:06:11.406 --> 00:06:17.367 So our rejection value is one, -1.976. 00:06:18.074 --> 00:06:21.964 So what this is telling us is that if our test statistic 00:06:21.964 --> 00:06:26.611 is equal to -1.976 or less, 00:06:27.470 --> 00:06:32.876 or if it's greater than positive 1.976, 00:06:33.381 --> 00:06:36.412 then we will reject our null hypothesis. 00:06:39.443 --> 00:06:43.282 And in this case, since our test 00:06:43.282 --> 00:06:48.284 statistic is not in the extreme, it's actually greater than this value, 00:06:48.637 --> 00:06:51.618 we will fail to reject our null hypothesis. 00:06:51.618 --> 00:06:54.598 So this is telling us that, 00:06:55.558 --> 00:06:56.973 we will fail to reject 00:06:56.973 --> 00:07:00.812 our null, meaning that we do not have enough evidence to conclude 00:07:00.963 --> 00:07:04.803 that the average petal length is not equal to four centimeters. 00:07:06.015 --> 00:07:09.754 The other way you can apply a decision rule is with a p-value. 00:07:10.915 --> 00:07:13.340 And since we are doing a 00:07:13.340 --> 00:07:15.462 two sided hypothesis test, 00:07:15.462 --> 00:07:19.402 we will, can do two times whatever probability 00:07:19.402 --> 00:07:22.434 we get because we're going to be calculating it for one tail. 00:07:22.434 --> 00:07:25.439 But since we're doing two sided we'll just need to multiply it 00:07:26.020 --> 00:07:26.941 by two. 00:07:26.941 --> 00:07:29.809 And so what we're going to put in here is we're going to put 00:07:29.809 --> 00:07:33.749 in our test statistic that we get. 00:07:35.316 --> 00:07:38.296 The degrees of freedom again 00:07:38.296 --> 00:07:40.468 and again we're going to do lower.tail 00:07:40.468 --> 00:07:44.308 equals true because our original test statistic is negative. 00:07:44.308 --> 00:07:48.097 So we want a lower tail like the extreme value. 00:07:49.460 --> 00:07:53.335 And then we're going to multiply by two again because we are doing a two sided 00:07:53.704 --> 00:07:56.735 p-va- two sided hypothesis test. 00:07:57.139 --> 00:07:59.918 And then this is the value that we compare to 00:07:59.918 --> 00:08:02.898 our alpha, which is 0.05. 00:08:02.999 --> 00:08:06.485 So if our p-value is less than the alpha 00:08:06.738 --> 00:08:10.829 less than 0.05, we would reject the null hypothesis. 00:08:11.032 --> 00:08:14.416 In this case our p-value is greater than 0.05. 00:08:14.820 --> 00:08:18.357 So we would fail to reject our null hypothesis again as well. 00:08:19.316 --> 00:08:22.348 You should get the same conclusion. 00:08:23.004 --> 00:08:25.581 With either method, you should be 00:08:25.581 --> 00:08:29.117 coming to the same reject or fail to reject. 00:08:29.117 --> 00:08:32.198 You should not be getting different conclusions. 00:08:35.081 --> 00:08:36.491 So that's how you can kind of 00:08:36.491 --> 00:08:39.321 compute a hypothesis test by hand. 00:08:39.726 --> 00:08:43.767 But as always, usually in R there is an easier way to do it. 00:08:44.424 --> 00:08:47.253 So there is a function t.test 00:08:47.253 --> 00:08:50.233 which may be familiar from when we did. 00:08:50.435 --> 00:08:52.406 Confidence intervals for means. 00:08:52.406 --> 00:08:54.123 And this is actually you can 00:08:54.123 --> 00:08:57.104 do confidence intervals plus hypothesis testing in here. 00:08:57.710 --> 00:09:01.044 So we still are going to have the same null. 00:09:01.044 --> 00:09:04.075 And I turned it off hypotheses from up here. 00:09:04.530 --> 00:09:07.763 And so what we're going to do is we're going to just say t.test, 00:09:09.177 --> 00:09:12.208 give it the data that we are doing the t-test on, 00:09:13.370 --> 00:09:16.351 which is the petal length of iris flowers. 00:09:17.058 --> 00:09:20.089 We need to specify what our 00:09:20.190 --> 00:09:22.767 null hypothesis new value is. 00:09:22.767 --> 00:09:27.414 We're saying that we are hypothesizing that the true, average 00:09:27.414 --> 00:09:29.334 petal length is four. 00:09:29.334 --> 00:09:32.315 So we will say mu is equal to four. 00:09:32.567 --> 00:09:35.548 And then we also need to specify that our, 00:09:35.548 --> 00:09:39.235 our, that our alternative hypothesis is a 00:09:39.539 --> 00:09:42.519 two sided hypothesis test. 00:09:43.378 --> 00:09:45.747 Okay. 00:09:45.747 --> 00:09:47.672 And if we go ahead and run that. 00:09:47.672 --> 00:09:51.764 And notice it shows it is a one sample t-test which is perfect. 00:09:51.814 --> 00:09:53.936 We have one sample and a t-test. 00:09:53.936 --> 00:09:57.725 It gives us a t which is our test statistic 00:09:58.129 --> 00:10:00.857 which should match what we got up here. 00:10:00.857 --> 00:10:02.878 And it does. 00:10:02.878 --> 00:10:05.202 The degrees freedom is pretty easy. 00:10:05.202 --> 00:10:08.182 150 minus one. And then here's a p-value 00:10:08.283 --> 00:10:11.112 Same exact p-value we got here by doing a by hand. 00:10:12.963 --> 00:10:13.739 And then 00:10:13.739 --> 00:10:16.720 you can kind of see they have xbar right here. 00:10:17.477 --> 00:10:20.963 And then it also gives you that 95% confidence interval. 00:10:22.226 --> 00:10:24.398 So this is an, quick and easy way 00:10:24.398 --> 00:10:27.379 that you can compute a t-test for me. 00:10:28.541 --> 00:10:31.521 You can this is kind of showing you how to do it all by hand. 00:10:31.796 --> 00:10:35.217 And then this will show you kind of how to just do it in one simple step 00:10:35.217 --> 00:10:37.988 by computing a p-value for you. 00:10:39.099 --> 00:10:41.978 If you wanted to change what your, 00:10:41.978 --> 00:10:43.544 your null hypothesis was. 00:10:43.544 --> 00:10:48.041 So say, like you were testing, is the mean equal to two instead? 00:10:48.748 --> 00:10:50.213 You could totally do that. 00:10:50.213 --> 00:10:54.557 And then you can see that this p-value is way, way smaller. 00:10:55.669 --> 00:10:59.407 Or if you wanted to change your alternative. 00:10:59.559 --> 00:11:03.651 So it's not that it's just not equal to four and it's, you know, 00:11:03.651 --> 00:11:07.389 maybe less or greater than. So 00:11:08.209 --> 00:11:09.511 you could do it like this. 00:11:09.511 --> 00:11:12.491 You can do less or 00:11:12.895 --> 00:11:15.876 greater and that'll tell you, 00:11:17.694 --> 00:11:19.260 which, that'll 00:11:19.260 --> 00:11:22.999 change the output of your hypothesis test, 00:11:22.999 --> 00:11:26.181 kind of depending on if you're doing a one sided or two sided test.