piesewise linear regression set constraints










0















from this question and answer, I'm trying to make a picewise linear regression with onknown breakpoint.



The difference, is that I need so contraint the slope on either side fo the breakpoint, wich i'm not able to do yet.



my data looks like this. I would like an function, so I can map it over multiple nested datasets.



dput(head(my_data, 30 ))
structure(list(vo2 = c(1.967, 3.113, 2.881, 2.931, 2.809, 2.802,
2.937, 3.235, 3.238, 3.118, 3.177, 2.959, 2.741, 3.157, 2.975,
2.986, 3.231, 2.448, 2.966, 2.834, 3.559, 3.37, 3.187, 3.079,
3.076, 2.848, 3.16, 3.285, 3.159, 3.305), vco2 = c(1.552, 2.458,
2.303, 2.372, 2.264, 2.284, 2.352, 2.566, 2.585, 2.506, 2.6,
2.441, 2.251, 2.592, 2.418, 2.428, 2.665, 2.039, 2.437, 2.298,
2.891, 2.733, 2.609, 2.514, 2.538, 2.286, 2.497, 2.59, 2.489,
2.606)), row.names = c(NA, -30L), class = c("tbl_df", "tbl",
"data.frame"))


The answer i'm working from is here
https://stackoverflow.com/a/15877616/9368078
the code is here



function (x,y) 
{
f <- function (Cx)

lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- lm(y ~ lhs(x) + rhs(x))
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])


r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x)))
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]
plot(x,y)
abline(coef1+best_Cx*coef2,-coef2) #lhs
abline(coef1-best_Cx*coef3,coef3) #rs


inspiration solution



T've tried the following alternatives, so I can contraint the slope. Here I try to set boundaries on the coef in optimize.



f <- function (Cx) 
lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- lm(y ~ lhs(x) + rhs(x))
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])

r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x), lower = c(-100,-100,1), upper = c(100,1,100)) )
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]
plot(x,y)
abline(coef1+best_Cx*coef2,-coef2) #lhs
abline(coef1-best_Cx*coef3,coef3) #rs


Here I try an nls solution. Where I try to set boundaries on the variables.



f <- function (Cx) 
lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- nls(y ~ lhs(x) + rhs(x), start = c(0,1.5),
algorithm = "port",
lower = c(-inf,-inf,1),
upper = c(inf,1,inf) )
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])

r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x)) )
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]


But neither solution works for me. So I'm defiently doing something wrong.



I'm into sports science, and trying to learn a bit of statistics.



Can someone point my in a direction regarding a solution.










share|improve this question






















  • Check out CRAN package segmented. It might save you time to use tested functions than to try to write your own.

    – Rui Barradas
    Nov 12 '18 at 12:31












  • @RuiBarradas, thanks for your suggestion. I've been messing around with segmented. But I can not figure out, how to constain the slope in segmented. Do you know if it is possible?

    – olesendan
    Nov 12 '18 at 13:38











  • I don't believe so, what you can is to pass an initial estimate for the breakpoint.

    – Rui Barradas
    Nov 12 '18 at 14:50















0















from this question and answer, I'm trying to make a picewise linear regression with onknown breakpoint.



The difference, is that I need so contraint the slope on either side fo the breakpoint, wich i'm not able to do yet.



my data looks like this. I would like an function, so I can map it over multiple nested datasets.



dput(head(my_data, 30 ))
structure(list(vo2 = c(1.967, 3.113, 2.881, 2.931, 2.809, 2.802,
2.937, 3.235, 3.238, 3.118, 3.177, 2.959, 2.741, 3.157, 2.975,
2.986, 3.231, 2.448, 2.966, 2.834, 3.559, 3.37, 3.187, 3.079,
3.076, 2.848, 3.16, 3.285, 3.159, 3.305), vco2 = c(1.552, 2.458,
2.303, 2.372, 2.264, 2.284, 2.352, 2.566, 2.585, 2.506, 2.6,
2.441, 2.251, 2.592, 2.418, 2.428, 2.665, 2.039, 2.437, 2.298,
2.891, 2.733, 2.609, 2.514, 2.538, 2.286, 2.497, 2.59, 2.489,
2.606)), row.names = c(NA, -30L), class = c("tbl_df", "tbl",
"data.frame"))


The answer i'm working from is here
https://stackoverflow.com/a/15877616/9368078
the code is here



function (x,y) 
{
f <- function (Cx)

lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- lm(y ~ lhs(x) + rhs(x))
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])


r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x)))
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]
plot(x,y)
abline(coef1+best_Cx*coef2,-coef2) #lhs
abline(coef1-best_Cx*coef3,coef3) #rs


