AIM : Show the utility and limitation of running a trend analysis in MHW metrics in conjonction of the heatwaveR package.
Document Note : The maps, plots and app within this document are interactive so make sure you give them a play like zooming in and out in the maps but also on the plots. Clicking on the legend allows to only select and display the time series needed.
Data - Downloading and Preparing NOAA OISST Data By Robert W Schlegel and AJ Smit From https://robwschlegel.github.io/heatwaveR/articles/OISST_preparation.html
Event Detection - Uses heatwaveR:: (Schlegel and Smit (2018)), translation of GitHub python functions from Eric C.J. Oliver (published in paper Holbrook et al. (2019) A global assessment of marine heatwaves and their drivers) https://robwschlegel.github.io/heatwaveR/articles/gridded_event_detection.html
The file YearSeason_NZ_BIOREGION_MeanMetrics_MHWDays_Pixels_1982_2021_OISST.Rds results from the functions from Schlegel and Smit (2018), used on OISST v2.1 data for NZ EEZ, using climatologyPeriod = c(“1982-01-01,” “2011-01-01”). MHW metrics are grouped by season and year for each pixels following the methology of Thoral et al. (2022).
Here we load the resulting dataframe for sake of clarity.
mhw_metrics_mean_realm <- readRDS("C:/Users/thoralf/OneDrive - NIWA/Documents/Collab/NZ_Coastal_MHW_2022/Data/YearSeason_NZ_BIOREGION_MeanMetrics_MHWDays_Pixels_1982_2021_OISST.Rds")
most_extreme_MHW_year_intensity <- mhw_metrics_mean_realm %>%
dplyr::filter(metrics == 'int_cumulative') %>%
group_by(lon, lat, year) %>%
summarise(max_intensity = max(values)) %>%
dplyr::filter(max_intensity==max(max_intensity))
## `summarise()` has grouped output by 'lon', 'lat'. You can override using the `.groups` argument.
pal <- c(brewer.pal(9,'Purples'),brewer.pal(9,'Blues'),brewer.pal(9,'Oranges'),brewer.pal(9,'Reds'))
p_intensity <- ggplot() +
#geom_tile(data=most_extreme_MHW_year_intensity,aes(x=lon,y=lat,fill=year)) +
geom_raster(data=most_extreme_MHW_year_intensity,aes(x=lon,y=lat,fill=year)) +
scale_fill_gradientn(colours=pal) +
ggtitle('Year of Most Intense MHW events (1981-2021)') +
borders('world2',xlim=c(170,200),ylim=c(-60,-20),fill='grey') +
#xlim(c(170,180)) + ylim(c(-40,-30)) +
theme_bw()
ggplotly(p_intensity)
Figure 1 - Year of Most Intense MHW around New Zealand EEZ.
MEOW from Spalding et al. (2007).
meow_NZ_lonlat2 <- st_read('C:/Users/thoralf/OneDrive - NIWA/Documents/InSitu_Data/Marine_Ecoregions_Of_the_World_(MEOW)-shp/Marine_Ecoregions_Of_the_World__MEOW_.shp') %>%
dplyr:::filter(PROVINCE %in% c('Northern New Zealand', 'Southern New Zealand','Subantarctic New Zealand')) %>%
st_make_valid(.) %>%
st_transform(.,crs = 4326) %>%
st_shift_longitude() %>%
mutate(ECOREGION = factor(ECOREGION,levels=c("Kermadec Island", "Three Kings-North Cape", "Northeastern New Zealand",
"Central New Zealand","Chatham Island","South New Zealand",
"Bounty and Antipodes Islands","Snares Island","Auckland Island","Campbell Island")))
## Reading layer `Marine_Ecoregions_Of_the_World__MEOW_' from data source
## `C:\Users\thoralf\OneDrive - NIWA\Documents\InSitu_Data\Marine_Ecoregions_Of_the_World_(MEOW)-shp\Marine_Ecoregions_Of_the_World__MEOW_.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 232 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -20037510 ymin: -30240970 xmax: 20037510 ymax: 23063390
## Projected CRS: WGS 84 / Pseudo-Mercator
map_bioregion <- ggplot() +
geom_sf(data=meow_NZ_lonlat2,aes(fill=ECOREGION)) +
scale_fill_viridis(begin = 0, end = .75,option="viridis",discrete = T) +
borders('world2',xlim=c(160,190),ylim=c(-55,-25),fill = 'grey') + xlim(c(160,190)) + ylim(c(-55,-25)) +
theme(panel.background = element_rect(fill = 'aliceblue')) +
xlab('') + ylab('') +
theme(legend.position = "right",
legend.title = element_blank(),
legend.text = element_text(size=16))
map_bioregion
Figure 2 - MEOW ecoregions around New Zealand EEZ.
MHW_global_FullTS <- mhw_metrics_mean_realm %>%
pivot_wider(names_from = metrics,values_from = values) %>%
group_by(year) %>%
summarize(Number_MHW_days = sum(nMHWdays)/5921,
Nevents = sum(nevents)/5921,
Mean_Intensity = mean(int_mean),
Maximum_Intensity = mean(int_max),
Cumulative_Intensity = sum(int_cumulative)/5921) %>%
complete(year = 1982:2021) %>%
#ungroup() %>%
pivot_longer(-year,names_to='Metrics',values_to='values') %>%
mutate(Metrics = factor(Metrics,levels=c("Number_MHW_days", "Nevents", "Mean_Intensity", "Maximum_Intensity", "Cumulative_Intensity")))
metrics.labs <- c(`Number_MHW_days` = "Number of MHW days",
`Nevents` = "Number of Events",
`Mean_Intensity` = "Mean Intensity (°C)",
`Maximum_Intensity` = "Maximum Intensity (°C)",
`Cumulative_Intensity` = "Cumulative Intensity (°C Days)")
p <- ggplot(MHW_global_FullTS, aes(x=year,y=values,col=Metrics)) +
facet_wrap(Metrics~.,scales = 'free',labeller = as_labeller(metrics.labs),ncol=2) +
geom_line(size=.5) +
geom_smooth(se=T) +
scale_colour_viridis(begin = 0, end = .75,option="viridis",discrete = T) +
ylab('') + xlab('Year') +
theme_bw() +
theme(legend.position="none")
ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Figure 3.
MHW_global_trends_FullTS <- MHW_global_FullTS %>% group_by(Metrics) %>% nest() %>%
mutate(ts_out = purrr::map(data, ~ts(.x$values,start=1982,end=2021,frequency = 1))) %>%
mutate(sens = purrr::map(ts_out, ~sens.slope(.x, conf.level = 0.95)),
pettitt = purrr::map(ts_out, ~pettitt.test(.x)),
lm = purrr::map(data,~lm(values ~ year,.x))) %>%
mutate(Sens_Slope = as.numeric(unlist(sens)[1]),P_Value =as.numeric(unlist(sens)[3]),
Change_Point_Year = time(ts_out[[1]])[as.numeric(unlist(pettitt)[3])],Change_Point_pvalue =as.numeric(unlist(pettitt)[4]),
lm_slope = unlist(lm)$coefficients.year) %>%
# Add step of cutting time series in 2 using Change_Point_Year
mutate(pre_ts = purrr::map(ts_out,~window(.x,start=1982,end=Change_Point_Year)),
post_ts = purrr::map(ts_out,~window(.x,start=Change_Point_Year,end=2021))) %>%
# Add step of calculating sen's slope and p-value to pre and post change point year
mutate(sens_pre = purrr::map(pre_ts, ~sens.slope(.x, conf.level = 0.95)),
Sens_Slope_pre = as.numeric(unlist(sens_pre)[1]),P_Value_pre =as.numeric(unlist(sens_pre)[3]),
sens_post = purrr::map(post_ts, ~sens.slope(.x, conf.level = 0.95)),
Sens_Slope_post = as.numeric(unlist(sens_post)[1]),P_Value_post =as.numeric(unlist(sens_post)[3])) %>%
dplyr::select(Metrics,Sens_Slope,P_Value,Change_Point_Year,Change_Point_pvalue,lm_slope,Sens_Slope_pre,P_Value_pre,Sens_Slope_post,P_Value_post)
kable(MHW_global_trends_FullTS,caption = "Table 1 - Sens's Slope and p-value.") %>%
kable_classic()
| Metrics | Sens_Slope | P_Value | Change_Point_Year | Change_Point_pvalue | lm_slope | Sens_Slope_pre | P_Value_pre | Sens_Slope_post | P_Value_post |
|---|---|---|---|---|---|---|---|---|---|
| Number_MHW_days | 1.4856217 | 0.0002019 | 2012 | 0.0057340 | 2.0688737 | 0.3295052 | 0.3767968 | 11.3593143 | 0.0490980 |
| Nevents | 0.1083118 | 0.0001150 | 1997 | 0.0054742 | 0.1095160 | -0.0298842 | 0.8218917 | 0.1456023 | 0.0234862 |
| Mean_Intensity | 0.0006986 | 0.6664042 | 2014 | 0.5718403 | 0.0013345 | -0.0024991 | 0.2580163 | 0.0045287 | 0.9015386 |
| Maximum_Intensity | 0.0046997 | 0.2301174 | 2012 | 0.0934928 | 0.0049157 | -0.0052801 | 0.1533785 | 0.0310895 | 0.2104977 |
| Cumulative_Intensity | 1.9307473 | 0.0002907 | 2012 | 0.0057340 | 2.9045268 | 0.3975435 | 0.4545553 | 15.0757903 | 0.0490980 |
One can be more interested in finding patterns in MHW trends between ecoregions.
npix_realms <- mhw_metrics_mean_realm %>%
group_by(lon,lat,ECOREGION) %>%
tally() %>%
group_by(ECOREGION) %>%
tally() %>%
rename(npix = n)
#X TOTAL 5,921
# 1 Kermadec Island 829
# 2 Three Kings-North Cape 364
# 3 Northeastern New Zealand 581
# 4 Central New Zealand 1,242
# 5 Chatham Island 665
# 6 South New Zealand 496
# 7 Bounty and Antipodes Islands 776
# 8 Snares Island 208
# 9 Auckland Island 352
# 10 Campbell Island 408
mhw_metrics_mean_realm <- left_join(mhw_metrics_mean_realm,npix_realms,by='ECOREGION')
MHW_realm_season_FullTS <- mhw_metrics_mean_realm %>%
pivot_wider(names_from = metrics,values_from = values) %>%
group_by(year,ECOREGION,season,npix) %>%
summarize(Number_MHW_days = sum(nMHWdays),
Nevents = sum(nevents),
Mean_Intensity = mean(int_mean),
Maximum_Intensity = mean(int_max),#get the max instead of mean(max)--> nope, tells different story
Cumulative_Intensity = sum(int_cumulative)) %>%
mutate(Number_MHW_days = Number_MHW_days/npix,
Nevents = Nevents/npix,
Cumulative_Intensity = Cumulative_Intensity/npix) %>%
group_by(ECOREGION,season) %>%
complete(year = 1982:2021) %>%
dplyr::select(-npix) %>%
pivot_longer(-c(year,ECOREGION,season),names_to='Metrics',values_to='values') %>%
mutate(Metrics = factor(Metrics,levels=c("Number_MHW_days", "Nevents", "Mean_Intensity", "Maximum_Intensity", "Cumulative_Intensity")))
## `summarise()` has grouped output by 'year', 'ECOREGION', 'season'. You can override using the `.groups` argument.
MHW_realm_season_FullTS$values[is.na(MHW_realm_season_FullTS$values)] <- 0
p <- MHW_realm_season_FullTS %>%
dplyr::filter(Metrics == 'Number_MHW_days') %>%
ggplot(., aes(x=year,y=values,col=season)) +
facet_wrap(ECOREGION~.,ncol=3) +
geom_line(size=.5) +
geom_smooth(se=T) +
ylab('Number of MHW days') + xlab('Year') +
scale_colour_viridis(begin = 0, end = .75,option="inferno",discrete = T) +
theme_bw() +
theme(#legend.position="bottom",
legend.position=c(0.8,0.05),
legend.title = element_blank())+
guides(colour = guide_legend(override.aes = list(shape=15,size=2)))
ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Figure 4. Seasonal trend in number of MHW days for ecoregions around NZ.
We could also show similar graphs for other metrics, like number of events, min, max or cumulative intensity.
MHW_realms_season_trends_FullTS <- MHW_realm_season_FullTS %>% group_by(Metrics,ECOREGION,season) %>% nest() %>%
mutate(ts_out = purrr::map(data, ~ts(.x$values,start=1982,end=2021,frequency = 1))) %>%
mutate(sens = purrr::map(ts_out, ~sens.slope(.x, conf.level = 0.95)),
pettitt = purrr::map(ts_out, ~pettitt.test(.x)),
lm = purrr::map(data,~lm(values ~ year,.x))) %>%
mutate(Sens_Slope = as.numeric(unlist(sens)[1]),P_Value =as.numeric(unlist(sens)[3]),
Change_Point_Year = time(ts_out[[1]])[as.numeric(unlist(pettitt)[3])],Change_Point_pvalue =as.numeric(unlist(pettitt)[4]),
lm_slope = unlist(lm)$coefficients.year) %>%
# Add step of cutting time series in 2 using Change_Point_Year
mutate(pre_ts = purrr::map(ts_out,~window(.x,start=1982,end=Change_Point_Year)),
post_ts = purrr::map(ts_out,~window(.x,start=Change_Point_Year,end=2021))) %>%
# Add step of calculating sen's slope and p-value to pre and post change point year
mutate(sens_pre = purrr::map(pre_ts, ~sens.slope(.x, conf.level = 0.95)),
Sens_Slope_pre = as.numeric(unlist(sens_pre)[1]),P_Value_pre =as.numeric(unlist(sens_pre)[3]),
sens_post = purrr::map(post_ts, ~sens.slope(.x, conf.level = 0.95)),
Sens_Slope_post = as.numeric(unlist(sens_post)[1]),P_Value_post =as.numeric(unlist(sens_post)[3])) %>%
ungroup() %>% #To remove Season column
dplyr::select(ECOREGION,Metrics,season,Sens_Slope,P_Value,Change_Point_Year,Change_Point_pvalue,lm_slope,Sens_Slope_pre,P_Value_pre,Sens_Slope_post,P_Value_post) %>%
dplyr::filter(Metrics == 'Number_MHW_days')
kable(MHW_realms_season_trends_FullTS,caption = "Table 2 - Sens's Slope and p-value.") %>%
kable_classic()
| ECOREGION | Metrics | season | Sens_Slope | P_Value | Change_Point_Year | Change_Point_pvalue | lm_slope | Sens_Slope_pre | P_Value_pre | Sens_Slope_post | P_Value_post |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Kermadec Island | Number_MHW_days | Summer | 0.3776782 | 0.0006927 | 1996 | 0.0229596 | 0.4035149 | -0.0595899 | 0.6169283 | 0.6059036 | 0.0275134 |
| Kermadec Island | Number_MHW_days | Autumn | 0.4007722 | 0.0001253 | 1994 | 0.0011874 | 0.7101183 | -0.0208082 | 0.4201127 | 0.4626055 | 0.0930928 |
| Kermadec Island | Number_MHW_days | Winter | 0.3881723 | 0.0000207 | 1997 | 0.0000837 | 0.6271762 | -0.0018094 | 0.4361867 | 0.6081768 | 0.0798387 |
| Kermadec Island | Number_MHW_days | Spring | 0.3235992 | 0.0000171 | 1997 | 0.0005322 | 0.4545867 | -0.0060917 | 0.6847231 | 0.6593620 | 0.0161476 |
| Three Kings-North Cape | Number_MHW_days | Summer | 0.5104146 | 0.0003038 | 2009 | 0.0112464 | 0.6568689 | 0.0839874 | 0.4065751 | 2.5716575 | 0.0123716 |
| Three Kings-North Cape | Number_MHW_days | Autumn | 0.4079836 | 0.0000080 | 1997 | 0.0007372 | 0.7903810 | 0.0000000 | 0.3956184 | 0.6331481 | 0.0525668 |
| Three Kings-North Cape | Number_MHW_days | Winter | 0.2666896 | 0.0002776 | 1996 | 0.0016182 | 0.4500897 | -0.0164835 | 0.5194961 | 0.3467878 | 0.1228530 |
| Three Kings-North Cape | Number_MHW_days | Spring | 0.1297036 | 0.0099837 | 1996 | 0.0554328 | 0.3388025 | -0.1858974 | 0.1233079 | 0.3228022 | 0.1028745 |
| Northeastern New Zealand | Number_MHW_days | Summer | 0.3740513 | 0.0005837 | 2012 | 0.0269500 | 0.5812906 | 0.0755403 | 0.3236287 | 4.2386690 | 0.0122661 |
| Northeastern New Zealand | Number_MHW_days | Autumn | 0.2625176 | 0.0005419 | 1994 | 0.0075444 | 0.6978272 | 0.0000000 | 0.6058268 | 0.3681870 | 0.0969403 |
| Northeastern New Zealand | Number_MHW_days | Winter | 0.1284423 | 0.0016520 | 1996 | 0.0016182 | 0.2582548 | 0.0000000 | 0.9603528 | 0.0263913 | 0.8600328 |
| Northeastern New Zealand | Number_MHW_days | Spring | 0.0652190 | 0.0865819 | 1997 | 0.1560348 | 0.2861837 | -0.0610298 | 0.1751873 | 0.0976424 | 0.5593049 |
| Central New Zealand | Number_MHW_days | Summer | 0.2713350 | 0.0057573 | 2009 | 0.0239047 | 0.5175112 | -0.0285185 | 0.8900068 | 2.7614734 | 0.0327356 |
| Central New Zealand | Number_MHW_days | Autumn | 0.4169547 | 0.0001202 | 1997 | 0.0025427 | 0.6813643 | 0.0056434 | 0.7177062 | 0.5805153 | 0.0972765 |
| Central New Zealand | Number_MHW_days | Winter | 0.2774104 | 0.0007916 | 1996 | 0.0004510 | 0.3894107 | -0.0024155 | 0.8043361 | 0.0418680 | 0.7243397 |
| Central New Zealand | Number_MHW_days | Spring | 0.1005720 | 0.0489502 | 1996 | 0.0617446 | 0.2315279 | -0.0251061 | 0.5526151 | 0.0189521 | 0.8947871 |
| Chatham Island | Number_MHW_days | Summer | 0.1844110 | 0.0328924 | 2000 | 0.1422534 | 0.5038222 | -0.0556391 | 0.6485076 | 0.5642857 | 0.2362892 |
| Chatham Island | Number_MHW_days | Autumn | 0.2489469 | 0.0008204 | 2010 | 0.0195030 | 0.8094297 | 0.0160627 | 0.4191888 | 4.4657040 | 0.0467449 |
| Chatham Island | Number_MHW_days | Winter | 0.6403846 | 0.0000076 | 1998 | 0.0002567 | 1.0209522 | 0.0021930 | 0.8687193 | 1.5165703 | 0.0349986 |
| Chatham Island | Number_MHW_days | Spring | 0.3451128 | 0.0010169 | 1998 | 0.0158392 | 0.6703680 | 0.0732331 | 0.4837558 | 0.6147556 | 0.1886332 |
| South New Zealand | Number_MHW_days | Summer | 0.1320004 | 0.1051362 | 2009 | 0.1176521 | 0.3725614 | -0.0626848 | 0.3523033 | 1.3402218 | 0.1605559 |
| South New Zealand | Number_MHW_days | Autumn | 0.3305756 | 0.0034145 | 2011 | 0.0239047 | 0.6355332 | 0.0129928 | 0.6676867 | 2.9637097 | 0.2129119 |
| South New Zealand | Number_MHW_days | Winter | 0.3189635 | 0.0022632 | 1996 | 0.0269500 | 0.4297337 | 0.0025202 | 0.9604827 | 0.4196429 | 0.1717582 |
| South New Zealand | Number_MHW_days | Spring | 0.0634297 | 0.1550901 | 2012 | 0.2625189 | 0.2726884 | -0.0081452 | 0.6707378 | 0.6214718 | 0.5915050 |
| Bounty and Antipodes Islands | Number_MHW_days | Summer | 0.0951282 | 0.1269388 | 2012 | 0.0617446 | 0.3751413 | -0.1067171 | 0.1849323 | 3.8537371 | 0.0490980 |
| Bounty and Antipodes Islands | Number_MHW_days | Autumn | 0.2583063 | 0.0029513 | 2010 | 0.0229596 | 0.5146589 | 0.0061343 | 0.8362801 | 3.4236469 | 0.0236422 |
| Bounty and Antipodes Islands | Number_MHW_days | Winter | 0.4260716 | 0.0000319 | 2009 | 0.0045391 | 0.8166855 | 0.0706780 | 0.1728195 | 3.3291398 | 0.0173434 |
| Bounty and Antipodes Islands | Number_MHW_days | Spring | 0.2717891 | 0.0260579 | 2009 | 0.0060051 | 0.6057146 | -0.0749678 | 0.1990812 | 1.8077964 | 0.2996654 |
| Snares Island | Number_MHW_days | Summer | 0.1324661 | 0.0924099 | 2013 | 0.2159504 | 0.3757482 | 0.0000000 | 0.8193622 | 1.9138622 | 0.2514522 |
| Snares Island | Number_MHW_days | Autumn | 0.3297858 | 0.0023423 | 2012 | 0.0195030 | 0.6542250 | 0.0033654 | 0.5711667 | 3.2516026 | 0.3710934 |
| Snares Island | Number_MHW_days | Winter | 0.2589130 | 0.0016520 | 1996 | 0.0291661 | 0.4609788 | -0.0144231 | 0.7278582 | 0.5312500 | 0.0524215 |
| Snares Island | Number_MHW_days | Spring | 0.0823135 | 0.1413285 | 2012 | 0.0816562 | 0.2036662 | -0.0257353 | 0.3479093 | 0.7331731 | 0.8580277 |
| Auckland Island | Number_MHW_days | Summer | 0.0136249 | 0.6159755 | 2015 | 0.4681690 | 0.1436674 | -0.0330579 | 0.2121995 | 0.9339489 | 0.2295562 |
| Auckland Island | Number_MHW_days | Autumn | 0.2681056 | 0.0026409 | 2011 | 0.0203206 | 0.5361526 | 0.0093344 | 0.6298136 | 1.4028409 | 0.2129119 |
| Auckland Island | Number_MHW_days | Winter | 0.3401883 | 0.0000703 | 1999 | 0.0057340 | 0.6106228 | 0.0903409 | 0.3051751 | 0.8201486 | 0.0506570 |
| Auckland Island | Number_MHW_days | Spring | 0.1318024 | 0.0933146 | 2011 | 0.0303332 | 0.3860689 | -0.0340909 | 0.2180424 | 1.6825284 | 0.4362749 |
| Campbell Island | Number_MHW_days | Summer | 0.0221639 | 0.6324839 | 2016 | 0.2485006 | 0.2251483 | -0.0677083 | 0.1548707 | -0.3741830 | 0.7071142 |
| Campbell Island | Number_MHW_days | Autumn | 0.1423875 | 0.0083344 | 2010 | 0.0496843 | 0.5126885 | 0.0000000 | 0.8504669 | 2.5588235 | 0.0864711 |
| Campbell Island | Number_MHW_days | Winter | 0.2274393 | 0.0004533 | 1999 | 0.0072105 | 0.5328096 | 0.0243566 | 0.5959124 | 0.5077614 | 0.1696455 |
| Campbell Island | Number_MHW_days | Spring | 0.0885695 | 0.1485067 | 2011 | 0.0904066 | 0.4764851 | -0.0575980 | 0.2318764 | 2.1746324 | 0.2757578 |
According to the previous tables, the trend and break point analyses are usefull to asses changes in MHW metrics at these scales. But MHWs are by definition discrete events in space and time. So some can argue that having a pixel and event-based approach is preferable. Can we still apply this methodology on pixel scale?
We use the time series of SST available within the heatwaveR package (sst_WA) and investigate the trend analysis in 5 metrics by grouping metric values per year as previously.
ts <- ts2clm(sst_WA, climatologyPeriod = c("1982-01-01", "2011-12-31"))
mhw <- detect_event(ts)
mhw_metrics_year <- mhw$climatology %>%
dplyr::filter(!is.na(event_no)) %>%
mutate(year = year(t),
intensity = temp-seas) %>%
group_by(event,year) %>%
summarise(nevents = length(unique(event_no)),
nMHWdays = length(t),
int_cumulative = sum(intensity),
int_mean = mean(intensity),
int_max = max(intensity)) %>%
# Need to complete time series in case of years with no MHWs in order to get full ts in trend analysis
complete(year = 1982:2018) %>%
ungroup() %>%
dplyr::select(-event) %>%
pivot_longer(-c(year),names_to='Metrics',values_to='values')
## `summarise()` has grouped output by 'event'. You can override using the `.groups` argument.
#Replace NAs by 0?
mhw_metrics_year$values[is.na(mhw_metrics_year$values)] <- 0
p <- ggplot(mhw_metrics_year,aes(x=year,y=values)) +
geom_point() +
geom_line() +
facet_wrap(~Metrics,scales = 'free') +
geom_smooth(se=T,method = 'lm') +
xlab('Year') +
theme_bw() +
theme(legend.position="none")
ggplotly(p)
## `geom_smooth()` using formula 'y ~ x'
Figure 5. Trend in 5 MHW metrics for the sst_WA time series.
The lm() regression seems to suject some upward trends in the metrics. What does Mann-Kendall have to say about these?
MHW_trends <- mhw_metrics_year %>% group_by(Metrics) %>% nest() %>%
mutate(ts_out = purrr::map(data, ~ts(.x$values,start=1982,end=2018,frequency = 1))) %>%
mutate(sens = purrr::map(ts_out, ~sens.slope(.x, conf.level = 0.95)),
pettitt = purrr::map(ts_out, ~pettitt.test(.x)),
lm = purrr::map(data,~lm(values ~ year,.x))) %>%
mutate(Sens_Slope = as.numeric(unlist(sens)[1]),P_Value =as.numeric(unlist(sens)[3]),
Change_Point_Year = time(ts_out[[1]])[as.numeric(unlist(pettitt)[3])],Change_Point_pvalue =as.numeric(unlist(pettitt)[4]),
lm_slope = unlist(lm)$coefficients.year) %>% #,
#lm_pvalue = parameters::p_value(aa$lm[[1]])[2,2]) %>%
#How to include p-value of lm? summary(aa$lm[[3]])$coefficient[,4] or parameters::p_value(aa$lm[[3]])[2,2]
# Add step of cutting time series in 2 using Change_Point_Year
mutate(pre_ts = purrr::map(ts_out,~window(.x,start=1982,end=Change_Point_Year)),
post_ts = purrr::map(ts_out,~window(.x,start=Change_Point_Year,end=2018))) %>%
# Add step of calculating sen's slope and p-value to pre and post change point year
mutate(sens_pre = purrr::map(pre_ts, ~sens.slope(.x, conf.level = 0.95)),
Sens_Slope_pre = as.numeric(unlist(sens_pre)[1]),P_Value_pre =as.numeric(unlist(sens_pre)[3]),
sens_post = purrr::map(post_ts, ~sens.slope(.x, conf.level = 0.95)),
Sens_Slope_post = as.numeric(unlist(sens_post)[1]),P_Value_post =as.numeric(unlist(sens_post)[3])) %>%
dplyr::select(Metrics,Sens_Slope,P_Value,Change_Point_Year,Change_Point_pvalue,lm_slope,Sens_Slope_pre,P_Value_pre,Sens_Slope_post,P_Value_post)
kable(MHW_trends,caption = "Table 3 - Sens's Slope and p-value.") %>%
kable_classic()
| Metrics | Sens_Slope | P_Value | Change_Point_Year | Change_Point_pvalue | lm_slope | Sens_Slope_pre | P_Value_pre | Sens_Slope_post | P_Value_post |
|---|---|---|---|---|---|---|---|---|---|
| nevents | 0.0000000 | 0.4914680 | 2006 | 0.4124316 | 0.0346136 | 0.0000000 | 0.3784001 | -0.2916667 | 0.1710692 |
| nMHWdays | 0.0000000 | 0.3216169 | 1995 | 0.3395085 | 0.9663348 | -0.5555556 | 0.1416319 | 0.0000000 | 0.8998992 |
| int_cumulative | 0.0000000 | 0.3152513 | 1995 | 0.3204836 | 2.0934086 | -0.8027143 | 0.1124402 | 0.0000000 | 0.9198687 |
| int_mean | 0.0000000 | 0.3152513 | 1995 | 0.2847829 | 0.0097617 | -0.0084139 | 0.1763236 | 0.0000000 | 0.7628066 |
| int_max | 0.0064262 | 0.1939960 | 1995 | 0.2223932 | 0.0292780 | -0.0277000 | 0.1416319 | 0.0000000 | 0.9598837 |
For some reason, the Mann-Kendall and Sen Slope analyses don’t seem to be useful here as they return suspiciously too low trends (vs lm_slope, which is the traditional linear regression using lm()) and quite high p-values
Summarizing MHW metrics by year in a given pixel will likely result in some years with no MHWs, hence bringing some 0 values into the time series. I am not too sure how sensitive the MK and Pettitt (breakpoint) analyses are to 0 values, but I can dig this out.