*! version 3.0.0 10/22/93 STB-24: snp7.1 program define spline if "`*'"=="" { di in bl "-> spline yvar xvar [if] [in] [,[noGraph] [gen(newvar)]" di in bl _skip(10) "[Nknots(#)] [Knots(#...#)] [graph_options] [logit]" di in bl _skip(10) "[REGress(command)] [Dead(deadvar)] [STrata(varname)]]" exit 198 } version 3.0 local varlist "req ex min(2) max(2)" local if "opt" local in "opt" #delimit ; local options "noGraph GEN(string) Nknots(int 0) Knots(string) REGress(string) Dead(string) STrata(string) LOGIT TItle(string) Symbol(string) Connect(string) *"; #delimit cr parse "`*'" if "`gen'"~="" { confirm new var `gen' } if "`dead'"!=""{ di in red "Use sp_adj with censored data" exit } if "`strata'"!=""{ di in red "Use sp_adj with stratified data" exit } parse "`varlist'", parse(" ") tempvar Y X SPLINE quietly { gen `Y'=`1' `if' `in' gen `X'=`2' `if' `in' replace `X'=. if `Y'==. count if `X'~=. local cnt = _result(1) if `cnt'<3 { noisily error 2001 } _crcslbl `Y' `1' _crcslbl `X' `2' sort `X' local kN = `X'[`cnt'] local k0=`X'[1] * GENERATE INTERIOR KNOTS if "`knots'"=="" { local nk = `nknots' if `nk' == 0 { local nk = int((`cnt')^.25) } local j = `nk' while `j' > 0 { local k`j'=`X'[int((`j'*`cnt'/(`nk'+1))+.5)] local j = `j' -1 } } else { parse "`knots'", parse(" ,") local nk = 0 while "`1'"!="" { if "`1'"!=","{ if `1'<`k0' | `1'>`kN' { noisily di "knot at `1' ignored"} else { local nk = `nk' + 1 local k`nk' = `1' } } macro shift } } * CREATE REGRESSION VARIABLES local j = `nk' local covars "`X'" while `j' > 0 { tempvar cov`j' #delimit ; gen `cov`j''= -((`kN'-`k`j'')/(`kN'-`k0'))*(`X'-`k0')^3 ; /* old replace `cov`j''=`cov`j''+(`X'-`k`j'')^3 if `X'>`k`j'' ; */ replace `cov`j''=`cov`j''+(`X'-`k`j'')^3 if `X'>`k`j'' & `k`j'' > `k0' ; replace `cov`j''=(`kN'-`k`j'')*(`k`j''-`k0') *(`kN'+`k`j''+`k0'-3*`X') if `X'>`kN' ; * replace `cov`j''=`cov`j'' - ((`k`j''-`k0')/(`kN'-`k0')) * *(`kN'-`k`j'')^3 if `X'>`kN' ; #delimit cr local covars "`covars' `cov`j''" local j = `j' -1 } if "`regress'"==""{ local regress "regress"} * if "`dead'"!=""{local dead "d(`dead')" } * if "`strata'"!=""{local strata "st(`strata')"} * `regress' `Y' `covars',`dead' `strata' `regress' `Y' `covars' if substr("`regress'",1,4)!="clog" {predict `SPLINE'} else {clogitp `SPLINE',`strata'} if "`regress'"=="poisson" {replace `SPLINE'=exp(`SPLINE')} if "`logit'" == "logit" { local adj = 1/`cnt' replace `SPLINE' = `adj' if `SPLINE'<`adj' replace `SPLINE' = 1-`adj' if `SPLINE'>(1-`adj') &`SPLINE'<. replace `SPLINE' = log(`SPLINE'/(1-`SPLINE')) sum(`SPLINE') local adj=(_result(6)-_result(5))/25 #delimit ; replace `Y'=`Y'*(_result(6) +(1.3+uniform()) *`adj') +(1-`Y')*(_result(5) -(1.3+uniform()) *`adj'); #delimit cr } * GRAPH (If required) if "`graph'" != "nograph" { if "`title'" ==""{ local title "Natural Cubic Spline" } if "`symbol'"==""{ local symbol "oi" } if "`connect'"==""{ local connect ".l" } #delimit ; noisily graph `Y' `SPLINE' `X', `options' sort ti(`title') s(`symbol') c(`connect') ; #delimit cr } if "`gen'"~=""{ rename `SPLINE' `gen'} } end