How to export breakpoints index into a vector from strucchange package?

I'm using the strucchange package in order to determine where my curve model segments.

My code looks like this:

l.fit <- loess(data$y~data$x)
px <- predict(l.fit,newdata=data$x)
bp <- breakpoints(px~1)

Now, the output of bp:

Optimal 6-segment partition: 

Call:
breakpoints.formula(formula = px ~ 1)

Breakpoints at observation number:
161 254 347 440 533 

Corresponding to breakdates:
0.2571885 0.4057508 0.5543131 0.7028754 0.8514377 

The breakdates are kind of useless, as my x scale goes from 1 to 700 or so. But the breakpoints themselves are exactly what I want, specifically the last one, 533.

If i type:

bp[1]

I get:

$breakpoints
[1] 161 254 347 440 533

but if I try to assign bp[1] to a new vector, I get nothing. Is there anyway I can isolate the last breakpoint, i.e. 533, and assign it to a variable so I can plot a vertical line at the breakpoint? I need to do this for a couple hundred datasets, so being able to isolate the breakpoints would be really useful.

Thanks!

Answers


[ extraction doesn't drop to the simpliest form, so it remains a list. Use [[ or $ extraction instead to get a numeric vector.

bps <- bp[[1]]

or

bps <- bp$breakpoints

To get the last one, you would need to either count its length and select the last one, or reverse it and select the first:

bps[length(bps)]

or

rev(bps)[1]

Need Your Help

ggplot2: side by side barplot with one bar stacked and the other not

r ggplot2 bar-chart

I am trying to create a barplot where for each category, two bars are plotted (side by side): one is for the "total", the other is stacked by subgroups. For example, in the following data frame, 'n...

What can webkit do?

javascript html css webkit

Since there are more and more apps going the web way (like Phonegap, Appcelerator over the mobile and desktop apps development and the ones that I don't know yet), I am wondering what kind of role ...