-
Notifications
You must be signed in to change notification settings - Fork 7
/
demo1.do
73 lines (68 loc) · 1.56 KB
/
demo1.do
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
#delim ;
version 12.0;
*
Creation of Figure 1.
SSC packages used:
bspline, keyby, expgen.
Output graphics files created:
figseq3.eps
*;
clear all;
set scheme sj;
sysuse auto, clear;
keyby foreign make;
desc;
*
Linear spline regression models
*;
local xmin=1500;
local xmax=5100;
local xrange=`xmax'-`xmin';
local intmin=`xrange'/12;
global tflist;
foreach nint of num 1(1)4 {;
local knotdiff=(`xmax'-`xmin')/`nint';
flexcurv, xvar(weight) power(1) refp(`xmin'(`knotdiff')`xmax') gene(ls`nint'_);
desc ls`nint'_*;
regress mpg ls`nint'_*, noconst nohead;
* Compute predicted values *;
preserve;
clear;
set obs `=`xmax'-`xmin'+1';
gene byte nint=`nint';
gene long predseq=_n;
lab var predseq "Prediction sequence number";
gene weight=`xmin'+_n-1;
compress;
flexcurv, xvar(weight) power(1) refp(`xmin'(`knotdiff')`xmax') gene(ls`nint'_);
predict mpghat;
summ predseq weight mpghat, de;
tempfile tfcur;
keyby nint weight;
desc;
save `"`tfcur'"', replace;
global tflist `"$tflist `"`tfcur'"'"';
restore;
};
*
Produce plots
*;
preserve;
expgen =4, copyseq(nint);
lab var nint "Number of intervals between knots";
keyby nint foreign make;
append using $tflist;
gene byte nknot=nint+1;
lab var nknot "number of knots";
keyby nknot foreign make predseq, miss;
desc;
scatter mpg weight || line mpghat weight, sort lpattern(solid) || ,
by(nknot, compact row(2) legend(off))
xlab(`xmin'(`intmin')`xmax', angle(270) grid)
ylab(10(5)45, angle(0))
ytitle("Mileage (mpg)")
xsize(4) ysize(4);
graph export figseq3.eps, replace;
more;
restore;
exit;