inspiration solution



T've tried the following alternatives, so I can contraint the slope. Here I try to set boundaries on the coef in optimize.



f <- function (Cx) 
lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- lm(y ~ lhs(x) + rhs(x))
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])

r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x), lower = c(-100,-100,1), upper = c(100,1,100)) )
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]
plot(x,y)
abline(coef1+best_Cx*coef2,-coef2) #lhs
abline(coef1-best_Cx*coef3,coef3) #rs


Here I try an nls solution. Where I try to set boundaries on the variables.



f <- function (Cx) 
lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- nls(y ~ lhs(x) + rhs(x), start = c(0,1.5),
algorithm = "port",
lower = c(-inf,-inf,1),
upper = c(inf,1,inf) )
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])

r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x)) )
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]


But neither solution works for me. So I'm defiently doing something wrong.



I'm into sports science, and trying to learn a bit of statistics.



Can someone point my in a direction regarding a solution.










share|improve this question






















  • Check out CRAN package segmented. It might save you time to use tested functions than to try to write your own.

    – Rui Barradas
    Nov 12 '18 at 12:31












  • @RuiBarradas, thanks for your suggestion. I've been messing around with segmented. But I can not figure out, how to constain the slope in segmented. Do you know if it is possible?

    – olesendan
    Nov 12 '18 at 13:38











  • I don't believe so, what you can is to pass an initial estimate for the breakpoint.

    – Rui Barradas
    Nov 12 '18 at 14:50













0












0








0








from this question and answer, I'm trying to make a picewise linear regression with onknown breakpoint.



The difference, is that I need so contraint the slope on either side fo the breakpoint, wich i'm not able to do yet.



my data looks like this. I would like an function, so I can map it over multiple nested datasets.



dput(head(my_data, 30 ))
structure(list(vo2 = c(1.967, 3.113, 2.881, 2.931, 2.809, 2.802,
2.937, 3.235, 3.238, 3.118, 3.177, 2.959, 2.741, 3.157, 2.975,
2.986, 3.231, 2.448, 2.966, 2.834, 3.559, 3.37, 3.187, 3.079,
3.076, 2.848, 3.16, 3.285, 3.159, 3.305), vco2 = c(1.552, 2.458,
2.303, 2.372, 2.264, 2.284, 2.352, 2.566, 2.585, 2.506, 2.6,
2.441, 2.251, 2.592, 2.418, 2.428, 2.665, 2.039, 2.437, 2.298,
2.891, 2.733, 2.609, 2.514, 2.538, 2.286, 2.497, 2.59, 2.489,
2.606)), row.names = c(NA, -30L), class = c("tbl_df", "tbl",
"data.frame"))


The answer i'm working from is here
https://stackoverflow.com/a/15877616/9368078
the code is here



function (x,y) 
{
f <- function (Cx)

lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- lm(y ~ lhs(x) + rhs(x))
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])


r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x)))
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]
plot(x,y)
abline(coef1+best_Cx*coef2,-coef2) #lhs
abline(coef1-best_Cx*coef3,coef3) #rs


inspiration solution



T've tried the following alternatives, so I can contraint the slope. Here I try to set boundaries on the coef in optimize.



f <- function (Cx) 
lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- lm(y ~ lhs(x) + rhs(x))
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])

r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x), lower = c(-100,-100,1), upper = c(100,1,100)) )
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]
plot(x,y)
abline(coef1+best_Cx*coef2,-coef2) #lhs
abline(coef1-best_Cx*coef3,coef3) #rs


Here I try an nls solution. Where I try to set boundaries on the variables.



f <- function (Cx) 
lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- nls(y ~ lhs(x) + rhs(x), start = c(0,1.5),
algorithm = "port",
lower = c(-inf,-inf,1),
upper = c(inf,1,inf) )
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])

r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x)) )
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]


But neither solution works for me. So I'm defiently doing something wrong.



I'm into sports science, and trying to learn a bit of statistics.



Can someone point my in a direction regarding a solution.










share|improve this question














from this question and answer, I'm trying to make a picewise linear regression with onknown breakpoint.



The difference, is that I need so contraint the slope on either side fo the breakpoint, wich i'm not able to do yet.



my data looks like this. I would like an function, so I can map it over multiple nested datasets.



