Home > Software engineering >  Find average change in timeseries
Find average change in timeseries

Time:04-06

I have an annual mean timeseries dataset for 15 years, and I am trying to find the average change/increase/decrease in this timeseries.

The timeseries I have is spatial (average values for each grid-cell/pixel, years repeat).

How can I do this in R via dplyr?

Sample data

year = c(2005, 2005, 2005, 2005, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 2007, 2008, 2008, 2008, 2008)
Tmean = c(24, 24.5, 25.8,25, 24.8, 25, 23.5, 23.8, 24.8, 25, 25.2, 25.8, 25.3, 25.6, 25.2, 25)

Code

 library(tidyverse)

df = data.frame(year, Tmean)

    change = df$year %>%
      # Sort by year
      arrange(year) %>%
      mutate(Diff_change = Tmean - lag(Tmean), # Difference in Tmean between years
             Rate_percent = (Diff_change / year)/Tmean * 100) # Percent change # **returns inf values** 
    
    Average_change = mean(change$Rate_percent, na.rm = TRUE) 

CodePudding user response:

To find the average: mean(). To find the differences or changes: diff()

So, to find the average change:

> avg_change <- mean(diff(Tmean))
> print(avg_change)
[1] 0.06666667

If you need that in percentage, then you want to find out how much the difference between an element and its previous one (this year - last year) is in percentage with respect to last year, like so:

> pct_change <- Tmean[2:length(Tmean)] / Tmean[1:(length(Tmean)-1)] - 1
> avg_pct_change <- mean(pct_change) * 100
> print(avg_pct_change)
[1] 0.3101632

CodePudding user response:

We can put those vectors into a data frame to use with dplyr (...if that's how you want to do it; this is straightforward with base R as well).

library(dplyr)
df <- data.frame(year, Tmean)
change <- df %>%
  arrange(year) %>%
  mutate(Diff_change = Tmean - lag(Tmean), # Difference in Tmean between years
         Diff_time = year - lag(year),
         Rate_percent = (Diff_change/Diff_time)/lag(Tmean) * 100) # Percent change 

Average_change = mean(change$Rate_percent, na.rm = TRUE)  

Results (with updated question data)

> change
   year Tmean Diff_change Rate_percent
1  2005  24.0          NA           NA
2  2005  24.5         0.5    2.0833333
3  2005  25.8         1.3    5.3061224
4  2005  25.0        -0.8   -3.1007752
5  2006  24.8        -0.2   -0.8000000
6  2006  25.0         0.2    0.8064516
7  2006  23.5        -1.5   -6.0000000
8  2006  23.8         0.3    1.2765957
9  2007  24.8         1.0    4.2016807
10 2007  25.0         0.2    0.8064516
11 2007  25.2         0.2    0.8000000
12 2007  25.8         0.6    2.3809524
13 2008  25.3        -0.5   -1.9379845
14 2008  25.6         0.3    1.1857708
15 2008  25.2        -0.4   -1.5625000
16 2008  25.0        -0.2   -0.7936508
> Average_change
[1] 0.3101632
  • Related