Loops for forecasting

Hello, I have panel data for some kidney centers. now I need to do linear forecasting them. How can I not using the repetition of coding and be able to do them in 1 coding set up?

Data in panel format, 20 kidney center+6 periods.

See the code below:
KTV.raw<-read.csv(file="KTV_Reporting(1).csv")

library(stargazer)
library(fpp3)
library(ggplot2)
#Auburn kidney center
Auburn.data<-KTV.raw%>%filter(ClinicName=="Auburn Kidney Center")
ggplot(Auburn.data)+ geom_line(mapping = aes(y=Score, x= Month))+
labs(x="Month", y="Score")+
ggtitle("Auburn Kidney Center KTV Matrix")

Auburn.data1<-as_tsibble(Auburn.data, index=Month)
autoplot(Auburn.data1, Score)

#Auburn.data1|>filter(year(Month) >= 2022)

forecast_Auburn <- Auburn.data1 |>
model(TSLM(Score~trend()))
fc_Auburn <- forecast(forecast_Auburn,h=6)

fc_Auburn |>
autoplot(Auburn.data1,show_gap= FALSE) +
labs(
title = "Forecasts of KTV Matrix-Auburn Kidney Center",
y = "KTV Score")

#Bellevue Kidney center
Bellevue.data<-KTV.raw%>%filter(ClinicName=="Bellevue Kidney Center")
Bellevue.data1<-as_tsibble(Bellevue.data, index=Month)
forecast_Bellevue <- Bellevue.data1 |>
model(TSLM(Score~trend()))

fc_Bellevue <- forecast(forecast_Bellevue,h=6)
fc_Bellevue |>
autoplot(Bellevue.data1,show_gap= FALSE) +
labs(
title = "Forecasts of KTV Matrix-Bellevue Kidney Center",
y = "KTV Score")

Here is a code example of how to reduce code repetition, using a reproducible example.
I hope that if you work through this example you will see it shows a general approach to a common problem or reducing repetative code that was likely made by copy-pasting and changing names.

you want to extract Auburn related info , then you want to do the same with Bellevue, etc.
Here is an analogous example using a common data example for R users called iris which has information about 3 species of plant

# lets say I want to do seperate ggplots from iris for each species

library(tidyverse)
# naive attempt 1
 
setosa <- filter(iris,Species=="setosa")
setosa_plot <- ggplot(data=setosa)+aes(x=Petal.Length,
                                       y=Petal.Width)+ geom_point()

versicolor <- filter(iris,Species=="versicolor")
versicolor_plot <- ggplot(data=versicolor)+aes(x=Petal.Length,
                                               y=Petal.Width)+ geom_point()


virginica <- filter(iris,Species=="virginica")
virginica_plot <- ggplot(data=virginica)+aes(x=Petal.Length,
                                             y=Petal.Width)+ geom_point()

#this works ; it makes 3 plots, but is heavily repetitive

# step 1 is to *rewrite as a function*

do_one <- function(x){
  temp<- filter(iris,Species==x) # we parameterise the Species now its whatever x is
  temp_plot<- ggplot(data=temp)+aes(x=Petal.Length,
                                         y=Petal.Width)+ geom_point()
  temp_plot # returning an object that is the result ; its name is not important and can be the same each time
}
# using manually
(set_p <- do_one("setosa"))
(ver_p <- do_one("versicolor"))
(vir_p <- do_one("virginica"))

# This is clearly a step in the right direction, but we are still repeating the calls 3 times, and 
#we may have analyis that requires us to do it 1000 times; so why do it manually ? when you 
# can iterate over a list; this is functional programming
# version2

# xnames will contain all the values we will pass to the function; 
# also if we repeat those values as the names on the cector,
# we will retain those names in the resulting list we are making
(xnames <- unique(iris$Species))
names(xnames) <- xnames

# map says to do the function for each item we give it and collect the results into a list
(my_list_of_result <- map(xnames,
                         \(x){do_one(x)
                         }))

# the results are there, to access them use them from the list i.e. 
my_list_of_result$versicolor

maybe I should turn this into a blog post, or canned response for this forum...

Thank you so much for your response!!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.