Skip to content

best way to fit a model

4 messages · Ronaldo Reis Jr., Achim Zeileis

#
Hi,

I have some data that have this behaviour:

|
|*******
|        *
|          * 
|            *
|              *
|----------------

What is the best and simpler way to fit this in R?

Thanks
Ronaldo
#
On Fri, 9 Sep 2005 14:55:04 -0300 Ronaldo Reis-Jr. wrote:

            
If the changepoint is known, then this is straightforward using lm:

## generate example data
set.seed(20050909)
x <- seq(0, 10, by = 0.25)
y.mean <- ifelse(x >= 5, x, 5)
y <- y.mean + rnorm(41)
plot(y ~ x)
lines(y.mean ~ x)

## fit linear model with break
fm <- lm(y ~ I((x-5) * (x >= 5)))
fm
y.fit1 <- fitted(fm)
lines(y.fit1 ~ x, col = 2)

If it is unknown, it can be estimated using Vito Muggeo's segmented
package:

## estimate change point in x
library("segmented")
sfm <- segmented(lm(y ~ x), x, 6)
sfm
y.fit2 <- fitted(sfm)
lines(y.fit2 ~ x, col = 3)

This fits a continuous mean function. Alternatively, breakpoints() in
strucchange can be used to estimate a break point:

## estimate break point in x
library("strucchange")
bp <- breakpoints(y ~ x)
summary(bp)
y.fit3 <- fitted(bp)
lines(y.fit3 ~ x, col = 4)

This does not enforce that the line is continuous, hence the jump in the
fitted mean. Of course, the estimated breakpoint could be used to fit a
continuous line model, but this is not what is optimized in
breakpoints().
Z
3 days later
#
Em Sex 09 Set 2005 15:25, Achim Zeileis escreveu:
I try this. But my doubt now is:

How to justify this kind of analysis? Why dont use any linearized or non 
linear regression to fit this? Something like a log function (I try but is 
not a good function).

Thanks
Ronaldo
#
On Tue, 13 Sep 2005 13:24:02 -0300 Ronaldo Reis-Jr. wrote:

            
The motivation usually comes from subject-matter knowledge. For example,
such changepoint models are accepted by practitioners as good proxies
for certain biological processes. If you think that the data-generating
process might contain a structural change, why not model it as such?
Z