/* This file: chi2dist.do by: Coye Cheshire For fun, lets show how a chi-square distribution works. */ *First, I need to use a lot more memory for stata since I will need a *million cases. If you dont have at least a few gigs of ram, you may *need to reduce the n in my examples below. *set memory to use 1 gig set mem 1g *Now, lets create two random variables, each from a normal distribution. *We will draw a million cases. drawnorm x1 x2, n(1000000) *Following our understanding of a chi-square distribution, we should *sum the squares of these variables. gen twovars = x1^2 + x2^2 *Ok, now lets get the summary stats and view the histogram for this *new "sum of squares" variable. sum twovars hist twovars graph save chi_twovars *Interesting...that looks a lot like what the chi-square distribution *is supposed to look like for df=1. Lets try again with 6 total variables. *I will make 4 more variables, also drawn randomly from a normal distribution. drawnorm x3 x4 x5 x6, n(1000000) * sum the squares. gen sixvars = x1^2 + x2^2 + x3^2 + x4^2 + x5^2 + x6^2 * summary stats and histogram. sum sixvars hist sixvars graph save chi_sixvars * Now lets try it one more time with 10 variables. drawnorm x7 x8 x9 x10, n(1000000) *Again, lets sum the squares. gen tenvars = x1^2 + x2^2 + x3^2 + x4^2 + x5^2 + x6^2 + x7^2 +x8^2 /// + x9^2 +x10^2 *And we now check the distribution and the summary stats. sum tenvars hist tenvars graph save chi_tenvars * This last little feature lets us easily combine our three saved graphs * into one graph. graph combine chi_twovars.gph chi_sixvars.gph chi_tenvars.gph exit.