-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathChapter7.do
More file actions
156 lines (102 loc) · 5.17 KB
/
Copy pathChapter7.do
File metadata and controls
156 lines (102 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
*******************************************************************************
*General Do-File Header*
clear all //clear memory
capture log close //close any log-files (if still running)
*version 15.0 //your current version, change this to your version (and remove asterisk)!
set more off //do not halt to show all output
*******************************************************************************
*cd "path" //enter the path to your working directory and uncomment this line
sysuse nlsw88, clear //open the dataset
*set scheme plotplain //Uncomment if you want to use the same style as in the book
***Regression***
/*We will use the following regression model for all diagnostics throughout
this chapter*/
regress wage c.ttl_exp i.union i.south c.grade
***Exogeneity***
estat ovtest //Ramsey test
/*The significant result (0.05 > 0.073) tells us that there might be
something wrong with the model, possibly some important variables are missing*/
***Linearity***
twoway (scatter wage ttl_exp) ///
(lfit wage ttl_exp) (lowess wage ttl_exp) //Visualize relation graphically
ssc install binscatter, replace //Install ado if not installed yet (Version 13+)
binscatter wage ttl_exp //Better scatterplot
*Alternative way of detection*
regress wage c.ttl_exp i.union i.south c.grade
predict r1, resid //Create residuals in variable "r1"
scatter r1 ttl_exp //Create scatterplot with "ttl_exp"
binscatter r1 ttl_exp //Scatter with other command
/*As long as there is no pattern visible the relation is linear*/
***Nested models***
regress wage c.ttl_exp i.union i.south c.grade //Complex Model
estimates store complex
regress wage c.ttl_exp if _est_complex
/*This procedure guarantees that both models use the same number of cases. Start with your
most complex model and then "work your way down" to the most simple one*/
***Multicollinearity***
quietly regress wage c.ttl_exp i.union i.south c.grade
estat vif
*vif //The old command
/*As we do not see any variables with values larger than 10 we can conclude that
there is no great amount of multicollinearity in the data. If you find
variables with large values exlude them from the analysis and run
the regression again. This does not hold for higher ordered terms as these always
have a high correlation to their derived variable */
***Heteroscedasticity***
quietly regress wage c.ttl_exp i.union i.south c.grade
rvfplot, yline(0)
/*As the data points are not distributed homogeneously, we might encounter
large heteroscedasticity here (Note the triangular shape of the data cloud)*/
estat hettest //Formal test
/*As the value is smaller than 0.05 we conclude that the result is highly
significant, which underlines that we have to deal with heteroscedasticity*/
*Option 1: Transform dependent variable manually*
histogram wage //Inspect distribution of dependet variable
gladder wage //See which transformation works best --> Log
generate lwage = log(wage) //Create logarithmized variable of log
regress lwage c.ttl_exp i.union i.south c.grade //Run regression again
rvfplot, yline(0) //Inspect residuals
estat hettest //Test again
/*After transforming the dependent variable the distribution of the
residuals is much more homogeneous and the p-value is larger, which tells
us that we clearly reduced heteroscedasticity. */
*Option 2: Transform dependent variable automatically*
bcskew0 wage_trans = wage //Make variable as symmetrical as possible
quietly regress wage_trans c.ttl_exp i.union i.south c.grade
rvfplot, yline(0) //Inspect residuals
estat hettest //Not significant anymore
*Option 3: Use robust standard errors*
regress wage c.ttl_exp i.union i.south c.grade, vce(robust)
/*Note that regression coefficients are unchanged, only the standard
errors are different (and therefore also p-values and significance levels*/
*Option 4: Transform, predict and Retransform*
*Make sure to generate lwage as described above!
regress lwage c.ttl_exp i.union i.south c.grade
margins union, at(ttl_exp=(0(4)24)) expression(exp(predict(xb)))
marginsplot
***Influential observations***
*DFBETAS*
regress wage c.ttl_exp i.union i.south c.grade
dfbeta //Create dfbeta-variables
scatter _dfbeta_1 idcode, mlabel(idcode) //Inspect problematic cases visually
scatter _dfbeta_2 idcode, mlabel(idcode)
scatter _dfbeta_3 idcode, mlabel(idcode)
scatter _dfbeta_4 idcode, mlabel(idcode)
graph box _dfbeta_1 _dfbeta_2 _dfbeta_3 _dfbeta_4 //Boxplot
display 2 / sqrt(1846) //Calculate rule of thumb limit
count if _dfbeta_1 > 0.0465 & !missing(_dfbeta_1) //Count number of problematic cases
list if _dfbeta_1 > 0.0465 & !missing(_dfbeta_1) //Inspect number of problematic cases
*Cooks Distance*
regress wage c.ttl_exp i.union i.south c.grade
predict cook, cooksd
scatter cook idcode, mlabel(idcode)
list wage ttl_exp union south grade if idcode == 856 //Inspect strange case
count if cook > 4/1876 //Count problematic cases
lvr2plot, mlabel(idcode) //Leverage VS squared residuals plot
***Local variables / local macros***
local controls c.age c.tenure i.race
regress wage i.union `controls'
*You have to select the two lines above and run them at the same time!
***Global variables / glöobal macros***
global test grade wage
summarize $test