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")
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...