Basic Stata Commands
Para o Stata ler os dados do “txt format”:
insheet using F:\[Link], clear
Para comprimir os dados:
compress
Para gravar os dados no “Stata format”:
save F:\[Link]
Para visualizar variáveis:
describe
Para obter a descrição estatística das variáveis (sumário estatístico):
sum
ou summarize
Para obter frequências absoluta, relativa e cumulativa:
tab variablename
or tabulate variablename
Para criar variáveis de modo a obter termos quadráticos:
gen y2=y^2
gen x2=x^2
Para criar variáveis de modo a obter logaritmos naturais:
gen lny=log(y)
gen lnx=log(x)
Para calcular a correlação entre duas variáveis:
corr y x
ou correlate y x
Para estimar o MRLS (em níveis):
reg y x
Para estimar o MRLS (na forma logaritmica):
reg lny lnx
Para estimar o MRLM (em níveis):
Para estimar o MRLM (na forma logaritmica):
Para obter resíduos (isto é, valores de ):
predict r, resid
ou predict r, residual
Para obter valores ajustados de (isto é, valores de ):
predict yhat
Para apagar observações
drop if town==“Chicago”
(1 observation deleted)
Teste de multicolinearidade:
estat vif
Teste de heterocedasticidade: Teste do Multiplicador de Lagrange de Breusch-Pagan (assume
erros distribuidos normalmente):
estat hettest, normal
Teste de heterocedasticidade de White
estat imtest, white
Teste de não-normalidade dos erros: Teste de Shapiro-Wilk dos dados normais
swilk r
Para construir um histograma de dados a partir dos resíduos
hist r, fraction bin(27) normal
ou histogram r, fraction bin(27) normal
Teste da Forma Funcional: Teste RESET de Ramsey:
estat ovtest
2
Time series data and tsset
To use Stata's time-series functions and analyses, you must first make sure that your data are,
indeed, time-series. First, you must have a date variable that is in Stata date format. Secondly,
you must make sure that your data are sorted by this date variable. Finally, you must use the
tsset command to tell Stata that your data are time-series:
sort datevar
tsset datevar
This example tells Stata that you have simple time-series data.
sort year
tsset year
Teste de correlacção serial: Durbin-Watson d-statistic (assume que não ha endogeneidade)
estat dwatson
Teste de correlacção serial: Teste de Breusch-Godfrey (ou Teste LM de Autocorrelação)
estat bgodfrey
Breusch-Godfrey general test of autocorrelation:
. bgodfrey, lags(1 2 3 4)
Breusch-Godfrey LM test for autocorrelation
---------------------------------------------------------------------------
lags(p) | chi2 df Prob > chi2
-------------+-------------------------------------------------------------
1 | 13.857 1 0.0002
2 | 14.264 2 0.0008
3 | 14.305 3 0.0025
4 | 14.331 4 0.0063
---------------------------------------------------------------------------
H0: no serial correlation
Teste de correlacção serial: Teste de Breusch-Godfrey (where p=3)
estat bgodfrey, lags(1 2 3)
Create lag (or lead) variables using subscripts.
. gen lag1 = x[_n-1]
. gen lag2 = x[_n-2]
. gen lead1 = x[_n+1]
3
You can create lag (or lead) variables for different subgroups using the by prefix. For example,
. sort state year
. by state: gen lag1 = x[_n-1]
If there are gaps in your records and you only want to lag successive years, you can specify
. sort state year
. by state: gen lag1 = x[_n-1] if year==year[_n-1]+1
Panel data and tsset
If you have panel data, then your data must be sorted by the date variable within the variable
that identifies the panel. Finally, you must use the tsset command to tell Stata that your data
are time-series:
sort panelvar datevar
tsset panelvar datevar
This example tells Stata that you have panel data.
egen cid = group(country)
sort cid year
tsset cid year
egen cid = group(country)
sort cid year
tsset cid year
panel variable: cid (strongly balanced)
time variable: year, 1985 to 2003
delta: 1 unit
****** fixed-effects and random effects ******
xtreg x1 x2 … xk, i(cid) fe
xtreg x1 x2 … xk, i(cid) re
******** see the returms of the estimation *******
ereturn list
****** Hausman test ******
xtreg x1 x2 … xk, fe
est store fixed
4
xtreg x1 x2 … xk, re
hausman fixed
Teste de multicolinearidade:
vif, uncentered
****** get residuals *******
predict comb, u
predict comb, e
predict error, ue
Note: Everything depends on which residual you are talking about:
"ue"(combined), "u"(random error) or "e"(overall error)
****** Bartlett's Test for Homogeneity of Variances ******
egen cell=group(country)
oneway y cell
Teste de correlação serial ou Autocorrelação:
tsset cid year
xtregar x1 x2 … xk, fe
Modified Bhargava et al. Durbin-Watson
Baltagi-Wu LBI
xtregar x1 x2 … xk if year !=1985 & year !=2003, fe lbi
xtregar x1 x2 … xk, re
Modified Bhargava et al. Durbin-Watson
Baltagi-Wu LBI
xtregar x1 x2 … xk if year !=1985 & year !=2003, re lbi