I wrote a user-defined function in a .R file.
lm_Forecast=function(d,h){
if (ncol(d)>=1){
d<-d[complete.cases(d),]
#lmmod <- lm(d$`Gross Sales` ~ paste(colnames(d)[2:length(colnames(d))], collapse = "+") , data = d[1:(nrow(d)-h),])
lmmod <- lm(d$`Gross Sales` ~ Inflation Food+ lagged_price , data = d[1:(nrow(d)-h),])
summary(lmmod)
print(colnames(d))
AIC(lmmod)
fcst <- predict(lmmod,d[(nrow(d)-h+1):nrow(d),])
#a<-data.frame(a=valid$Gross.Sales, p=predict)
#mean(abs(fcst/d[nrow(d)-h+1:nrow(d),]$Gross.Sales-1))
#fcst=forecast(fitArima,xreg=xreg[(length(x)+1):(length(x)+h),])$mean
} else {
fcst=mean(d)
}
print("lm model")
summary(lmmod)
return(fcst)
}
I am then invoking it as
j=lm_Forecast(d,Horizon)
from within the same file after the function definition.
I get the error
Error in lm_Forecast(d, Horizon) : could not find function "lm_Forecast"
despite the function being clearly present in the file before the statement that invokes it. Can anybody help me debug this? Thank you.