I refer to this answer: https://stackoverflow.com/a/65076441/14436230
I am trying to predict the "Education" value for 2019 using past values for each year, using lm(Education ~ poly(TIME,2)).
However, I will have to apply this lm named function(TIME) to each "LOCATION", which I was able to create separate lm for each LOCATION in m.
Following the answer in the link attached, I was able to run my code until my_predict. When I run sapply , I get an error Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list"
Can someone advise me on my mistake? I will really appreciate any help.
linear_model <- function(TIME) lm(Education ~ poly(TIME,2), data=table2)
m <- lapply(split(table2,table2$LOCATION),linear_model)
new_df <- data.frame(TIME=c(2019))
my_predict <- function(TIME) predict(m,new_df)
sapply(m,my_predict) #error here
EDIT:
I am now able to predict education values for each "LOCATION" for 2020 and 2021 as shown below.
linear_model <- function(x) lm(Education ~ TIME, x)
m <- lapply(split(tableLinR,tableLinR$LOCATION),linear_model)
new_df <- data.frame(TIME=c(2020, 2021), row.names = c ("2020.Education", "2021.Education"))
my_predict <- function(x) predict(x,new_df)
result <- sapply(m,my_predict)
However, I actually wish to do this for more Independent Variables (e.g. Education, GDP, Hoursworked, PPI etc.) as shown in my column header:
Can someone advise me on how do I create a loop for my code to create a dataframe with the predicted values? I have struggled for so many hours but failed to do so.


