Narendra Modi
2016-Jul-05 18:41 UTC
[R] How to extract "specific"/"last" intercept value from segmented package.
I am able to perform regression on a dataset as below: plot(x,y) lin.mod <- lm(y~x) m <- mean(x) m segmented.mod <- segmented(lin.mod, seg.Z = ~x, psi= m) plot(segmented.mod, add=T) sl <- slope(segmented.mod) inter <- intercept(segmented.mod) summary(segmented.mod) # Show Summary sl # show all the slopes inter # show all the intercepts In my dataset, the above method correctly identifies the breakpoints and hence I get two intercepts.> inter$x Est. intercept1 -3.269 intercept2 -19.980 What I am interested is the "intercept2" value. How can I obtain this? The method needs to be dynamic as in if the next dataset has 3 intercepts, I would like to get "intercept3 value. PD [[alternative HTML version deleted]]
ruipbarradas at sapo.pt
2016-Jul-05 19:28 UTC
[R] How to extract "specific"/"last" intercept value from segmented package.
Hello, Try dimnames(inter$x)[[1]] You could have seen this by inspecting 'inter': str(inter) Hope this helps, Rui Barradas ? Citando Narendra Modi <bjpmodi2016 at gmail.com>:> I am able to perform regression on a dataset as below: > > plot(x,y) > lin.mod <- lm(y~x) > m <- mean(x) > m > > segmented.mod <- segmented(lin.mod, seg.Z = ~x, psi= m) > > plot(segmented.mod, add=T) > sl <- slope(segmented.mod) > inter <- intercept(segmented.mod) > > summary(segmented.mod)? ? # Show Summary > sl? ? ? ? ? ? ? ? ? ? ? ? # show all the slopes > inter? ? ? ? ? ? ? ? ? ? ?# show all the intercepts > > In my dataset, the above method correctly identifies the breakpoints and > hence I get two intercepts. >> inter > > $x > ? ? ? ? ? ? ?Est. > intercept1? -3.269 > intercept2 -19.980 > > What I am interested is the "intercept2" value. How can I obtain this? > > The method needs to be dynamic as in if the next dataset has 3 intercepts, > I would like to get "intercept3 value. > > PD > > ? ? ? ? [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.htmland provide commented, > minimal, self-contained, reproducible code.? [[alternative HTML version deleted]]
David L Carlson
2016-Jul-05 19:33 UTC
[R] How to extract "specific"/"last" intercept value from segmented package.
You should use only plain text emails, provide sample data, and indicate any relevant packages that you had to load. First let's load the necessary package and create some data:> library(segmented) > set.seed(42) > x <- sort(runif(60, 10, 60)) > int <- c(rep(0, 20), rep(60, 20), rep(-80, 20)) > slp <- c(rep(2, 20), rep(0, 20), rep(3, 20)) > y <- int + slp*x + rnorm(60, 0, 2) > plot(x, y)You should have a plot showing 2 break points. Using psi=mean(x) puts a single breakpoint in the middle of the distribtution so we have to be more specific:> lin.mod <- lm(y~x) > segmented.mod <- segmented(lin.mod, seg.Z = ~x, psi= c(25, 55)) > plot(segmented.mod, add=T) > sl <- slope(segmented.mod) > inter <- intercept(segmented.mod)Now your plot shows the segmented model. You need to know what intercept() is returning so you should look at the manual page: ?intercept. It returns a list of matrices (one matrix for each independent variable). You have only one, x, so you get a list with one element.> str(inter)List of 1 $ x: num [1:3, 1] -0.179 60.25 -86.77 ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:3] "intercept1" "intercept2" "intercept3" .. ..$ : chr "Est." To get the first matrix without knowing its name and the last row:> tail(inter[[1]], 1)Est. intercept3 -86.77 If you want to strip off the labels:> as.vector(tail(inter[[1]], 1))[1] -86.77 ----------------------------- David L. Carlson Department of Anthropology Texas A&M University -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Narendra Modi Sent: Tuesday, July 5, 2016 1:42 PM To: R-help at r-project.org Subject: [R] How to extract "specific"/"last" intercept value from segmented package. I am able to perform regression on a dataset as below: plot(x,y) lin.mod <- lm(y~x) m <- mean(x) m segmented.mod <- segmented(lin.mod, seg.Z = ~x, psi= m) plot(segmented.mod, add=T) sl <- slope(segmented.mod) inter <- intercept(segmented.mod) summary(segmented.mod) # Show Summary sl # show all the slopes inter # show all the intercepts In my dataset, the above method correctly identifies the breakpoints and hence I get two intercepts.> inter$x Est. intercept1 -3.269 intercept2 -19.980 What I am interested is the "intercept2" value. How can I obtain this? The method needs to be dynamic as in if the next dataset has 3 intercepts, I would like to get "intercept3 value. PD [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Narendra Modi
2016-Jul-06 17:49 UTC
[R] How to extract "specific"/"last" intercept value from segmented package.
Thanks! that worked. I also tested with the below method although your solution is faster and done in fewer steps. inter <- intercept(segmented.mod) inter.m <- as.matrix(inter$x) inter.row <- nrow(inter.m) answer <- inter.m[inter.row,1] PD On Tue, Jul 5, 2016 at 2:28 PM, <ruipbarradas at sapo.pt> wrote:> Hello, > > Try > > dimnames(inter$x)[[1]] > > You could have seen this by inspecting 'inter': > > str(inter) > > Hope this helps, > > Rui Barradas > > > Citando Narendra Modi <bjpmodi2016 at gmail.com>: > > I am able to perform regression on a dataset as below: > > plot(x,y) > lin.mod <- lm(y~x) > m <- mean(x) > m > > segmented.mod <- segmented(lin.mod, seg.Z = ~x, psi= m) > > plot(segmented.mod, add=T) > sl <- slope(segmented.mod) > inter <- intercept(segmented.mod) > > summary(segmented.mod) # Show Summary > sl # show all the slopes > inter # show all the intercepts > > > In my dataset, the above method correctly identifies the breakpoints and > hence I get two intercepts. > > inter > > $x > Est. > intercept1 -3.269 > intercept2 -19.980 > > What I am interested is the "intercept2" value. How can I obtain this? > > The method needs to be dynamic as in if the next dataset has 3 intercepts, > I would like to get "intercept3 value. > > PD > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.htmland provide commented, minimal, > self-contained, reproducible code. > > >