*! sp_adj -- Calculate conditional spline (snp7: STB-22) *! version 3.0.1 10/09/94 program define sp_adj if "`*'"=="" { di in bl "-> spline yvar xvar [if] [in] [,[noGraph] [gen(newvar)]" di in bl _skip(10) "[Nknots(#)] [Knots(#...#)] [graph_options] " di in bl _skip(10) "[Adjust(varlist)] [REGress(command)] [Dead(deadvar)]]" 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) Adjust(string) TItle(string) Symbol(string) Connect(string) *"; #delimit cr parse "`*'" if "`gen'"~="" { confirm new var `gen' } parse "`varlist'", parse(" ") if "`adjust'"!=""{ _crcunab "`adjust'" local adjust "$S_1" } 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 "`adjust' `X'" while `j' > 0 { tempvar cov`j' #delimit ; gen `cov`j''= -((`kN'-`k`j'')/(`kN'-`k0'))*(`X'-`k0')^3 ; replace `cov`j''=`cov`j''+(`X'-`k`j'')^3 if `X'>`k`j'' ; 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' predict `SPLINE',xb if "`adjust'"!="" { parse "`adjust'",parse(" ") if "`regress'"=="bprobit" | "`regress'"=="blogit" | /* */ "`regress'"=="gprobit" | "`regress'"=="glogit" /* */ { mac shift } while "`1'"!=""{ capture replace `SPLINE'=`SPLINE'-_b[`1']*`1' if _rc!=0 { noisily di in red "`1' dropped from regression" } mac shift } } * GRAPH (If required) if "`graph'" != "nograph" { if "`title'" ==""{ local title "Natural Cubic Spline" } if "`symbol'"==""{ local symbol "i" } if "`connect'"==""{ local connect "l" } #delimit ; noisily graph `SPLINE' `X', `options' ti(`title') s(`symbol') c(`connect') ; #delimit cr } if "`gen'"~=""{ rename `SPLINE' `gen'} } end