Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Moderator of knot location in hockey stick regression

 From Maarten Buis To statalist@hsphsun2.harvard.edu Subject Re: st: Moderator of knot location in hockey stick regression Date Wed, 13 Mar 2013 10:15:59 +0100

```On Tue, Mar 12, 2013 at 9:16 PM, Jordan Silberman wrote:
> We used a hockey stick regression model to identify the optimal knot
> location for a piecewise regression of healthcare costs on age. Now we
> want to ask the question: is the knot location observed for males
> different from that of females? Does anyone know of a way to test this
> hypothesis in Stata? What we basically want to do is test the null
> hypothesis that the optimal knot location observed for group 1 is the
> same as that of group 2.

You would need to use -nl- to estimate such a model. Moreover, you
would almost certainly need to specify starting values in order for
that model to converge. What usually works well for me is to first
look at a scatter plot to get an initial guess of where the knots
might be. Than create splines with knots at those initial guesses and
estimate a linear regression with those splines: the coefficients
together with the inital guesses of the knot location form your
starting values. Below is an example:

*------------------ begin example ------------------
sysuse auto, clear

gen price2 = price / 1000

// first take a look
separate price2, by(foreign)
twoway scatter price2? mpg

// 20 seems a nice starting value for
// domestic cars and 25 for foreign cars
mkspline mpg1dom 20 mpg2dom = mpg, marginal
mkspline mpg1for 25 mpg2for = mpg, marginal
gen byte domestic = (foreign == 0) if foreign < .
replace mpg1dom = mpg1dom*domestic
replace mpg2dom = mpg2dom*domestic
replace mpg1for = mpg1for*foreign
replace mpg2for = mpg2for*foreign

reg price2 mpg1dom mpg2dom mpg1for mpg2for

local cons = _b[_cons]
local b1 = _b[mpg1dom]
local b2 = _b[mpg2dom]
local b3 = _b[mpg1for]
local b4 = _b[mpg2for]

predict initial

// use -nl- to find the knot
nl (price2 = {cons=`cons'} + {b1=`b1'}*mpg*domestic +  ///
{b2=`b2'}*max(mpg -{k1=20},0)*domestic +  ///
{b3=`b3'}*mpg*foreign +                   ///
{b4=`b4'}*max(mpg -{k2=25},0)*foreign )

// test whether the knots are equal
test _b[k1:_cons] = _b[k2:_cons]

predict nl

// look at the predictions and see if they make sense
twoway scatter price2 mpg ||        ///
line initial nl mpg,         ///
sort by(foreign)             ///
legend(order(2 "first guess" ///
3 "estimated"))
*------------------- end example -------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```