Search code examples
rheatwave

heatwaveR package, ts2clm() turn temperature values into NA


I'm using heatwaveR package in R to make a plot (event_line()) and visualize the heatwaves over the years. The first step is to run ts2clm(), but this command turn my temp colum into NA so I can't plot anything. Does anyone see any errors?

This is my data:

>>> Data
    t           temp
    [Date]      [num]
0   2020-05-14  6.9
1   2020-05-06  6.8
2   2020-04-23  5.5
3   2020-04-16  3.6
4   2020-03-31  2.5
5   2020-02-25  2.3
6   2020-01-30  2.8
7   2019-10-02  13.4
8   2022-09-02  19
9   2022-08-15  18.7
...
687 1974-05-06  4.2

This is my code:

#Load data
Data <- read_xlsx("seili_raw_temp.xlsx")

#Set t as class Date
Data$t <- as.Date(Data$t, format =  "%Y-%m-%d")

#Constructs seasonal and threshold climatologies
ts <- ts2clm(Data, climatologyPeriod = c("1974-05-06", "2020-05-14"))
#This is the point where almost all temp values turn into NA, so you can ignore below.

#Detect_even
res <- detect_event(ts)

#Draw heatwave plot
event_line(res, min_duration = "3",metric = "int_cum",
           start_date = c("1974-05-06"), end_date = c("2020-05-14"))

Solution

  • The data you posted isn't long enough to get the function to work, so I just made some up:

    library(heatwaveR)
    library(lubridate)
    set.seed(1234)
    Data <- data.frame(
      t = seq(ymd("2015-01-01"), ymd("2023-01-01"), by="7 day"))
    Data$temp <- runif(nrow(Data), 0,45)
    

    Then, when I execute the function, I get the result below. The problem is that your data (like the ones I generated) have one observation every 7 days. The ts2clm() function pads out the dataset so that every day has an entry and if a temperature was not observed on that day, it fills in with a missing value.

    ts <- ts2clm(Data, climatologyPeriod = c("2015-01-01", "2022-12-29"))
    ts
    #> # A tibble: 2,920 × 5
    #>      doy t           temp  seas thresh
    #>    <int> <date>     <dbl> <dbl>  <dbl>
    #>  1     1 2015-01-01  5.12  22.5   38.6
    #>  2     2 2015-01-02 NA     22.4   38.5
    #>  3     3 2015-01-03 NA     22.2   38.2
    #>  4     4 2015-01-04 NA     22.1   37.9
    #>  5     5 2015-01-05 NA     21.9   37.3
    #>  6     6 2015-01-06 NA     21.7   36.8
    #>  7     7 2015-01-07 NA     21.5   36.5
    #>  8     8 2015-01-08 28.0   21.3   36.1
    #>  9     9 2015-01-09 NA     21.2   36.1
    #> 10    10 2015-01-10 NA     21.0   35.8
    #> # … with 2,910 more rows
    

    Created on 2023-02-10 by the reprex package (v2.0.1)