UNIVERSITY OF CALIFORNIA, LOS ANGELES
Department of Economics
Economics 143 (Cameron) - Applied Regression
Analysis
Computing Lab Session #2:
GNUPLOT using SHAZAM; SHAZAM
Distribution Visualization Program
Topics to be covered:
- In this lab session, we will complete the introduction to SHAZAM by going over
the remainder of the instructions on the handout entitled "How to
Run Basic Types of Shazam Programs..." that was distributed in the lecture.
In particular, our first session did not cover the procedure for using SHAZAM in
conjunction with GNUPLOT to get laser printouts of nice plots. We will address
this task in this lab session.
- We will execute a "feet first" plunge into an example of using SHAZAM to plot
the shapes of common distributions with different parameter values. We will look
briefly at the tasks performed in the program entitled n:plotdist.sha. Do not feel uncomfortable about the
complexity of the program. You will understand it by then end of the quarter.
Now, there will be only a cursory overview of what is IN it, and we will
concentrate on passive consumption of what it produces for us.
- You may try any on-line self-grading quizzes you have not yet attempted.
Using the Distribution Plotting Program
If all you wanted to do was plot distributions, there are better graphics
programs you might want to use. Shazam can be programmed to plot distributions
however, so I have done this. The exercise will introduce you to the concept of
"subroutines" or "macros" or "procedures." These are blocks of commands that take
as input a number of parameters, can then be executed for those particular
parameter values, and produce some sort of output. You need not issue the entire
block of commands each time you want to do something similar. Instead, you set
the values of the input parameters as desired, then just "execute" the
procedure.
Here are the things you want to do:
- Start SHAZAM for Windows and open the n:plotdist.sha program file.
If you are working from home, and you have your own
copy of the plotdist program, you will need to change the drive and directory
appropriately. Review the
plotdist.sha commands. This program is far more complex than the ones you will be writing, but
we will use it to illustrate some of the points from the lectures.
- Note that there are some initial default settings for the input parameters, so
that you can run the procedures under these settings and see "initial" plot
shapes. The defaults are N(0,1), t(30), F(4,40), and chi-squared(4). Also
initially, something called "scale" is set to 1 for "on," and maxf, maxchi, minn,
and maxn have default settings.
- Now run the program file n:plotdist.sha. Note that SHAZAM does all of the initial steps for you. You are just going to add some commands to have SHAZAM do additional things for you.
- To look at the default normal distribution: exec ndistn. Then plot a
nice picture of this normal probability density function: plot normpdf x / gnu
line. Close the gnuplot window to continue with the program.
Then look at the corresponding cumulative density function: plot
normcdf x / gnu line. You might then want to look at both of these functions
on the same plot: plot normcdf normpdf x / gnu line.
- Now consider a different normal distribution. Change the mean and standard
deviation parameters using the gen1 command: gen1 mu=10, then gen1
sig=3.5. Now create the data for plotting this distribution: exec
ndistn. Then plot the results: plot normcdf normpdf x / gnu line.
What do you notice about the shapes of these curves compared to the previous ones?
What do you notice about the spread? About the height of the curves at the
mean?
- Now turn off the automatic scaling of the axes: gen1 scale=0, and
rerun the procedure: exec ndistn. Now plot the probability density
function: plot normpdf x / gnu line. This is the default range, which can
be changed to anything else using commands such as: gen1 minn=0, and
gen1 maxn=50. Try this, then run the procedure again with exec
ndistn. Now look at the pdf: plot normpdf x / gnu line.
- Now clean up what you have done with the normal distribution by putting back
its parameters and rerunning it: gen1 scale=1, gen1 mu=0, gen1
sig=1. Then exec ndistn. This restores the standard normal. Now save
the standard normal probability density function with genr
nor01=normpdf.
- Try a t-distribution with 30 degrees of freedom (the default): exec
tdistn. Plot the results: plot tpdf t / gnu line. Now save this curve
by renaming to something that won't be overwritten next time you execute the
procedure: genr t30=tpdf.
- Try a t-distribution with fewer degrees of freedom, say 5: gen1 df=5.
Rerun the procedure: exec tdistn. Plot the results: plot tpdf t / gnu
line. Rename to save for later: genr t5=tpdf.
- Now compare the t-distributions with 30 and 5 degrees of freedom on the same
plot: plot t30 t5 t / gnu line.
- Now try a t(100) distribution and compare to a standard normal: gen1
df=100. Then exec tdistn. Then genr t100=tpdf. Finally,
plot t100 nor01 t / gnu line. What do you notice about these two
distributions? If you like, try the same comparison with the t30 or t5
distributions that remain in the program's memory at this point. What do you
notice?
- Try the default F-distribution, with "4 and 40" degrees of freedom. Use
exec fdistn. Then plot fpdf f / gnu line. What is different about
the shape of this distribution, compared to the normal and the t?
- Now change the value of maxf: gen1 maxf=2. Then exec fdistn.
Then redo the plot: plot fpdf f / gnu line. What has
happened?
- Lastly, take a look at the shape of a Chi-squared distribution with four
degrees of freedom (the default). Use exec chidistn. Then plot chipdf
chi / gnu line. What do you observe about the shape of this chi-squared
distribution? Save this curve by renaming: genr chi4=chipdf
- Now change the degrees of freedom to 2 with gen1 k=2. Rerun the
procedure with exec chidistn, and do the plot: plot chipdf chi / gnu
line. What has changed? Save this curve by renaming: genr
chi2=chipdf. Compare the two with: plot chi4 chi2 chi / gnu line.
What difference does it make to the distributions to change the degrees of
freedom?
At your leisure (what's that?) you are welcome to explore the shapes of these
useful distributions for other parameter values. As an alternative, you might want to
check out two JAVA applets
that have been developed by Geoff Gerdes and me to help students
visualize the characteristics of different distributions in the t and F families.
Updated: October 13,1998, prepared by: Trudy Ann Cameron; site index