dput(head(my_data, 30 ))
structure(list(vo2 = c(1.967, 3.113, 2.881, 2.931, 2.809, 2.802,
2.937, 3.235, 3.238, 3.118, 3.177, 2.959, 2.741, 3.157, 2.975,
2.986, 3.231, 2.448, 2.966, 2.834, 3.559, 3.37, 3.187, 3.079,
3.076, 2.848, 3.16, 3.285, 3.159, 3.305), vco2 = c(1.552, 2.458,
2.303, 2.372, 2.264, 2.284, 2.352, 2.566, 2.585, 2.506, 2.6,
2.441, 2.251, 2.592, 2.418, 2.428, 2.665, 2.039, 2.437, 2.298,
2.891, 2.733, 2.609, 2.514, 2.538, 2.286, 2.497, 2.59, 2.489,
2.606)), row.names = c(NA, -30L), class = c("tbl_df", "tbl",
"data.frame"))


The answer i'm working from is here
https://stackoverflow.com/a/15877616/9368078
the code is here



function (x,y) 
{
f <- function (Cx)

lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- lm(y ~ lhs(x) + rhs(x))
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])


r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x)))
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]
plot(x,y)
abline(coef1+best_Cx*coef2,-coef2) #lhs
abline(coef1-best_Cx*coef3,coef3) #rs


inspiration solution



T've tried the following alternatives, so I can contraint the slope. Here I try to set boundaries on the coef in optimize.



f <- function (Cx) 
lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- lm(y ~ lhs(x) + rhs(x))
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])

r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x), lower = c(-100,-100,1), upper = c(100,1,100)) )
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]
plot(x,y)
abline(coef1+best_Cx*coef2,-coef2) #lhs
abline(coef1-best_Cx*coef3,coef3) #rs


Here I try an nls solution. Where I try to set boundaries on the variables.



f <- function (Cx) 
lhs <- function(x) ifelse(x < Cx,Cx-x,0)
rhs <- function(x) ifelse(x < Cx,0,x-Cx)
fit <- nls(y ~ lhs(x) + rhs(x), start = c(0,1.5),
algorithm = "port",
lower = c(-inf,-inf,1),
upper = c(inf,1,inf) )
c(summary(fit)$r.squared,
summary(fit)$coef[1], summary(fit)$coef[2],
summary(fit)$coef[3])

r2 <- function(x) -(f(x)[1])

res <- optimize(r2,interval=c(min(x),max(x)) )
res <- c(res$minimum,f(res$minimum))

best_Cx <- res[1]
coef1 <- res[3]
coef2 <- res[4]
coef3 <- res[5]


But neither solution works for me. So I'm defiently doing something wrong.



I'm into sports science, and trying to learn a bit of statistics.



Can someone point my in a direction regarding a solution.







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 11:50









olesendanolesendan

12




12












  • Check out CRAN package segmented. It might save you time to use tested functions than to try to write your own.

    – Rui Barradas
    Nov 12 '18 at 12:31












  • @RuiBarradas, thanks for your suggestion. I've been messing around with segmented. But I can not figure out, how to constain the slope in segmented. Do you know if it is possible?

    – olesendan
    Nov 12 '18 at 13:38











  • I don't believe so, what you can is to pass an initial estimate for the breakpoint.

    – Rui Barradas
    Nov 12 '18 at 14:50

















  • Check out CRAN package segmented. It might save you time to use tested functions than to try to write your own.

    – Rui Barradas
    Nov 12 '18 at 12:31












  • @RuiBarradas, thanks for your suggestion. I've been messing around with segmented. But I can not figure out, how to constain the slope in segmented. Do you know if it is possible?

    – olesendan
    Nov 12 '18 at 13:38











  • I don't believe so, what you can is to pass an initial estimate for the breakpoint.

    – Rui Barradas
    Nov 12 '18 at 14:50
















Check out CRAN package segmented. It might save you time to use tested functions than to try to write your own.

– Rui Barradas
Nov 12 '18 at 12:31






Check out CRAN package segmented. It might save you time to use tested functions than to try to write your own.

– Rui Barradas
Nov 12 '18 at 12:31














@RuiBarradas, thanks for your suggestion. I've been messing around with segmented. But I can not figure out, how to constain the slope in segmented. Do you know if it is possible?

– olesendan
Nov 12 '18 at 13:38





@RuiBarradas, thanks for your suggestion. I've been messing around with segmented. But I can not figure out, how to constain the slope in segmented. Do you know if it is possible?

– olesendan
Nov 12 '18 at 13:38













I don't believe so, what you can is to pass an initial estimate for the breakpoint.

– Rui Barradas
Nov 12 '18 at 14:50





I don't believe so, what you can is to pass an initial estimate for the breakpoint.

– Rui Barradas
Nov 12 '18 at 14:50












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261562%2fpiesewise-linear-regression-set-constraints%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261562%2fpiesewise-linear-regression-set-constraints%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo