I have a dataset below for which I want to do linear regression and then get their fitted values and upper and lower limit in a single data frame.
The dataset has five weeks of data for each country and area combination. i.e. for US and G combination linear regression model will be built where amount will regress against week in five weeks and then this model will be used to predict 6 weeks. My final data frame would look like this
What I did is for one combination
data <- data.frame(country = c("US","US","US","US","US","US","US","US","US","US","UK","UK","UK","UK","UK"),
Area = c("G","G","G","G","G","I","I","I","I","I","A","A","A","A","A"),
week = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),amount = c(12,23,34,32,12,12,34,45,65,45,45,34,23,43,43))
data_1 <- data[(data$country=="US" & data$Area=="G"),]
# model building on initial five weeks
model <- lm(amount ~ week, data = data_1)
# creating one future week: week no 6
future <- data.frame(country="US",Area="G",week=6,amount = NA)
data2 <- rbind(data_1,future)
# prediction on all 6 weeks
pre <- predict(model,newdata = data2,interval = "prediction",level = 0.95)
pre
I took hint from here Looping linear regression output in a data frame in r
But not able to replicate this to include 6th weeks prediction for all other combination. Please help!