{"id":157,"date":"2018-03-04T12:20:00","date_gmt":"2018-03-04T11:20:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/03\/04\/hypothesis-testing-how-to-perform-paired-sample-t-test\/"},"modified":"2019-08-20T10:24:18","modified_gmt":"2019-08-20T08:24:18","slug":"hypothesis-testing-how-to-perform-paired-sample-t-test","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/hypothesis-testing-how-to-perform-paired-sample-t-test\/","title":{"rendered":"Hypothesis Testing &#8211; How to Perform Paired-Sample t-Test (With Python Codes)"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\">\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>What is paired-sample t-test?<\/b><br \/>\nThis is also called two-sample t-test or dependent samples t-test.<br \/>\nThis is a test used to compare the means of two populations. Here two samples are provided where corresponding measurements from the two sample could form pairs.This means that the number of measurements from the two samples would be the same.<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">That is n1 = n2<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>Scenario where it can be applied<\/b><br \/>\nSome of the scenarios where paired t-test could e applied include:<br \/>\nBefore-and-after osbservations with the same subject. Examples could be weights of respondents before and and after a weight-loss therapy, test scores of student after taking an intensive prep etc<br \/>\nAnother scenario wuold be comparing two different methods of measurement on the same subjects. Example would be comparing the effect of treatment with injection with treatment with tablets on the same group of patients.<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"8227894917\"><\/ins><br \/>\n<b>How to Carry out Paired-Sample t-Test (Step-by-Step Procedure)<\/b><br \/>\nAssuming a sample of n students\u00a0 were underwent a two-weeks tutorial towards the end of the semester. During this tutorials, past questions and answers were discussed and solved. We want to know how effective the two-weeks tutorial was.<br \/>\nSo a test was given to the students before the tutorial and their scores are recorded. After the tutorial, a test was also given to the same set of studend and the scores were recored.<br \/>\nIn this case paired sample t-test will help us achieve this objective<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">Let:<br \/>\nx = test scores of the students before the tutorial<br \/>\ny = scores of the students after taking the tutorial<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>Step 1: Set up the null and alternate hypothesis<\/b><\/p>\n<p><b style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">Step 2: Tabulate the given values with <\/b><span style=\"color: #555555; font-family: segoe ui;\"><span style=\"font-size: 18px;\"><b>columns<\/b><\/span><\/span><b style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">\u00a0for <\/b><span style=\"color: #555555; font-family: segoe ui;\"><span style=\"font-size: 18px;\"><b>difference<\/b><\/span><\/span><b style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">\u00a0as shown below<\/b><br \/>\n<span style=\"color: #555555; font-family: segoe ui;\"><span style=\"font-size: 18px;\">Normally you can use a spreadsheet like excel to to this<\/span><\/span><\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/4.bp.blogspot.com\/-4BviuZRwFz4\/WpvgkzxXI1I\/AAAAAAAABCA\/l9AMF_xl3MgogoB07tvc9SM3fqljKzygACLcBGAs\/s1600\/t-test%2Btable%2Bcolumns.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/4.bp.blogspot.com\/-4BviuZRwFz4\/WpvgkzxXI1I\/AAAAAAAABCA\/l9AMF_xl3MgogoB07tvc9SM3fqljKzygACLcBGAs\/s400\/t-test%2Btable%2Bcolumns.jpg\" width=\"400\" height=\"37\" border=\"0\" data-original-height=\"113\" data-original-width=\"1204\" \/><\/a><\/div>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><a href=\"https:\/\/www.youtube.com\/watch?v=7PTATUS9-24\" target=\"_blank\" rel=\"noopener\">Watch a video on how to do this.<\/a><\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>Step 3: Calculate the mean difference<\/b><br \/>\nTo do this, you need to first subtract\u00a0 the corresponding values for each pair. Then you find the mean of this new column D<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>Step 4: Calculate the standard deviation of the differences<\/b><br \/>\nTo to this, you need to subtract the mean difference for each value of D. That would give you the 5th column of the table. Watch the video to get it clearer.<br \/>\nThe formula for the standard deviation is:<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/3.bp.blogspot.com\/-DaClsR2jUEE\/WptaC4IsMaI\/AAAAAAAABAo\/bub3tCDA1XknZ_BF2MGsmWOa56z2m9dZgCEwYBhgL\/s1600\/Standard-deviation-formular%2Bfor%2Bpaired%2Bsample.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/3.bp.blogspot.com\/-DaClsR2jUEE\/WptaC4IsMaI\/AAAAAAAABAo\/bub3tCDA1XknZ_BF2MGsmWOa56z2m9dZgCEwYBhgL\/s320\/Standard-deviation-formular%2Bfor%2Bpaired%2Bsample.jpg\" width=\"320\" height=\"83\" border=\"0\" data-original-height=\"113\" data-original-width=\"430\" \/><\/a><\/div>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">Remember you need to take square root, to get the standard deviation<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>Step 5: Calculate the Standard Error<\/b><br \/>\nThis is given by the formula<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/1.bp.blogspot.com\/-JQAjYzF-ZL8\/WpvC7ZsbMgI\/AAAAAAAABBo\/qyudK4L6gGguw-DAYRcFR-zAbGmNjCj7ACEwYBhgL\/s1600\/Standard-error-formular.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/1.bp.blogspot.com\/-JQAjYzF-ZL8\/WpvC7ZsbMgI\/AAAAAAAABBo\/qyudK4L6gGguw-DAYRcFR-zAbGmNjCj7ACEwYBhgL\/s200\/Standard-error-formular.jpg\" width=\"200\" height=\"85\" border=\"0\" data-original-height=\"99\" data-original-width=\"229\" \/><\/a><\/div>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>Step 5: Calculate the t statistic<\/b><br \/>\nThe t statistic can be calculated using the formula. That is, the mean difference divided by the standard error value.<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/4.bp.blogspot.com\/-gJXrUnthwaI\/WpvEDRzCPUI\/AAAAAAAABB0\/ySNs-Ut-u3Yc8mR4_QVv_W7Zb_IbV_pJACEwYBhgL\/s1600\/t-test-formula-using-standard-error.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/4.bp.blogspot.com\/-gJXrUnthwaI\/WpvEDRzCPUI\/AAAAAAAABB0\/ySNs-Ut-u3Yc8mR4_QVv_W7Zb_IbV_pJACEwYBhgL\/s200\/t-test-formula-using-standard-error.jpg\" width=\"200\" height=\"109\" border=\"0\" data-original-height=\"114\" data-original-width=\"209\" \/><\/a><\/div>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>Step 6: Look up the t value from table of t-distibution<\/b><br \/>\nTo to this, you need to know:<br \/>\nthe degree of freedom df, given by n-1<br \/>\nwhere n is the number of samples<br \/>\nAlso the significance level, which is normally given. Most times it it 0.05<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\"><b>Step 7: Compare the tabulated t and calculated t<\/b><br \/>\nIf the calculated value of t is greater than the tabulated value, this means that there is significant difference between the two means. But if the calculated value of t is less than the tabulated value, this means that there is no significant difference between the two.<\/p>\n<p><b style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">Step 8: State your <\/b><span style=\"color: #555555; font-family: segoe ui;\"><span style=\"font-size: 18px;\"><b>conclusion<\/b><\/span><\/span><b style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">\u00a0<\/b><br \/>\n<span style=\"color: #555555; font-family: segoe ui;\"><span style=\"font-size: 18px;\">\u00a0Your conclusion would be based on the set up of your null and alternate hypothesis. You can either state that: &#8216;based on the&#8230;. we therefore conclude that the tutorials does not have any effect on students performance&#8217; or &#8216; we therefore conclude that the tutorials leads to a significant improvement\u00a0in the performance of students on the test&#8217;<\/span><\/span><\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">Sample Question Solved here<\/p>\n<p style=\"color: #555555; font-family: 'segoe ui'; font-size: 18px;\">Watch a video on how to use excel to generate mean, Sd and difference.<\/p>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Python Code for One-Sample t-Test<\/strong><\/p>\n<p>You can perform paired-samples t-test in Python using the scipy module. The Python Code is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># How to Perform one-sample t-Test in Python<\/span>\r\n<span style=\"color: #888888;\"># Example 1:<\/span>\r\n<span style=\"color: #888888;\"># Six students were chosen at random from a class and given a math test. <\/span>\r\n<span style=\"color: #888888;\"># The teacher wants the class to be able to score 70 on the test. <\/span>\r\n<span style=\"color: #888888;\"># The six students get scores 62, 92, 75, 68, 83 and 95. <\/span>\r\n<span style=\"color: #888888;\"># Can the teacher be 95% confident that the mean score for the class would be 70?<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">scipy<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> stats <span style=\"color: #008800; font-weight: bold;\">as<\/span> st\r\nscores <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">62<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">92<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">75<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">68<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">83<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">95<\/span>]\r\nst<span style=\"color: #333333;\">.<\/span>ttest_1samp(scores, <span style=\"color: #0000dd; font-weight: bold;\">70<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The Output is shown below:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-922 size-large aligncenter\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/03\/One-Sample-t-Test-1024x561.jpg\" alt=\"One-Sample t-Test in PYthon\" width=\"640\" height=\"351\" srcset=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/03\/One-Sample-t-Test-1024x561.jpg 1024w, https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/03\/One-Sample-t-Test-300x164.jpg 300w, https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/03\/One-Sample-t-Test-768x421.jpg 768w, https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2018\/03\/One-Sample-t-Test.jpg 1561w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is paired-sample t-test? This is also called two-sample t-test or dependent samples t-test. This is a test used to compare the means of two &hellip; <\/p>\n","protected":false},"author":1,"featured_media":923,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[223],"tags":[544],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/157"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":5,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":924,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/157\/revisions\/924"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/923"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}