Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: broken stick (piecewise linear) regression


From   "Maarten Buis" <[email protected]>
To   <[email protected]>
Subject   st: RE: broken stick (piecewise linear) regression
Date   Tue, 24 May 2005 13:41:18 +0200

Dear Keith,

The paper you cite involves Bayesian statistics, and I am not familiar with how to implement the exact model they suggest. But you can do two things in Stata: 
* Assume the number of knots and their location is known, and estimate a spline regression. You can make the spline terms by hand or using -mkspline-. The manual entry of -mkspline- tells you how to do this.
* You can assume the number of knots is known, but not their location. In that case you would need to use non-linear least squares, see the manual entry for -nl-. It will also be helpful to be familiar with ways of coding splines, see the manual entry for -mkspline-. I have made a small example to illustrate using -nl- to estimate the location of a knot in spline regression. 

Hope this helps,
Maarten

----begin example-----
capture drop _all
set seed 123
set obs 1000

/*generate data with one knot located at .5, and coefficients of 1 before and -1 after the knot*/
gen x = uniform()
gen d = max(x-.5,0)
gen y = 1 + x -d + .25*invnorm(uniform())


gen d1 = max(x-.6,0) /*getting starting values assuming the knot is located at .6)*/
reg y x d1
global b0 = _b[_cons]
global b1 = _b[x]
global b2 = _b[d1]

/*y = b0 + b1 x + b2 (max(x-k1,0))*/

/*actual estimation*/
capture program drop nlspline 
program define nlspline
    version 8.2
    if "`1'" == "?" {
        global S_1 "b0 b1 b2 k1"
        global b0 = $b0
        global b1 = $b1
        global b2 = $b2
        global k1 = .6
        exit
    }
    replace `1' = $b0 + $b1*x + $b2*(max(x-$k1, 0))
end
nl spline y

----end example-----

-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of keith Farnsworth
Sent: dinsdag 24 mei 2005 12:35
To: [email protected]
Subject: st: broken stick (piecewise linear) regression

Dear all,
I intend to fit a broken stick model following Bacon and Watts 
"Estimating the transition between two intersecting straight lines", 
Biometrika 1971. I have seen code for this to use the S or R package's 
nls, which I suspect uses maximum likelihood (but not sure) [see 
http://www.biostat.wustl.edu/archives/html/s-news/2000-04/
msg00215.html].

I am not an S user, I am a Stata enthusiast!
However, I found no reference to this kind of model in Stata searches. 
I hope someone with more experience than me can explain how Stata will 
implement the Bacon and Watts model.
Thanks,

Keith.

Dr Keith Farnsworth
Lecturer in Mathematical and Behavioural Ecology
Queens University Belfast

Medical Biology Centre
97 Lisburn Road
Belfast BT9 7BL
Northern Ireland
phone +44 (0)2890 272352
fax       +44 (0)2890 236505

*
*   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/

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index