Home > database >  R compute average increase for a timeseries dataset
R compute average increase for a timeseries dataset

Time:07-19

Let' say I have the following dataset.

How can I compute the average increase in times (I don't know a proper word for this)?

Meaning you know like I want to tell the audience, that the average temperature in 2021 increased this many times compared to 1980.

Sample data (AvgTMean):

structure(list(year = 1980:2021, AvgTMean = c(24.2700686838937, 
23.8852956598276, 25.094446596092, 24.1561175050287, 24.157183605977, 
24.3047482638362, 24.7899738481466, 24.5756232655603, 24.5833086228592, 
24.7344695534483, 25.3094451071121, 25.2100615173707, 24.3651692293534, 
24.5423890611494, 25.2492166633908, 24.7005097837931, 24.2491591827443, 
25.0912281781322, 25.0779264303305, 24.403294248319, 24.4983991453592, 
24.4292324356466, 24.8179824927011, 24.7243948463075, 24.5086534543966, 
24.2818632071983, 24.4567195220259, 24.8402224356034, 24.6574465515086, 
24.5440715673563, 23.482670620977, 24.9979594684914, 24.5452453980747, 
24.9271462811494, 24.7443215819253, 25.8929839790805, 25.1801908261063, 
25.2079308058908, 25.0722425561207, 25.4554644289799, 25.4548979078736, 
25.0756772250287)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-42L)) 

Code:

library(tidyverse)

AvgTMean %>% # Stuck...?

CodePudding user response:

You can use the slope of the regression line to get the increase per year based on fitting a linear model to the data.

fm <- lm(AvgTMean ~ year, DF); fm
## 
## Call:
## lm(formula = AvgTMean ~ year, data = DF)
##
## Coefficients:
## (Intercept)         year  
##    -6.93064      0.01582  <-- 0.01582 degree increase per yr

plot(DF)
abline(fm)

screenshot

CodePudding user response:

data <- structure(list(year = 1980:2021, AvgTMean = c(24.2700686838937, 
                                          23.8852956598276, 25.094446596092, 24.1561175050287, 24.157183605977, 
                                          24.3047482638362, 24.7899738481466, 24.5756232655603, 24.5833086228592, 
                                          24.7344695534483, 25.3094451071121, 25.2100615173707, 24.3651692293534, 
                                          24.5423890611494, 25.2492166633908, 24.7005097837931, 24.2491591827443, 
                                          25.0912281781322, 25.0779264303305, 24.403294248319, 24.4983991453592, 
                                          24.4292324356466, 24.8179824927011, 24.7243948463075, 24.5086534543966, 
                                          24.2818632071983, 24.4567195220259, 24.8402224356034, 24.6574465515086, 
                                          24.5440715673563, 23.482670620977, 24.9979594684914, 24.5452453980747, 
                                          24.9271462811494, 24.7443215819253, 25.8929839790805, 25.1801908261063, 
                                          25.2079308058908, 25.0722425561207, 25.4554644289799, 25.4548979078736, 
                                          25.0756772250287)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                      -42L)) 




 n <- length(data$AvgTMean) ##n to identify the position of your reference/last year (last value)
 reference <- data$AvgTMean[n]
 increase <- (reference/data$AvgTMean)

You will get a numerical vector indicating how much the AvgTMean increased in respect to last year/value temperature for each year.

  • Related