-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregress_yhat_ci.ado
executable file
·108 lines (72 loc) · 2.59 KB
/
regress_yhat_ci.ado
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
/***
Title
======
__regress_yhat_ci__ -- Generate predictions and associated confidence intervals from a linear regression model.
Syntax
------
__regress_yhat_ci__ [, forecast alpha(int) stub(str) drop]
- - -
Options:
> __forecast__ : If specified, the command generates individual forecast intervals instead of population mean intervals (which is the default output).
> __alpha__ (_int_) : A number; the default is 0.05 which generates 95% confidence intervals. If specified, the command will generate (1-alpha)% confidence intervals.
> __stub__ (_str_) : A string which will serve as a prefix for the generated variables.
> __drop__ : If specified, the command will write over existing variables.
- - -
Description
-----------
__regress_yhat_ci__ will generate predictions and associated confidence intervals from a linear regression model. This command is built on top of the {help predict:{it:predict}} command.
Note
----
This command will generate population mean intervals from a bootstrapped linear model; however, it will not generate forecast intervals from the bootstrapped model.
Example(s)
----------
See {help plotdata:{it:plotdata}}
***/
capture program drop regress_yhat_ci
program regress_yhat_ci
//http://www.stata.com/statalist/archive/2007-03/msg00372.html
syntax [, forecast alpha(str) stub(str) drop]
local cmd = e(cmd)
if "`cmd'" != "regress" {
display "This function only works for linear regression."
error 301
}
if "`forecast'" == "forecast" {
local type = "stdf"
}
else {
local type = "stdp"
}
if "`alpha'" == "" {
local alpha = 0.05
}
local dv = e(depvar)
if strlen("`stub'") > 32-9 {
display as error "Your variable stub has too many characters)."
exit
}
if strlen("`stub'`dv'")+7 > 32 {
display as error "NOTE: " as text "Because STATA variable names are limited to 32 characters,"
display "the generated variables will have a different name than usual. If you used"
display "the drop command, variables may be unintentionally overwritten."
display "Short name: " as error "`stub'y_hat"
display as text "The usual, but overly long variable name: " as error "`stub'`dv'_hat"
local dv = "y"
}
if "`drop'" == "drop" {
capture fussydrop `stub'`dv'_hat
capture fussydrop `stub'`dv'_hat_lb
capture fussydrop `stub'`dv'_hat_ub
}
predict `stub'`dv'_hat, xb
predict _yhatse, `type'
if("`e(df_r)'" == ""){
local z = 1.96
}
else{
local z = invttail(e(df_r),`alpha'/2)
}
generate `stub'`dv'_hat_lb = `stub'`dv'_hat - `z' * _yhatse
generate `stub'`dv'_hat_ub = `stub'`dv'_hat + `z' * _yhatse
drop _yhatse
end