library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
DataCPSim |>
as_tibble() |>
mutate(
region = rep(1:4, times = diff(c(0, changepoints(y), nobs(y$segmenter)))),
id = row_number()
) |>
group_by(region) |>
summarize(
N = n(), first = min(id), last = max(id), mean = mean(value), var = var(value)
)
#> # A tibble: 4 × 6
#> region N first last mean var
#> <int> <int> <int> <int> <dbl> <dbl>
#> 1 1 547 1 547 35.3 127.
#> 2 2 275 548 822 58.2 372.
#> 3 3 150 823 972 96.8 927.
#> 4 4 124 973 1096 157. 2426.
Note that the first region is correct, but the others are off by a rounding error.
Created on 2024-06-17 with reprex v2.1.0
changepoint
appears to be including the changepoints as the closed right end of the intervals, whereas we are using it as the closed left end.