Doing piecewise regression using a binary independent variable as breakpoint in R -
first time trying use 'segmented' package in r , have (probably stupid) questions.
i implementing piecewise regression in r, trying this:
y <- c(621, 552, 581, 549, 563, 633, 531, 348, 323, 285, 282, 303, 192, 281, 466, 969, 1549, 1264, 447, 367) x1 <- c(727, 689, 702, 646, 677, 798, 832, 377, 366, 351, 316, 333, 291, 526, 466, 1488, 1549, 1431, 696, 687) x2 <- c(10, 10, 10, 10, 8, 8, 8, 10, 10, 10, 10, 8, 8, 8, 8, 10, 10, 10, 10, 10) x3 <- rep(c(0,1),10) df <- data.frame(cbind(y,x1,x2,x3)) fit.lm <- lm(y~x1+x2, data=df) piecewise <- segmented(fit.lm, seg.z = ~x3, psi=0.5)
i hope two-segment piecewise regression in can use variable x3, binary variable, break model 2 segments - 1 segment x3=0, , x3=1.
with in mind, therefore used 0.5 estimated break-point. however, got error message this:
error: @ least 1 coef na: breakpoint(s) @ boundary? (possibly many x-values replicated)
two questions:
- given intended do, code make sense?
- what's possible cause of error , how can fix it?
thanks in advance!
Comments
Post a Comment