UNIVERSITY OF CALIFORNIA, LOS ANGELES
Department of Economics

Economics 143 (Cameron) - Applied Regression Analysis

SHAZAM code [plotsrf.sha]


If you are downloading this SHAZAM code for use on your own computer, select "File", then "Save As...", and save on your own diskette (a:) or your own hard drive (c:\) using the same filename e143sh3.sha.

IMPORTANT: you must then use an editor (like TED) to delete all of the HTML code from the top and the bottom of the file, leaving only the SHAZAM code. The line which reads "* SHAZAM code (e143sh3.sha) downloaded from UCLA Econ 143 (CAMERON) WebSite" should be the first line of your edited program file. Save the edited program as plotsrf.sha



* SHAZAM code downloaded from UCLA Econ 143 (CAMERON) WebSite: 
* HTML file called e143sh3.htm, and should have 
*  been downloaded as plotsrf.sha

********************** CONTENTS OF  n:plotsrf.sha  FILE *********************
* This program, called  n:plotsrf.sha, demonstrates a population regression
* function, PRF, using all of the data, and then shows what happens when
* you take different samples from this population and calculate a
* sample regression function, SRF, for each sample.

* This example uses the data from Gujarati, Essentials of Econometrics,
* Table 5-1, page 119.

set nocolor
* there are 55 observations in the "population"
sample 1 55
read(table5_1.dat) p q
* check out the "true" population regression function; note its parameters.
ols q p
pause

* Now, explore what happens when you draw random samples, with replacement,
* from this population.  Two such random samples appear in Tables 5-2 and
* 5-3 in the textbook, on page 124.

********* HERE'S WHERE YOU DEFINE SAMPLE SIZE AND # SIMULATED SAMPLES *******
* if the exercise takes too long with nsim (number of simulated samples)
*   equal to 100, try 50, or 25 (and change nsamsim accordingly)

* set nsamp to the number of observations in each sample..must be <55
nsamp:10

* set nsim to the number of simulated samples to "draw" from population
nsim:100

* set nsamsim to nsamp*nsim
nsamsim:1000
*****************************************************************************

* make enough space to hold simulated quantities that will be appended
*   in each simulated sample and its regression:

* b1 and b2 are the slope and intercept values for each simulated sample
dim b1 [nsim]      b2 [nsim] 

* qfit holds fitted quantities and pdat holds actual prices in each sample
dim qfit [nsamp] [nsim]     pdat [nsamp] [nsim] 

* In what follows, for each regression:
*  - save estimated coefficients beta:1=slope beta:2=intercept
*  - save fitted values of q in qfit, save associated p values in pdat

********************** HERE ARE THE SAMPLE SIMULATIONS ***********************

do #=1,[nsim]

* first make whole "population" of 55 eligible to be sampled
sample 1 55

* generate a uniformly distributed random number vector length 55 obs.
genr rndm=uni(1)

* sort all of the data according to the values of this random number vector
sort rndm p q

* take the first [nsamp] obs. ...these will be a RANDOM sample from the 55
sample 1 [nsamp]
print q p


* run an ordinary least squares regression, save fitted coefficients
* also save the fitted values of qhat for each value of p in this sample
ols q p / coef=bb predict=qhat

* take a look at fitted values if you like...currently commented out
*print qhat p

* begin to fill in the matrix of b1 coefficients, one for each sample
matrix b1(#)=bb:2

* begin to fill in the matrix of b2 coefficients, one for each sammple
matrix b2(#)=bb:1

* to visualize variability in fitted regression lines across samples
* copy the "data" for each sample into a matrix of q's and a matrix of p's
copy qhat qfit / frow=1;[nsamp] fcol=1;1 trow=1;[nsamp] tcol=#;#
copy p pdat / frow=1;[nsamp] fcol=1;1 trow=1;[nsamp] tcol=#;#

endo
*************************** END OF SIMULATIONS *****************************
pause

* unstack all these data into one long vector each
matrix qd=vec(qfit)
matrix pd=vec(pdat)

* turn the fitted q's and the associated p's for each sample into "data"
sample 1 [nsamsim]
genr qdd=qd
genr pdd=pd

* In order to view the range of fitted SRFs across these [nsim] different
*    samples, each of size [nsamp] (drawn from the population of 55), you
*    will need to enter the following two commands when the program has
*    finished executing (be sure to leave out the comment characters):
* sample 1 [nsamsim]
* plot qdd pdd / gnu
pause

* now look at the joint distribution and marginal distns of b1 and b2
sample 1 [nsim]
genr bb1=b1
genr bb2=b2

* The SRF intercept and slope represent values of a pair of random variables
*     that result from the experiment "draw a sample and fit a regression
*     line."  Thus, they are jointly distributed.  We are interested in
*     seeing the marginal distributions and the joint distribution of these
*     two random variables.  When the program has finished executing,
*     enter the following commands (without comment characters):
* sample 1 [nsim]
* plot bb1 / histo groups=15
* plot bb2 / histo groups=15
* plot bb1 bb2 / gnu
* stat bb1 bb2 / pcor

**** ALL RELEVANT INFORMATION IS NOW IN MEMORY...TRY SUGGESTED COMMANDS ****
set color
* reminder:  There are [nsamsim] total fitted values ([nsim] simulations
*      and [nsamp] observations per simulated sample (qdd, pdd).
* reminder:  There are [nsim] different intercepts and slopes (bb1, bb2)




COURSE OUTLINE LECTURE OUTLINES PROBLEM SETS PROBLEM SOLUTIONS COMPUTER LABS
SHAZAM EXAMPLES DATA SETS ONLINE QUIZZES GRAPHICS HANDOUTS

Updated: February 2, 1998
Prepared by: Trudy Ann Cameron