Using Stata for Survey Data Analysis


Download 1.39 Mb.
Pdf ko'rish
bet59/61
Sana08.03.2023
Hajmi1.39 Mb.
#1252470
1   ...   53   54   55   56   57   58   59   60   61
Bog'liq
2009 Usingstataforsurveydataanalysis (1)

Using loops  
This command starts a loop, allowing groups of Stata commands to be repeated until some condition 
is met. The syntax is: 
while exp { 
commands 

where exp
is an expression. Stata repeats the commands as long as the expression is
true.
commands 
are any Stata commands that you want to repeat
{ } 
define the beginning and the ending of the commands to be repeated 


Minot
 
 
 
 
 
Using Stata for Survey Analysis 
Page 56 
 

This is an example of a loop that uses local macros to carry out a regression analysis of the 


determinants of housing value for each region: 
tab region, gen(reg_)
local r = 1 
while `r' <= 4 { 
regress housval roof floor wall room area water if reg_`r' == 1
local r = `r' + 1 

The tab command creates a dummy variables for each region (region1, region2, etc). The first local 
command creates a macro called “r” that is equal to 1. The while statement says that the commands 
in brackets will be repeated until the condition r<=4 is no longer true. On each loop, the regress 
command is carried out in one region (when r=3, the if statement is “if reg_3==1). The second local 
command increases the value of r each time that the loop is completed. When r reaches 5, the loop 
stops because the while condition is no longer true. Then Stata goes on to the next command after the 
bracket.
Using matrix algebra  
Stata has a special set of commands for matrix algebra. These can be used to implement custom 
econometric procedures or for doing calculations on the output of regression analysis. This is a very 
short summary of a very long list of complex commands. complex set of commands (type “help 
matrix” for more information).
1. Creating matrices by hand
Examples: 
matrix mymat = (1,2\3,4) 
commas separate elements, backslash indicates new row 
matrix myvec = (1 5 3 1 3) 
creates a row vector 
matrix mycol = (1/5/3/1/3)
creates a column vector 
2. Setting the maximum matrix size
For regular Stata, the default maximum matrix size is 40x40, but this can be increased up to 800x800 
with the matsize command. For Stata SE, the default maximum is 400x400, but this can be increased 
up to 11,000x11,000. The maximum matrix size can be changed using
set matsize 500
sets the maximum size for a matrix at 500x500 
3. Manipulating matrices 
Examples: 
matrix D = B
makes matrix D equal to matrix B 
matrix beta = syminv(X'*X)*X'*y 
calculates beta using regression equation 
matrix C = (C+C')/2
redefines C matrix in terms of old values 
matrix sub = A[1..., 2..5]/2 
defines matrix using sub-set of A matrix 
matrix A[2,2] = B 
redefines subset of A matrix as equal to B 
4. Converting variables into matrices and vice versa
Variables can be converted into matrices and likewise matrices can be converted into variables. Type 
“help mkmat” for more information.



Download 1.39 Mb.

Do'stlaringiz bilan baham:
1   ...   53   54   55   56   57   58   59   60   61




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling