{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w51.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/77) 04:09*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Earlier in my blog post, we have learn about hypothesis testing. Based on the difference of 30% in female vs male being promoted, we want to know if the difference makes two variable independent vs dependent. The Null Hypothesis said that is due to chance. The alternative said, there's dependant variable and some relationship. Using simulation earlier we know that achieving 0.3 is very rare using simulation. If there's no strong evidence, we stick to the Null Hypothesis, but if we do we reject the Null Hypothesis in favor of the alternative. We can do this by using CLT, like in the Kloud score example.\n",
"\n",
"In a way, Hypothesis Testing is similar to what we do in court trial. There's skeptical that judge whether the one in court is guilty or not guilty. The judge will pressume that the man is innocent, until strong evidence prove that the judge reject the innocent and set him guilty. The court is not about the evidence, it's about whether it reject, or fail to reject the innocent.\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w52.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 01:08*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Null Hypothesis is skeptical claims that there's no difference between two variable, sometime denote by equal value. In above cases, \n",
"\n",
"P(female) = P(male)\n",
" \n",
"Alternative Hypothesis claim theres difference between two variables, and represented by range of values (>,<,$\\neq$)\n",
"\n",
"P(female) $\\neq$ P(male)\n",
"\n",
"The skeptic will keep sided with Null Hypothesis until there's strong evidence to reject it.\n",
"\n",
"\n",
"So HT is all about population parameter, because it's often not defined. There's no point in testing sample statistics. H0 is about the skeptical, that is there's no difference, and set null value to be the value of H0. The HA on the other hand is state that there is different, $\\neq$ two sided, or less/greater than, one sided."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w53.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 03:11*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The hypothesis testing is always the population parameters $\\mu$ and not sample statistics $\\bar{x}$. It will always be that way. This synchronize with CI that always concern about the population parameters. Hypothesis testing is not about sample, because we already know the sample, and we want to guess the population parameters. In this case, when we know 3 is in the range of interval, we can't reject the Null Hypothesis."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w54.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 05:19*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Pay attention to what p-value described above**. The p-value is a conditional probability that given null hypothesis is true, what is the probability of at least as extreme as observed outcome. \n",
"\n",
"Suppose we are asked the probability of average more than 3.2. p-value is the probability of observed point estimate at least as extreme or more given the Null Hypothesis. Under the assumption that null hypothesis is true, we can use mu as our basis, and compute the Z-score. Attaining z-score is a matter of calculating the distance from mu divided by standard deviation.Z-score is often called **test statistic**, where we want to test our statistic parameter 3.2. Using z-table we get **p-value** of 0.209. In R"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"[0.208107]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%R pnorm(3.2,mean=3,sd=0.246,lower.tail=F)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w55.jpg)\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 06:03*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"the p-value is used to calculate the extreme condition where at the very least sided with alternative under the condition of null hypothesis is true. If the p-value is low, lower than threshold 5%(commonly used), $\\alpha$, we can reject our null hypothesis. But even under null hypothesis is true the p-value is higher then $\\alpha$, then we stick to Null Hypothesis."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w56.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 06:59*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So calculating all those, we have 20.9%, which is larger than 5%. We know that under the condition that our Null Hypothesis(average = 3) true, the p-value makes we can't reject Null Hypothesis in favor of the alternative (average >3.2).So we stick to the Null Hypothesis."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w57.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 07:44*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember the p-value is always take assumption that Null Hypothesis is true. Under the assumption of that, there's two condition. 21% chance average of 3.2 or higher, or average of 3.2 or higher is due to chance."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w58.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 08:14*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When talking about sampling distribution, 3.2 means that average of our sample is due to chance or sampling variability. Based on data that we have, it doesn't provide convincing evidence that the average is higher than 3 relationships."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w59.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 08:54*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The two-sided will be the same as one-sided, except that it takes range, a divergence instead of one. So we have to have substracting in two sided if we want to know the range of value in between, or takes sum over two cut tail, if we search for less than or greater than."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w60.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 10:13*"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"[0.416213]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%R pnorm(3.2,mean=3,sd=0.246,lower.tail=F) + pnorm(2.8,mean=3,sd=0.246)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" ![jpeg](../galleries/coursera-statistics/3w61.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/79) 14:01*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So this is the complete steps of Hypothesis Testing Framework.\n",
"\n",
"1. Remember that the Hypothesis is always about the population parameter, where NH = null value, and AH $\\neq$ null value.\n",
"2. Calculate the sample statistics, if we want to estimate the mean of population, we calculate mean sample and set that as point estimate.\n",
"3. Check that the distribution is normal/skew, check that we indeed have random sample in our sample by following condition 2. Plot boxplot or histogram to see the shape of your sample distribution.\n",
"4. Next, draw sampling distribution, shaded the cut off value to get better understanding, and calculate test statistics.\n",
"5. Finally, after making calculation, be sure to interpret the data based on the research question."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Hypothesis Testing(mean) example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w62.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/81) 02:19*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So now we're taking one problem, and follow the steps of HTF. This example also stated that sample collected by gathering thirty-six mother that has gifted children, testing the children when they achieved age of four.\n",
"\n",
"1. We set the hypothesis, from the question we know that the there's two competing events, one true average of IQ mother of gifted children vs true average IQ population. We then know that $\\mu$ is 100. \n",
"2. We check the point estimate, since we're talking about average here, we calculate $\\bar{x}$ and that is 118.2\n",
"3. We check the condition. Other than less than 10% population, we know that the researcher sample one mother, therefore a mother should be independent for another mother that has 4 year old children. We also now the shape of the distribution is nearly normal, so more than 30 should be enough.Following this condition, we can expect our sampling distribution is normal."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w63.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/81) 04:26*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we do step 4.\n",
"\n",
"By knowing $\\bar{x}$ and the sample size, we can compute the standard error. When we draw the sampling distribution. We know that the width of the distribution is very skinny. calculating The Z-score what we get is 16.8. So 16.8 standard deviation away from the mean! This is actually very far.Because we're doing two side tail, we multiply by 2. Doing R, the p-value we get is"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1] 2.236673e-63\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%R\n",
"\n",
"pnorm(118.2, mean=100,sd=1.083,lower.tail=118.2 < 100) * 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We get very low probability approximately zero. In this case we're reject Null Hypothesis, that stated true average of IQ mother gifted children vs IQ population is not due to chance. Thus we reject it and favor the alternative hypothesis, that stated it's not the same.Therefore we provide strong evidence that there is a difference between average of IQ mother of gifted children and average IQ population at large. And that's step 5, **Make a decision and interpret based on context research question.** Because we reject the null hypothesis, the interval of IQ mother sample is not capturing true average of IQ population."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w64.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/81) 06:55*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next for new problem, we want to provide an evidence of difference between cat sleep and dog sleep.Remember that test-statistics == Z value. And thus we can directly calculate two-tailed t-test equals 0.0836. The test statistics is gathered that given the dog is slept average of 14 hours/day, what are the probability of the cat, in the sample which slept 16 hours a day."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w65.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/81) 09:19*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because this is two-tailed test, and 16 is 2 more from the mean, 14, we can also say 2 less than the mean, which is 12. So two-tailed p-value intrepret as the probability of extreme random sample 16 hours or more or 12 hours or less, is 0.083. Remember the p-value can be interpret two things. One is that either we keep or reject the null hypothesis. Second we can state the probability(**p-value**) of at least as extreme or more(2 hours) from the observed value, 14 hours a day.So the probability that we get from our sample is 0.0836."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inference for other estimators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far we've learned about doing inference that we gather from sample, like sample mean and sample standard deviation infer the unknown population mean. Turns out HT isn't just that, next we begin to open a variety of estimate within HT and confidence interval."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w66.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/83) 01:28*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So other than mean, we also can calculate the proportion. Remember that proportion is between 0 to 1. And not only mean and sample of one sample, but also the difference between two sample, for example two sample group."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w67.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/83) 02:13*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fortunately, based on CLT, sampling distribution always gives aprroximately normal distribution and centered around true mean population, or other point estimate that just listed. Therefore, the it will give unbiased estimate from the true population parameter.\n",
"\n",
"So remember again, confidence interval for nearly normal point estimates is:\n",
"\n",
"$$ pointestimate \\pm z* x SE$$\n",
"\n",
"Note that for all confidence interval, we always start with point estimate, and give plus or minus based on that. The plus minus difference will be our *margin of error* and it incorporates z-star, assume that our sampling distribution is nearly normal. Remember that for CI we must follow the normal condition and unbiased. CI is always applicable to all kind of point estimate, but Standard Error has different calculations for each of them. So remember, all requirements and the calculations are the same, except the calculations about SE."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w68.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/83) 05:28*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this problem, the standard error has been given. Here we see that it know has point estimate, proportion denoted as $\\hat{p}$ , confidence level, and standard error. Plug all those we have our interval.\n",
"\n",
"HTF is also applicable, same as CI but still follow the conditions unbiased and normal shape distribution. The formula is:\n",
"\n",
"$$Z = \\frac{pointestimate - nullvalue}{SE}$$\n",
"\n",
"Consider following example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w69.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/83) 08:23*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we calculate the difference of sample mean between two group. As before we convert the problem into our HTF.\n",
"\n",
"1. Set the Hypothesis. Here we are asked to provide strong evidence that men and women has different BF%. So we set H0 : $\\mu$ men = $\\mu$ women. The alternative, HA: $\\mu$ men $\\neq$ $\\mu$ women\n",
"2. Next we calculate the point estimate. In this case the point estimate is the difference of sample mean between two group, hence simple substracting\n",
"$$\\bar{x} men - \\bar{x} women = 23.9-35 = -11.1$$\n",
"3. Check the conditions. we already given information that the distribution of point estimate is nearly normal. In Addition, we also know that the it's national survey. Hence we expect to safe because they already randomly sample and have independent samples."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jpeg](../galleries/coursera-statistics/3w70.jpg)\n",
"\n",
"\n",
"*Snapshot taken from [Coursera](https://class.coursera.org/statistics-003/lecture/83) 10:03*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So if we plot it into sampling distribution, where's it has centered? Since the sampling distribution *centered around the null value* we observe each of mean female and male. Since they're not given, that's not matter. Because under the assumption that both are same value, we can expect the differece is 0, so it will centered around 0. We have the cut-off value of -11.1 the difference, and have two-sided t-test. When draw the distribution and shade the p-value, it's really small. Remember that we're looking the difference, hence it's exclusive within distribution. So more than 11.1 or less than -11.1. Calculating the Z-score we got 97.36. So far standard deviation away from the mean, which mean p-value is bound to be zero. Thus we reject the null hypothesis, and **doing step 5**, we intepret it as the statement *These data provide strong evidence that there is a different between true average BF% of men vs women population*."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember that CI and HT is agree with each other. CI always have interval that capture null value in HT. We will discuss this in the next section."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **REFERENCES**:\n",
"\n",
"> * https://class.coursera.org/statistics-003/"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}