{"id":12,"date":"2011-08-07T11:55:55","date_gmt":"2011-08-07T15:55:55","guid":{"rendered":"http:\/\/www.clayford.net\/statistics\/?p=12"},"modified":"2023-08-16T06:30:09","modified_gmt":"2023-08-16T10:30:09","slug":"explaining-and-simulating-a-chi-square-distribution","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/explaining-and-simulating-a-chi-square-distribution\/","title":{"rendered":"Explaining and simulating a chi-square distribution"},"content":{"rendered":"<p>Randomly select several values, say 10, from a standard Normal distribution. That&#8217;s a Normal distribution with a mean of 0 and standard deviation of 1. Now square each value and sum them all up. That sum is an observation from a chi-square distribution with 10 degrees of freedom. If you picked 12 values and did the same, you would have an observation from a chi-square distribution with 12 degrees of freedom.<\/p>\n<p>We can do this in R as follows:<\/p>\n<pre>\r\n# random sample of 10 values from N(0,1) summed up\r\ns <- rnorm(10)\r\nsum(s^2)\r\n<\/pre>\n<p>When I did it, I got the following:<\/p>\n<pre>\r\ns <- rnorm(10)\r\ns\r\n[1] 2.3235602\u00a0 0.1339985\u00a0 0.5645995\u00a0 1.3603184 -1.1383273\u00a0 0.1735691\r\n[7] 0.9075259\u00a0 0.2842374 -0.4214184 -0.1069466\r\ns^2\r\n[1] 5.39893181 0.01795559 0.31877258 1.85046616 1.29578902 0.03012623\r\n[7] 0.82360328 0.08079092 0.17759345 0.01143758\r\nsum(s^2)\r\n[1] 10.00547\r\n<\/pre>\n<p>Now let's repeat this 1000 times and make a histogram:<\/p>\n<pre>\r\nx <- rep(NA, 1000)\r\nfor (j in 1:1000){\r\nx[j] <-sum(rnorm(10)^2)}\r\nhist(x)\r\n<\/pre>\n<p><a href=\"http:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-15\" title=\"histogram of 1000 summed sample of 10 from N(0,1)\" src=\"http:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist-300x300.png 300w, https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist-150x150.png 150w, https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist.png 672w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You can click on the image to see bigger version. Obviously we don't see the Normal-looking bell-shape. It skews right. Also notice that since we squared all our values, every sum is positive, which means the histogram starts at 0. Now let's plot a chi-square curve with 10 degrees of freedom on top of our histogram:<\/p>\n<pre>\r\nh <- hist(x, plot=F)\r\nylim  <- range(0, h$density, 0.10)\r\nhist(x, freq=F, ylim=ylim)\r\ncurve(dchisq(x,df=10), add = TRUE)\r\n<\/pre>\n<p><a href=\"http:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-17\" title=\"histogram with chi-square curve\" src=\"http:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist21-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist21-300x300.png 300w, https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist21-150x150.png 150w, https:\/\/www.clayford.net\/statistics\/wp-content\/uploads\/2011\/08\/hist21.png 672w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>I had to experiment a little to get the 0.10 value for the range statement. That was necessary to do in order to keep R from setting the displayed range of the y axis from being too small.<\/p>\n<p>The formula for the chi-square curve (i.e., the probability density function) is<\/p>\n<p>$$ f(x)=\\frac{1}{\\Gamma(r\/2)2^{r\/2}}x^{r\/2-1}e^{-x\/2}$$<\/p>\n<p>where r is degrees of freedom and x must be positive. And, yes, I think it looks pretty frightening if you're not used to that sort of thing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Randomly select several values, say 10, from a standard Normal distribution. That&#8217;s a Normal distribution with a mean of 0&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/explaining-and-simulating-a-chi-square-distribution\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,8,13],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-continuous-distributions","category-simulation","category-using-r"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":4,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":846,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/12\/revisions\/846"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}