--- Joseph Coveney wrote:
> What's the most direct way in Stata to generate a set of restricted cubic
> splines (or B splines) for two predictors and their interaction?
The variable names created with -splinegen- allow you to quit easily use
-foreach- to create the interactions. The real challenge is the
interpretation of such a model. You might consider a graph like the one
in the example below. Though I still find it pretty hard to interpret.
*------------- begin example ---------------------
sysuse nlsw88, clear
splinegen tenure 1.5 9
splinegen grade 12 15
foreach i of varlist tenure* {
foreach j of varlist grade* {
gen `i'X`j' = `i'*`j'
}
}
reg wag tenure* grade*
drop _all
set obs 135
gen grade = 4.5*floor((_n-1)/27)
bys grade: gen tenure = _n -1
splinegen tenure 1.5 9
splinegen grade 12 15
foreach i of varlist tenure* {
foreach j of varlist grade* {
gen `i'X`j' = `i'*`j'
}
}
predict yhat
label var yhat "predicted hourly wage"
twoway line yhat tenure if grade == 0 || /*
*/ line yhat tenure if grade == 4.5 || /*
*/ line yhat tenure if grade == 9 || /*
*/ line yhat tenure if grade == 13.5 || /*
*/ line yhat tenure if grade == 18 , /*
*/ legend( order(5 4 3 2 1) /*
*/ label(1 "grade = 0") /*
*/ label(2 "grade = 4.5") /*
*/ label(3 "grade = 9") /*
*/ label(4 "grade = 13.5") /*
*/ label(5 "grade = 18"))
*----------------- end example -------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )
A model that is somewhat easier to interpret is a model
non-linear main effects of tenure and grade, but these
non-linear main effects of tenure change linear with grade
and vice versa. See the example below:
*-------------- begin example -------------------
sysuse nlsw88, clear
splinegen tenure 1.5 9
splinegen grade 12 15
foreach i of varlist tenure* {
gen `i'Xgrade = `i'*grade
}
foreach j of varlist grade* {
gen `j'Xtenure = `j'*tenure
}
reg wag tenure* grade*
drop _all
set obs 135
gen grade = 4.5*floor((_n-1)/27)
bys grade: gen tenure = _n -1
splinegen tenure 1.5 9
splinegen grade 12 15
foreach i of varlist tenure* {
gen `i'Xgrade = `i'*grade
}
foreach j of varlist grade* {
gen `j'Xtenure = `j'*tenure
}
predict yhat
label var yhat "predicted hourly wage"
twoway line yhat tenure if grade == 0 || /*
*/ line yhat tenure if grade == 4.5 || /*
*/ line yhat tenure if grade == 9 || /*
*/ line yhat tenure if grade == 13.5 || /*
*/ line yhat tenure if grade == 18 , /*
*/ legend( order(5 4 3 2 1) /*
*/ label(1 "grade = 0") /*
*/ label(2 "grade = 4.5") /*
*/ label(3 "grade = 9") /*
*/ label(4 "grade = 13.5") /*
*/ label(5 "grade = 18"))
*-------------- end example --------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )
Hope this helps,
Maarten
-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands
visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434
+31 20 5986715
http://home.fsw.vu.nl/m.buis/
-----------------------------------------
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/