Error: could not find the "Log" function

When starting model0 , which was found below, I received the error message “Error in eval (expr, envir, enclos): could not find the“ Postpone "function. Before posting this message, I also looked at this forum on the Internet, but I could not deem it necessary. I believe that the error in my model is probably not related to the Lag function, since the other models (1 and 2) shown below can work without any problems.

My main motive was to launch the GAM model by going through a list of explanatory variables and their delays.

 library(quantmod) library(gamair) library(mgcv) data(chicago) names(chicago) varlist0 <- c("pm10median", "pm25median", "o3median", "so2median") model0<- lapply(varlist0, function(x) { gam(substitute(death ~ s(time,bs="cr",k=200)+ s(tmpd,bs="cr") + Lag(i,0:4) , list(i = as.name(x))),family=quasipoisson,na.action=na.omit, data=chicago) }) 

Gam + Lag without error message:

 model1<- gam(death ~ s(time,bs="cr",k=200)+ s(tmpd,bs="cr") + Lag(pm10median, 0:4),family=quasipoisson,na.action=na.omit, data=chicago) 

Lm with delay and without error message:

 hsb2 <- read.csv("http://www.ats.ucla.edu/stat/data/hsb2.csv") varlist <- names(hsb2)[8:11] models <- lapply(varlist, function(x) { lm(substitute(read ~ Lag(i,0:4) , list(i = as.name(x))), data = hsb2) }) 

I can not decipher the cause of this error. What did I do wrong in the first model?

+4
source share
1 answer

I can’t say the exact reason, but it has something to do with the environment in which mgcv evaluates the formula. A safer approach than substitute is this:

 varlist0 <- c("pm10median", "pm25median", "o3median", "so2median") model0 <- lapply(varlist0,function(v) { f <- sprintf("death ~ s(time,bs='cr',k=200)+s(tmpd,bs='cr') + Lag(%s,0:4)",v) gam(as.formula(f),family=quasipoisson,na.action=na.omit,data=chicago) }) 
+3
source

Source: https://habr.com/ru/post/1447979/


All Articles