Home > other >  Loop instead of iterative calculations in R
Loop instead of iterative calculations in R

Time:02-06

In my example below I have quarterly data from 2021Q1 to 2022Q3 for variable X. I have forecasted growth rate of variable X (growth_x) from 2022Q4 to 2025Q4. I want to use the growth_x variable to calculate the variable X from 2022Q4 to 2025Q4 iteratively.I am manually calculating it below and still missing 2025Q4. Is it possible to write a function to do it? I am fairly new to writing loops. Any help will be greatly appreciated. Thank you in advance.

library(readxl)
library(dplyr)
library(lubridate)

# Quarterly Data
data <- data.frame(c("2021Q1","2021Q2","2021Q3","2021Q4",
                     "2022Q1","2022Q2","2022Q3","2022Q4",
                     "2023Q1","2023Q2","2023Q3","2023Q4",
                     "2024Q1","2024Q2","2024Q3","2024Q4",
                     "2025Q1","2025Q2","2025Q3","2025Q4"),
              
                   # Variable X - Actuals upto 2022Q3           
                   c(804,511,479,462,
                     427,330,440,NA,
                     NA,NA,NA,NA,
                     NA,NA,NA,NA,
                     NA,NA,NA,NA),
                   
                   # Forecasted Growth rates of X from 2022Q4      
                    c(NA,NA,NA,NA,
                      NA,NA,NA,0.24,
                      0.49,0.65,0.25,0.71,
                      0.63,0.33,0.53,0.83,
                      0.87,0.19,0.99,0.16))  

# Renaming the columns
data<-data%>%rename(yrqtr=1,x=2,growth_x=3)

# Creating Date Variable
data<-data%>%mutate(year=substr(yrqtr,1,4),
                    qtr=substr(yrqtr,5,6),
                    mon=ifelse(qtr=="Q1",3,
                               ifelse(qtr=="Q2",6,
                                      ifelse(qtr=="Q3",9,12))),
                                date=make_date(year,mon,1))

# Computing Growth Rate from 2022Q3 to 2023Q3
Growth_2023_3<-data%>%mutate(forecast_x=(1 growth_x)*lag(x,4),
                                       x=ifelse(date>"2022-09-01",forecast_x,x))%>%select(-forecast_x)

# Computing Growth Rate from 2023Q3 to 2024Q3
Growth_2024_3<-Growth_2023_3%>%mutate(forecast_x=(1 growth_x)*lag(x,4),
                                          x=ifelse(date>"2023-09-01",forecast_x,x))%>%select(-forecast_x)

# Computing Growth Rate from 2024Q3 to 2025Q3
Growth_2025_3<-Growth_2024_3%>%mutate(forecast_x=(1 growth_x)*lag(x,4),
                             x=ifelse(date>"2024-09-01",forecast_x,x))%>%select(-forecast_x)

CodePudding user response:

Does this do what you want?

n_years <- length(unique(data$year))


for(i in unique(data$year)[2:n_years]){

# Computing Growth Rate from 2022Q3 to 2023Q3
data <- data %>% 
        mutate(forecast_x=(1 growth_x)*lag(x,4),
        x=ifelse(date > as.Date(paste0(i,"-09-01")),forecast_x,x)) 


}

As an aside, column names can be assigned at the time the data frame is created. For example:

# Quarterly Data
data <- data.frame(yrqtr = c("2021Q1","2021Q2"),        
                   x = c(804,511),
                   growth_x = c(0.24,0.49))  

CodePudding user response:

If you want to avoid using a loop, you can use purrr::reduce().

library(tidyverse)
library(lubridate)

sol <- reduce(
  .x = unique(data$year), # iterate over years
  .init = data,
  \(lhs, rhs) lhs %>%
    mutate(x = ifelse(year == rhs & is.na(x), (1 growth_x)*lag(x,4), x))
)
sol  
#>     yrqtr         x growth_x year qtr mon       date
#> 1  2021Q1  804.0000       NA 2021  Q1   3 2021-03-01
#> 2  2021Q2  511.0000       NA 2021  Q2   6 2021-06-01
#> 3  2021Q3  479.0000       NA 2021  Q3   9 2021-09-01
#> 4  2021Q4  462.0000       NA 2021  Q4  12 2021-12-01
#> 5  2022Q1  427.0000       NA 2022  Q1   3 2022-03-01
#> 6  2022Q2  330.0000       NA 2022  Q2   6 2022-06-01
#> 7  2022Q3  440.0000       NA 2022  Q3   9 2022-09-01
#> 8  2022Q4  572.8800     0.24 2022  Q4  12 2022-12-01
#> 9  2023Q1  636.2300     0.49 2023  Q1   3 2023-03-01
#> 10 2023Q2  544.5000     0.65 2023  Q2   6 2023-06-01
#> 11 2023Q3  550.0000     0.25 2023  Q3   9 2023-09-01
#> 12 2023Q4  979.6248     0.71 2023  Q4  12 2023-12-01
#> 13 2024Q1 1037.0549     0.63 2024  Q1   3 2024-03-01
#> 14 2024Q2  724.1850     0.33 2024  Q2   6 2024-06-01
#> 15 2024Q3  841.5000     0.53 2024  Q3   9 2024-09-01
#> 16 2024Q4 1792.7134     0.83 2024  Q4  12 2024-12-01
#> 17 2025Q1 1939.2927     0.87 2025  Q1   3 2025-03-01
#> 18 2025Q2  861.7802     0.19 2025  Q2   6 2025-06-01
#> 19 2025Q3 1674.5850     0.99 2025  Q3   9 2025-09-01
#> 20 2025Q4 2079.5475     0.16 2025  Q4  12 2025-12-01
  • Related