Stata Technical Bulletin
21
logit transforms the fitted values and plots the y-axis on a logit scale. The observations are automatically jittered vertically
and plotted just outside the range of the smoothed curve. This option should only be used with binary data.
nograph suppresses production of the graph.
graph-options are any of the options allowed with graph twoway. spline plots yvar followed by its smooth against xvar; the
default options are s(oi) and c(.l). If there are more than 1000 observations, we advise using s(.i) instead. sp_adj
only plots the smooth against xvar.
Example 1 : spline
In this example spline is used to approximate a sinusoidal signal in the presence of Gaussian noise.
. clear
. set obs 1000
obs was 0, now 1000
. generate x = (-n-500)∕100
. generate sinx = sin(x) + 0.2*invnorm(uniform())
. spline sinx x, xlabel ylabel(-l,-.5,0,.5,1) yline(0)
(graph appears, see Figure 1 )
Example 2: sp adj
In this example, we first generate Poisson data and then use sp_adj to fit the model. The program used to generate Poisson
random variables may be of independent interest. It works using the property that the number of renewals by time 1 in a renewal
process of independent exponential random variables with rate μ has a Poisson distribution with mean μ.
. clear
. set obs 1000
obs was 0, now 1000
. generate age = 40+15*invnorm(uniform())
. replace age = 30+15*uniform() if (age<15) I (age>75)
(43 real changes made)
. generate sex = uniform()<.4
. generate case = mod(-n,2)
. generate mu = age - 3*(log(age)2) + 10*sin(age∕15) + 50/age + 5*sex if case==0
(500 missing values generated)
. replace mu = age -3*(log(age)2) +10*sin(age∕25) +90∕age -exp((age-65)∕10) + 5*sex if case ==1
(500 real changes made)
. program define mk.pois
1. generate tx = -(l∕mu)*log(uniform()) ∕* tx is an exponential random variable */
2. quietly generate int nx = .
3. local i = 0
4. while 'i' < 50 -(
5. quietly replace nx = 'iz if tx> 1 & nx==.
6. quietly replace tx = tx - log(uniform())∕mu
7. local i = "±' + 1
8. >
9. end
. mk-pois
. summarize nx
Variable ∣ Obs Mean Std. Dev. Min Max
---------+-----------------------------------------------------
nx I 1000 10.104 5.975524 0 32
. sp_adj nx age if case==!, kn(20,35,50,65) reg(poisson) adj(sex) gen(spl) nog
. sp_adj nx age if case==0, kn(20,35,50,65) reg(poisson) adj(sex) gen(spθ) nog
The smooths obtained from sp_adj with poisson regression must be transformed back to obtain the estimated mean
function.
. generate snl = exp(spl)
(500 missing values generated)
. generate snθ = exp(spθ)
(500 missing values generated)
. graph snl snθ mu age, sort c(ll.) s(ii.) !!(’'Count”) xlabel ylabel
(graph appears, see Figure 2)