简体   繁体   中英

Computation of yearwise breakpoints in R

I have daily rainfall data which I have converted to yearwise cumulative value using following code

library(tidyverse); library(segmented); library(seas); library(strucchange)

## get mscdata from "seas" packages
data(mscdata)
dat <- (mksub(mscdata, id=1108447))

## generate cumulative sum of rain by year
d2 <- dat %>% group_by(year) %>% mutate(rain_cs = cumsum(rain)) %>% ungroup

Then I want to compute of yearwise breakpoints using strucchange . I could able to do it for single year like

y <- subset(d2,year=="1992")$rain_cs
breakpoints(y ~ 1, breaks = 3)$breakpoints

I have used breaks = 3 to have 3 breakpoints. Now how to dynamically apply it year-wise to estimate breakpoints?

You can group_by year and use summarise in dplyr 1.0.0 which can generate multiple rows in summarise :

library(dplyr) 
library(strucchange)

d2 %>%
  group_by(year) %>%
  summarise(breakpoints = breakpoints(rain_cs~1, breaks = 3)$breakpoints)

#   year breakpoints
#   <int>       <dbl>
# 1  1975          73
# 2  1975         237
# 3  1975         301
# 4  1976          83
# 5  1976         166
# 6  1976         297
# 7  1977          98
# 8  1977         239
# 9  1977         311
#10  1978         102
# … with 80 more rows

To get data as 3 columns instead, we can store the output in a list and use unnest_wider .

d2 %>%
  group_by(year) %>%
  summarise(breakpoints = list(breakpoints(rain_cs~1,breaks = 3)$breakpoints)) %>%
  tidyr::unnest_wider(breakpoints) %>%
  tibble::column_to_rownames('year')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM